ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
String a = "Hi";
String b = new String("Hi");
String c = b.intern();
StringBuilder d = new StringBuilder("Hi");
System.out.print((a == b) + " " + (a == c) + " " + a.equals(d) + " " + a.contentEquals(d));- A. false true true true
- B. false true false true
- C. true true false true
- D. false false false true
Correct answer: B. false true false true
new String creates a distinct object (false), and intern() returns the pooled literal (true). String.equals is true only for another String, so comparing with a StringBuilder gives false; contentEquals compares characters (true).
More OOP with C++ and Java questions
- What is printed? java abstract class Shape { Shape() { System.out.print(area() + " "); } abstract double area(); } class Sq extends Shape…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(5, 6, 7)); Iterator<Integer> it = l.iterator(); while (it.hasNext()) if…
- What does this Java program print? class A { String n = "A"; String get() { return n; } } class B extends A { String n = "B"; String get()…
- What does this Java program print? class P { P() { init(); } void init() { System.out.print("P "); } } class C extends P { int x = 5; C()…
- What does this Java code print? Integer a = 127, b = 127, c = 128, d = 128; System.out.println((a == b) + " " + (c == d) + " " +…
- What does this Java code print? class A { String n = "A"; String get() { return n; } } class B extends A { String n = "B"; String get() {…