Java OCA OCP Practice Question 1422

Question

What is the output of the following?

string s1 = "asdf"; 
string s2 = new String("asdf"); 

boolean test1 = s1 == s2; 
boolean test2 = s1.equals(s2); 
System.out.println(test1 + " " + test2); 
  • A. false false
  • B. false true
  • C. true false
  • D. true true
  • E. The code does not compile.
  • F. The code compiles but throws an exception at runtime.


E.

Note

In Java, String is a class and not a primitive.

This means it needs to begin with an uppercase letter in the declaration.

The code does not compile, making Option E correct.

If this was fixed, the answer would be Option B.




PreviousNext

Related