ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
// in main:
String a = "hello";
String b = "hello";
String c = new String("hello");
String d = c.intern();
System.out.println((a == b) + " " + (a == c) + " " + (a == d));- A. true false true
- B. true true true
- C. false false false
- D. true false false
Correct answer: A. true false true
String literals are pooled: a and b refer to the same interned object, so a == b is true. `new String(...)` always creates a distinct heap object outside the pool, so a == c is false. intern() returns the canonical pooled reference, which is the same object as the literal, so a == d is true.
More OOP with C++ and Java questions
- What is printed? java // in main: Integer a = 100; Integer b = 100; Integer c = 200; Integer d = 200; System.out.println((a == b) + " " +…
- What is printed? java // in main: String s1 = "Java"; String s2 = new StringBuilder("Java").toString(); System.out.println((s1 == s2) + "…
- 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…