Question 1
What is the output of the following program? ```java public class Main { public static void main(String[] args) { String x = "java7"; String y = "java" + "7"; String z = new String("java7"); System.out.println((x == y) + " " + (x == z) + " " + x.equals(z)); } } ```
A. false false true
This treats "java" + "7" as a runtime concatenation producing a distinct object. It is a compile-time constant folded to "java7" and interned, so x == y is true, not false.
B. true false trueCorrect answer
"java" + "7" is a constant expression folded and interned to the same pooled object as x (x == y true); new String(...) is a fresh heap object (x == z false); its contents match, so equals is true (JLS 7 §3.10.5).
C. true true true
new String("java7") always allocates a distinct object on the heap, so x == z is false even though the contents are equal; only the pooled literal comparison is true.
D. false false false
The folded literal is pooled, so x == y is true, and equals() compares contents, which match, so the last value is true; not all three are false.
Explanation
"java" + "7" is a compile-time constant expression, folded to "java7" and interned in the string pool — the same pooled object x refers to, so x == y is true. `new String(...)` always creates a fresh object on the heap, so x == z is false, but the contents match, so equals is true.