Java OCA OCP Practice Question 2803

Question

Given:

3. public class Main {  
4.   Long[] v = {new Long(3L), new Long(4L), new Long(5L)};  
5.   static int count = 0;   
6.   public static void main(String[] args) {  
7.     new Main().go();  
8.     System.out.println(count);  
9.   }  //from w  w  w  .ja v  a  2  s  .co m
10.   void go() {  
11.     for(short x = 0; x < 5; x++) {  
12.       if(x == 2) return;  
13.       for(long ell: v) {  
14.         count++;  
15.         if(ell == 4) break;  
16.       } 
17.     } 
18.   } 
19. } 

What is the result? (Choose all that apply.)

  • A. 2
  • B. 4
  • C. 8
  • D. 12
  • E. Compilation fails due to an error on line 13.
  • F. Compilation fails due to an error on line 16.


B is correct.

Note

In for loops (any flavor), a return immediately returns execution to the calling method (in this case, main()).

A break moves execution to the first statement after the for loop (in this case, the inner for loop).

Line 13 is a so-called for-each loop that's using auto- unboxing.

Don't be surprised to see this kind of thing on the real exam where the number of lines of code is limited by the test engine.




PreviousNext

Related