ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
class P {
private void f() { System.out.print("P"); }
void call() { f(); }
}
class C extends P {
void f() { System.out.print("C"); }
}
// in main:
new C().call();- A. C
- B. P
- C. Compile error: f() has weaker access
- D. PC
Correct answer: B. P
A private method is not inherited and cannot be overridden; C.f is a new, unrelated method. Inside P, f() binds to P's private f, so P is printed.
More OOP with C++ and Java questions
- What is printed? java class P { static String s() { return "S"; } } // in main: P p = null; System.out.print(p.s());
- Which line fails to compile? java interface I { static String s() { return "I"; } default String d() { return "D"; } } class C implements…
- What is printed? java int[] a = {1, 2}; int[] b = {1, 2}; System.out.print(a.equals(b) + " " + java.util.Arrays.equals(a, b) + " " +…
- What is printed? java outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == i) continue outer; if (i + j == 3) break…
- What is printed? java enum Lvl { LOW, MID, HIGH } // in main: Lvl a = Lvl.valueOf("MID"); System.out.print(a.ordinal() + " " +…
- Which line fails to compile? java interface Cfg { int MAX = 10; } class Impl implements Cfg { int a = MAX; // line 1 void set() { MAX =…