Java OCA OCP Practice Question 234

Question

Given:

3. class MyBase { }
4. public class Main extends MyBase {
5.   static Integer i;
6.   public static void main(String[] args) {
7.     int sw = (int)(Math.random() * 3);
8.     switch(sw) {
9.       case 0: {  for(int x = 10; x > 5; x++)
10.                    if(x > 10000000) x = 10;
11.                  break; }
12.       case 1: {  int y = 7 * i;  break;  }
13.       case 2: {  MyBase inf = new Main();
14.                  Main b = (Main)inf;  }
15.     }//from  w  w  w.  j  a v  a2 s  .c o  m
16.   }
17. }

And given that line 7 will assign the value 0, 1, or 2 to sw, which are true?

Choose all that apply.

  • A. Compilation fails
  • B. A ClassCastException might be thrown
  • C. A StackOverflowError might be thrown
  • D. A NullPointerException might be thrown
  • E. An IllegalStateException might be thrown
  • F. The program might hang without ever completing
  • G. The program will always complete without exception


D and F are correct.

Note

Because i was not initialized, case 1 will throw a NullPointerException.

Case 0 will initiate an endless loop, not a stack overflow.

Case 2's downcast will not cause an exception.




PreviousNext

Related