Java OCA OCP Practice Question 2451

Question

Given:

2. public class Main {  
3.   public static void main(String[] args) {  
4.     String result = "";  
5.     int x = 7, y = 8;  
6.     if(x == 3) { result += "1"; }  
7.     else if (x > 9) { result += "2"; }  
8.     else if (y < 9) { result += "3"; }  
9.     else if (x == 7) { result += "4"; }  
10.     else { result += "5"; }  
11.     System.out.println(result);  
12.   }  //from w w w  .  j  av a  2 s . c om
13. } 

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

  • A. 3
  • B. 34
  • C. 35
  • D. 345
  • E. Compilation fails due to an error on line 5.
  • F. Compilation fails due to errors on lines 8 and 9.
  • G. Compilation fails due to errors on lines 7, 8, and 9.


A is correct.

Note

It's legal to declare several variables on a single line, and it's legal to have multiple else-if statements.

Once an else-if succeeds, the remaining else-if and else statements in the block are ignored.




PreviousNext

Related