ProgrammingHardMicrosoft/Google SDE MCQ · GATE CS

Which line fails to compile?

Which line fails to compile?
struct B { void hi() { } };
class D1 : B { };          // note: class
struct D2 : B { };         // note: struct
int main() {
    D2 x; x.hi();          // line 1
    B* p = &x;             // line 2
    D1 y; y.hi();          // line 3
}
  1. A. Line 3
  2. B. Line 1
  3. C. Line 2
  4. D. Lines 2 and 3

Correct answer: A. Line 3

A class defaults to PRIVATE inheritance, so B's public hi() is private within D1 and y.hi() is rejected. A struct defaults to public inheritance, so lines 1 and 2 are fine.

More OOP with C++ and Java questions

Exams that ask this topic