Java OCA OCP Practice Question 3010

Question

Given this code in a method:

3.     int y, count = 0;  
4.     for(int x = 3; x < 6; x++) {  
5.       try {  /*  www  .ja va2s.c o  m*/
6.         switch(x) {  
7.            case 3: count++;  
8.            case 4: count++;  
9.            case 7: count++;  
10.           case 9: { y = 7 / (x - 4); count += 10; }  
11.         }  
12.       } catch (Exception ex) { count++; }  
13.     }  
14.     System.out.println(count); 

What is the result?

  • A. 2
  • B. 15
  • C. 16
  • D. 25
  • E. 26
  • F. Compilation fails.
  • G. An exception is thrown with no other output.


C is correct.

Note

Remember that once a matching case is found, its code and all subsequent cases' code will be executed.

When "x" equals 4 and the code for case 9 is executed, an exception will be thrown, but it's caught correctly and the code will continue to run.




PreviousNext

Related