ProgrammingModerateProduct-company placement · C++

Which line fails to compile?

Which line fails to compile?
struct B { virtual B* clone() { return new B; } virtual int id() { return 1; } };
struct D : B {
    D* clone() override { return new D; }   // L1
    long id() override { return 2; }        // L2
};
  1. A. L1 only
  2. B. Both
  3. C. L2 only
  4. D. Neither

Correct answer: C. L2 only

Return types may be covariant only for pointers or references to classes (B* → D*). Changing int to long is not allowed for an overriding function.

More OOP with C++ and Java questions

Exams that ask this topic