ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
class P {
P() { show(); }
void show() { System.out.print("P"); }
}
class C extends P {
int v = 7;
String s = "x";
C() { show(); }
void show() { System.out.print(v + s); }
}
// in main:
new C();- A. P7x
- B. 7x7x
- C. 0null7x
- D. Pnull7x
Correct answer: C. 0null7x
P's constructor runs before C's field initialisers, yet show() dispatches to C, which sees the defaults 0 and null. After super() returns, v = 7 and s = "x" are set, and C's constructor prints 7x.
More OOP with C++ and Java questions
- 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 =…
- What happens? java boolean flag = true; Integer none = null; Integer r1 = flag ? null : 0; System.out.print(r1); Integer r2 = flag ? none…
- What is printed? java System.out.print(Math.abs(Integer.MINVALUE) + " "); System.out.print(Integer.MAXVALUE + 1 == Integer.MINVALUE);
- What is printed? java System.out.print((-7 / 2 == -3) + " " + (-7 % 2 == 1) + " " + (Math.floorMod(-7, 2) == 1) + " " + (7 % -2 == 1));