ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
class P {
static { System.out.print("1"); }
{ System.out.print("2"); }
P() { System.out.print("3"); }
}
class C extends P {
static { System.out.print("4"); }
{ System.out.print("5"); }
C() { System.out.print("6"); }
}
// in main:
new C();
new C();- A. 1423562356
- B. 1234562356
- C. 142356142356
- D. 1425362536
Correct answer: A. 1423562356
Class initialisation runs once, parent first: 1, 4. Each new C() then runs P's instance initialiser and constructor (2, 3) before C's (5, 6). The second object repeats only the instance part: 2356.
More OOP with C++ and Java questions
- Which line fails to compile? java int base = 10; int step = 1; step = 2; Function<Integer, Integer> f = x -> x + base; // line 1…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(int... a) {…
- What is printed? java char c = 'a'; c += 1; System.out.print(('a' + 1) + "" + (char) ('a' + 1) + c);
- What is printed? java Object o = null; String s = (String) o; System.out.print(s + (o instanceof Object) + (s == null));
- What is printed? java List<String> l = new ArrayList<>(List.of("pear", "fig", "apple", "kiwi"))…
- Why does this class fail to compile? java class Util { static void f(List<String> l) { } static void f(List<Integer> l) { } }