Java OCA OCP Practice Question 466

Question

What will the following code snippet print?

int index = 1; 
String [] strArr = new String [5]; 
String myStr  = strArr [index]; 
System.out.println(myStr); 

Select 1 option

  • A. nothing
  • B. null
  • C. It will throw ArrayIndexOutOfBounds at runtime.
  • D. It will print some junk value.
  • E. None of the above.


Correct Option is  : B

Note

When you create an array of Objects (here, Strings) all the elements are initialized to null.

So in the line 3, null is assigned to myStr.

Note that. empty string is "" (String str = "";) and is not same as null.




PreviousNext

Related