ProgrammingVery hardWipro Elite · TCS NQT Prime
What is printed?
What is printed?
class A {
void f() { System.out.print("A"); g(); }
void g() { System.out.print("a"); }
}
class B extends A {
void f() { super.f(); System.out.print("B"); }
void g() { System.out.print("b"); }
}
// main: new B().f();- A. AaB
- B. ABa
- C. AabB
- D. AbB
Correct answer: D. AbB
super.f() runs A.f, but its call g() is virtual on the B object, so B.g prints b; then B.f prints B.
More OOP with C++ and Java questions
- What is printed? java int x = Integer.MAXVALUE; long y = x + 1; long z = x + 1L; System.out.print(y + " " + z);
- What is printed? java class A { A() { this(1); System.out.print("0"); } A(int x) { System.out.print("1"); } } class B extends A { B() {…
- Which OOP principle bundles data with the methods that act on it and hides the internal state behind an interface?
- In Java, which keyword makes one class inherit from another class?
- What is the return type of a C++ constructor?
- Every Java class that declares no superclass implicitly extends: