Java OCA OCP Practice Question 3038

Question

Given:

3. public class Main {  
4.   public static void main(String[] args) {  
5.     String r = "0";  
6.     int x = 3, y = 4;  
7.     boolean test = false;  
8.     if((x > 2) || (test = true))  
9.     if((y > 5) || (++x == 4))  
10.     if((test == true) || (++y == 4))  
11.     r += "1";  
12.     else if(y == 5) r += "2";  
13.     else r += "3";  
14.     else r += "4";  
15.     // else r += "5";  
16.     System.out.println(r);  //from   w  w  w  .  j a v  a 2s .co  m
17. } } 

And given that, if necessary you can add line 15 to make the code compile, what is the result?

(Choose all that apply.)

  • A. At line 10, test will equal true
  • B. At line 10, test will equal false
  • C. The output will be 02
  • D. The output will be 03
  • E. The output will be 023
  • F. The code will compile as is.
  • G. The code will only compile if line 15 is added.


B, C, and F are correct.

Note

Line 8 doesn't change the value of test because of the short circuit operator.

The first two ifs are true, but the third is false (although "y" is incremented), so the "else if" is executed.

The code compiles as is because not all if statements need to have matching else statements.




PreviousNext

Related