ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
struct B { int v = 7; };
class  D1 : B { };
struct D2 : B { };
int main() { D1 a; D2 b; cout << b.v << a.v; }
  1. A. 77
  2. B. Compile-time error: a.v is inaccessible
  3. C. 7 followed by a runtime error
  4. D. Compile-time error: b.v is inaccessible

Correct answer: B. Compile-time error: a.v is inaccessible

The default inheritance mode follows the derived class's keyword: class → private, struct → public. So B's members are private inside D1 and a.v cannot be used from main.

More OOP with C++ and Java questions

Exams that ask this topic