Java OCA OCP Practice Question 2919

Question

Given:

42. void go() {  
43.   int cows = 0;  
44.   int[] arr = {1,2,3};  
45.   for(int i = 0; i < 4; i++)  
46.     switch(arr[i]) {  
47.        case 2: cows++;  
48.        case 1: cows += 10;  
49.        case 0: go();  
50.     }  /* w ww.  j  ava  2 s  .  co  m*/
51.   System.out.println(cows);  
52. } 

What is the result?

  • A. 11
  • B. 21
  • C. 22
  • D. Compilation fails.
  • E. A StackOverflowError is thrown at runtime.
  • F. An ArrayIndexOutOfBoundsException is thrown at runtime.


E is correct.

Note

Because of switch fall-through logic, the go() method is called recursively until the stack "explodes" (that's the vulgate).

The for loop would fail with option F if it ever got to run to completion.




PreviousNext

Related