ProgrammingModerateWipro · Cognizant technical pattern
What is printed?
What is printed?
class K {
K() { this(5); System.out.print("A"); }
K(int x) { System.out.print("B" + x); }
}
// in main:
new K();- A. AB5
- B. A
- C. B5
- D. B5A
Correct answer: D. B5A
this(5) must run first and calls K(int), printing B5; control then returns to the rest of K(), printing A.
More OOP with C++ and Java questions
- Which line fails to compile? java final int[] arr = {1, 2, 3}; arr[0] = 10; // L1 arr = new int[3]; // L2 System.out.print(arr[0]); // L3
- What is printed? java class P { static int c = 0; P() { c++; } } class Q extends P { Q() { c += 10; } } // in main: new P(); new Q(); new…
- What is printed? java class A { int x = 10; A() { x = 20; } { x = 30; } } // in main: System.out.print(new A().x);
- What is printed? java interface I { default String who() { return "I"; } } class P { public String who() { return "P"; } } class C extends…
- What is printed? java StringBuilder a = new StringBuilder("hi"); StringBuilder b = new StringBuilder("hi"); System.out.print(a.equals(b) +…
- What happens when this Java code is compiled and run? java Object o = new ArrayList<String>(); List<String> l = (List<String>) o; String s…