Java OCA OCP Practice Question 619

Question

In the following application, the value of list has been omitted.

Assuming the code compiles without issue, which one of the following is not a possible output of executing this class?

package mypkg; /*from   w ww . j a v  a  2  s .  co m*/
? 
public class Main { 
   private Boolean[] list = // value omitted 
   public int m() { 
      int count=0; 
      for(int i=0; i<10; i++) { 
         if(list[i]) ++count; 
      } 
      return count; 
   } 
   public static void main(String[] roster) { 
      new Main().m(); 
   } 
} 
  • A. A stack trace for NullPointerException is printed.
  • B. A stack trace for ArrayIndexOutOfBoundsException is printed.
  • C. A stack trace for ClassCastException is printed.
  • D. None of the above


D.

Note

A NullPointerException can be thrown if the value of list is null.

An ArrayIndexOutOfBoundsException can be thrown if the value of list is an array with fewer than 10 elements.

A ClassCastException can be thrown if list is assigned an object that is not of type Boolean[].

The assignment list = (Boolean[]) new Object() will compile without issue but throws a ClassCastException at runtime.

The first three options are possible.

Option D is the correct answer.




PreviousNext

Related