ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
class Base {
public:
Base(const Base&) = delete;
Base() = default;
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived"; }
};
void print(Base& b) { b.show(); }
int main() {
Derived d;
print(d);
}- A. Derived
- B. Base
- C. Compilation error: Base's deleted copy constructor blocks passing d by reference
- D. Compilation error: Derived must also delete its copy constructor
Correct answer: A. Derived
Passing by reference (Base&) never invokes any copy constructor at all — only pass-by-value would. So the deleted Base copy constructor is irrelevant here; print(d) binds the reference to d directly, and b.show() dispatches virtually to Derived::show, printing "Derived".
More OOP with C++ and Java questions
- What is printed? java class Vehicle { void move() { System.out.print("Vehicle moves "); } } class Car extends Vehicle { @Override void…
- What is printed? java class Animal { String category() { return "Animal"; } } class Mammal extends Animal { @Override String category() {…
- What is printed? java class Shape { double area() { return 0; } @Override public String toString() { return getClass().getSimpleName() +…
- What is printed? cpp class Base { public: Base() { cout << "Base "; } Base& operator=(const Base&) { cout << "Base= "; return this; } }…
- What is printed? cpp struct Base { Base() { cout << "B "; } virtual ~Base() { cout << "~B "; } }; struct Derived : Base { int data…
- What is printed? java class Converter<T, R> { R convert(T input, java.util.function.Function<T, R> fn) { return fn.apply(input); } } // in…