Java OCA OCP Practice Question 884

Question

What is the result of the following code?

2: String s1 = "java"; 
3: StringBuilder s2 = new StringBuilder("java"); 
4: if (s1 == s2) 
5:  System.out.print("1"); 
6: if (s1.equals(s2)) 
7:  System.out.print("2"); 
  • A. 1
  • B. 2
  • C. 12
  • D. No output is printed.
  • E. An exception is thrown.
  • F. The code does not compile.


F.

Note

Line 4 does not compile.

Java does not allow you to compare String and StringBuilder using ==.




PreviousNext

Related