ProgrammingHardWipro Elite · TCS NQT Prime
What is printed?
What is printed?
String a = "hi";
String b = new String("hi");
String c = b.intern();
System.out.print((a == b) + " " + (a == c) + " " + a.equals(b));- A. true true true
- B. false false true
- C. true false true
- D. false true true
Correct answer: D. false true true
new String creates a separate heap object; intern() returns the pooled instance, which is the same object as the literal a.
More OOP with C++ and Java questions
- What is printed? java static void f(Integer i) { System.out.print("I"); } static void f(long l) { System.out.print("L"); } static void…
- What is printed? java Integer a = 127, b = 127, c = 128, d = 128; System.out.print((a == b) + " " + (c == d) + " " + c.equals(d));
- What happens when this runs? java Integer n = null; boolean flag = false; Integer r = flag ? 1 : n; System.out.print(r);
- What is printed? java byte b = 10; b += 120; System.out.print(b + " " + (b >>> 1));
- What is printed? java try { try { throw new Exception("1"); } finally { throw new RuntimeException("2"); } } catch (Exception e) {…
- What is printed? java class R implements AutoCloseable { String n; R(String n) { this.n = n; System.out.print("o" + n); } public void…