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; }- A. 77
- B. Compile-time error: a.v is inaccessible
- C. 7 followed by a runtime error
- 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
- What is printed? cpp template <typename T> void f(T) { cout << "T"; } void f(int) { cout << "I"; } int main() { f(1); f(1.0); f('a')…
- What is printed? cpp template <typename T> struct Is { static const int v = 0; }; template <> struct Is<int> { static const int v = 1; }…
- What is printed? cpp struct L { L() { cout << "L"; } }; void f() { static L l; cout << "f"; } int main() { f(); f(); f(); }
- What is printed? cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int main() { T(); cout << "1"; const T& r = T(); cout <<…
- Which lines fail to compile? cpp struct U { U() { } U(const U&) = delete; }; void take(U u) { } int main() { U a; // L1 U b = a; // L2 U&…
- Which line fails to compile? cpp struct P { int v = 1; int get() { return v; } int cget() const { return v; } }; int main() { const P p{}…