Java OCA OCP Practice Question 1495

Question

What will the following program print?

public class Main{ 
  public static void main (String [] args){ 
    String str = "111"; 
    boolean[] myArray = new boolean [1]; 
    if ( myArray [0] ) str = "222"; 
    System.out.println (str); 
   } 
} 

Select 1 option

  • A. 111
  • B. 222
  • C. It will not compile as myArray [0] is uninitialized.
  • D. It will throw an exception at runtime.
  • E. None of the above.


Correct Option is  : A

Note

All the arrays are initialized to contain the default values of their type.

int[] myArray = new int[10]; will contain 10 integers with a value of 0.

Object[] oA = new Object[10]; will contain 10 object references pointing to null.

boolean[] myArray = new boolean[10] will contain 10 booleans of value false.

myArray[0] is false, the if condition fails and str remains 111.




PreviousNext

Related