ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class P {
private void f() { System.out.print("P"); }
void g() { f(); }
}
class C extends P {
void f() { System.out.print("C"); }
}
// in main:
new C().g();- A. C
- B. Compilation error
- C. P
- D. PC
Correct answer: C. P
Private methods are not inherited and cannot be overridden; C.f is an unrelated new method. Inside P, the call f() is bound to P's private f at compile time, so P prints.
More OOP with C++ and Java questions
- What is printed? java class Animal { String speak() { return "animal"; } } class Dog extends Animal { String speak() { return "dog"; } }…
- What is printed? java class P { int id; P(int id) { this.id = id; } @Override public boolean equals(Object o) { return o instanceof P &&…
- What is printed? java System.out.print(5.0 / 0 + " "); System.out.print(0.0 / 0 + " "); try { System.out.print(5 / 0); } catch…
- What is printed? cpp struct R { int v = 0; R& inc() { v++; return this; } }; struct V { int v = 0; V inc() { v++; return this; } }; int…
- What is printed? cpp int a = 5; int &r = a; r = 10; int b = 20; r = b; b = 30; cout << a << " " << r;
- What is printed? java class R implements AutoCloseable { String n; R(String n) { this.n = n; System.out.print("o" + n + " "); } public…