ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class A {
void show() { System.out.print("A"); }
}
class B extends A {
void show() { super.show(); System.out.print("B"); }
}
class C extends B {
void show() { System.out.print("C"); super.show(); }
}
// in main:
A obj = new C();
obj.show();- A. C
- B. CBA
- C. ABC
- D. CAB
Correct answer: D. CAB
The object is a C, so C.show runs: it prints C, then calls B.show, which calls A.show (A) before printing B. Result CAB.
More OOP with C++ and Java questions
- What is printed? cpp class Base { public: void show() { cout << "B1"; } virtual void vshow() { cout << "B2"; } }; class Derived : public…
- In C++, Base has a non-virtual destructor and Derived : public Base owns a heap buffer freed in ~Derived. What happens on: Base p = new…
- What is printed? java String a = "hi"; String b = "hi"; String c = new String("hi"); System.out.print((a == b) + " " + (a == c) + " " +…
- What is printed? java System.out.print('a' + 1); System.out.print(" "); System.out.print("" + 'a' + 1);
- What is printed? cpp void f(int) { cout << "int "; } void f(double) { cout << "double "; } int main() { f('a'); f(2.5f); }
- What is printed? cpp struct A { A() { cout << "C"; } A(const A &) { cout << "K"; } ~A() { cout << "D"; } }; void f(A a) { } int main() { A…