Java OCA OCP Practice Question 588

Question

What is the output of the following code snippet?

3: int x = 0; 
4: String s = null; 
5: if(x == s) System.out.println("Success"); 
6: else System.out.println("Failure"); 
  • A. Success
  • B. Failure
  • C. The code will not compile because of line 4.
  • D. The code will not compile because of line 5.


D.

Note

The variable x is an int and s is a reference to a String object.

The two data types are incomparable because neither variable can be converted to the other variable's type.

The compiler error occurs on line 5 when the comparison is attempted, so the answer is option D.




PreviousNext

Related