Java OCA OCP Practice Question 2609

Question

Given:

1. public class Main {  
2.   public static void main(String[] args) {  
3.     int i = 0;  
4.     short s = 0;  
5.     for(int j = 0, k = 0; j < 3; j++) ;  
6.     for(int j = 0; j < 3; counter(j)) ;  
7.     for(int j = 0, int k = 0; j < 3; j++) ;  
8.     for(; i < 5; counter(5), i++) ;  
9.     for(i = 0; i < 3; i++, System.out.print("howdy ")) ;  
10.   }  //from w  w  w.j ava  2  s .c  o  m
11.   static int counter(int y) { return y + 1; }  
12. } 

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

  • A. howdy howdy howdy
  • B. The code runs in an endless loop.
  • C. Compilation fails due to an error on line 5.
  • D. Compilation fails due to an error on line 6.
  • E. Compilation fails due to an error on line 7.
  • F. Compilation fails due to an error on line 8.
  • G. Compilation fails due to an error on line 9.


E is correct.

Note

When defining multiple variables in a for loop, line 5 uses the correct syntax.

A is incorrect, although if other issues were fixed in the code, line 9 would create that output.

B is incorrect, although if line 7 was fixed, line 6 would cause an endless loop because the counter() method is not incrementing the for loop's "j" variable.

C, D, F, and G are incorrect because the rest of the code's syntax is correct.




PreviousNext

Related