ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
// in main:
String s1 = "Java";
String s2 = new StringBuilder("Java").toString();
System.out.println((s1 == s2) + " " + s1.equals(s2) + " " + (s1 == s2.intern()));- A. false true true
- B. true true true
- C. false false true
- D. false true false
Correct answer: A. false true true
s2 is built via StringBuilder.toString(), which always allocates a fresh String object outside the pool, so s1 == s2 is false. Their contents are identical so equals() is true. s2.intern() returns the pooled reference equal to the literal s1, so s1 == s2.intern() is true.
More OOP with C++ and Java questions
- What is printed? java // in main: String s = "hello"; s.concat(" world"); System.out.println(s); String t = s.concat(" world")…
- What is printed? java class Node { int val; Node(int v) { val = v; } @Override public int hashCode() { return 42; } @Override public…
- What is printed? java // in main: String a = "Hello".toLowerCase(); String b = "hello"; System.out.println((a == b) + " " + a.equals(b));
- Which overridden equals() below is actually BROKEN with respect to the symmetry requirement of the equals contract? java class A { int x…
- What is printed? java class Version { int major, minor; Version(int ma, int mi) { major = ma; minor = mi; } @Override public boolean…
- What is printed? java public class Main { static int f() { int x = 10; try { return x; } finally { x = 20; } } public static void…