ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
String x = "a";
final String y = "a";
System.out.print(("ab" == x + "b") + " ");
System.out.print(("ab" == y + "b") + " ");
System.out.print("ab" == (x + "b").intern());- A. false false true
- B. true true true
- C. false true false
- D. false true true
Correct answer: D. false true true
x is not a constant, so x + "b" builds a new String at run time (false). y is a final variable initialised with a constant, so y + "b" is a compile-time constant and is interned like the literal (true). intern() returns the pooled "ab" (true).
More OOP with C++ and Java questions
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { return x; } finally { x = 5; } } static…
- What is printed? java class A { static int x = f(); static int y = 10; static int f() { return y 2; } } // in main: System.out.print(A.x +…
- What is printed? java class P { P() { show(); } void show() { System.out.print("P"); } } class C extends P { int v = 7; String s = "x"…
- Which statement about this code is correct? java try { new java.io.FileReader("a.txt").close(); } catch (java.io.IOException | Exception…
- What is printed? java class A { void m(A a) { System.out.print("AA"); } } class B extends A { void m(A a) { System.out.print("BA"); } void…
- Which lines fail to compile? java List<Integer> ints = new ArrayList<>(); List<Number> a = ints; // line 1 List<? extends Number> b =…