ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
struct Base { virtual void show() { cout << "Base "; } };
struct Derived : Base { void show() override { cout << "Derived "; } };
int main() {
Derived d;
Base b = d;
Base &r = d;
b.show();
r.show();
}- A. Derived Base
- B. Base Derived
- C. Base Base
- D. Derived Derived
Correct answer: B. Base Derived
Base b = d copies only the Base part (object slicing), so b really is a Base and calls Base::show. A reference keeps the dynamic type, so r.show() dispatches to Derived::show.
More OOP with C++ and Java questions
- What is printed? cpp class Base { public: Base() { hello(); } virtual void hello() { cout << "B"; } }; class Derived : public Base {…
- What is printed? cpp struct A { A() { cout << "A"; } }; struct B : virtual A { B() { cout << "B"; } }; struct C : virtual A { C() { cout…
- How many of these statements about C++ friend functions are correct? (i) A friend function can access the private members of the class…
- Interfaces A and B both declare default void hello(). What happens with class C implements A, B { } ?
- How many of these are correct in Java (8 or later)? (i) An abstract class can have a constructor. (ii) An interface can declare instance…
- What is printed? java // static methods of the main class: static void f() { try { System.out.print("1"); throw new…