ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
public class Main {
    public static void main(String[] args) {
        String s1 = "java";
        String s2 = "java".intern();
        String s3 = "ja" + "va";
        System.out.print((s1 == s2) + " " + (s1 == s3));
    }
}
  1. A. false true
  2. B. true false
  3. C. true true
  4. D. false false

Correct answer: C. true true

s1 is pooled. s2 interns the same literal, so it returns the identical pooled reference: s1 == s2 is true. "ja" + "va" is a compile-time constant expression (both operands are literals), so the compiler folds it into "java" at compile time and interns it too: s1 == s3 is also true.

More OOP with C++ and Java questions

Exams that ask this topic