Java OCA OCP Practice Question 1386

Question

What would be the result of trying to compile and run the following program?

public class Main{ 
   int [] myArray = new int [1]; 
   Object myObject []  = new Object [1]; 
   boolean bool; 
   public static void main (String args []){ 
      Main test = new Main (); 
      System .out.println (test.myArray [0] + "  " + test.myObject [0]+"  "+test.bool); 
    } 
} 

Select 1 option

  • A. The program will fail to compile, because of uninitialized variable 'bool'.
  • B. The program will throw a java.lang.NullPointerException when run.
  • C. The program will print "0 null false".
  • D. The program will print "0 null true".
  • E. The program will print null and false but will print junk value for myArray[0].


Correct Option is  : C

Note

Following are the default values that instance variables are initialized with if not initialized explicitly:

types (byte, short, char, int, long, float, double) to 0 ( or 0.0 ).

All Object types to null.

boolean to false.




PreviousNext

Related