Java OCA OCP Practice Question 777

Question

Consider the following code snippet ...

boolean [] b1 = new boolean [2]; 
boolean [] b2 =  {true , false}; 
System.out.println ( "" +  (b1[0] == b2 [0]) + ", "+  (b1[1] == b2 [1])  ); 

What will it print ?

Select 1 option

  • A. It will not compile.
  • B. It will throw ArrayIndexOutOfBoundsException at Runtime.
  • C. false, true
  • D. true, false
  • E. It will print false, false.


Correct Option is  : C

Note

Whenever you create an array all of its elements are automatically given defaults values.

Numeric types are initialized to 0, objects are initialized to null, and booleans to false.

So if you have, float[] f = new float[3]; f[0], f[1] and f[2] will all be 0.0.

if you have Object[] o = new String[3]; o[0], o[1] and o[2] will all be null.

In this case, b 1[0] and b 1[1] are false.

whereas b2[0] and b2[1] are true and false.

So the answer is false and true.




PreviousNext

Related