ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
String a = "hel" + "lo"; String b = "hello"; System.out.println(a == b);
- A. true
- B. false
- C. Compilation error
- D. Throws an exception
Correct answer: A. true
Both operands of + are compile-time string literal constants, so the compiler folds "hel" + "lo" into the single literal "hello" at compile time. a therefore refers to the same pooled literal as b, and a == b is true.
More OOP with C++ and Java questions
- Which statement about this code is correct? java class P { P(int x) { System.out.print("P" + x + " "); } } class C extends P { C() {…
- What is printed? cpp struct Base { virtual void show() { cout << "Base "; } }; struct Derived : Base { void show() override { cout <<…
- What is printed? java // in main: List<String> ls = new ArrayList<>(); List<Integer> li = new ArrayList<>()…
- Which statement about this class is correct? java class Example<T> { void method(List<String> list) { System.out.println("string-list"); }…
- What is printed? java class Util { static <T> T firstNonNull(T a, T b) { return a != null ? a : b; } } // in main: String s =…
- What is printed? java class Container<T extends Comparable<T>> { T max(T a, T b) { return a.compareTo(b) >= 0 ? a : b; } } // in main…