ProgrammingVery hardProduct-company placement · C++
Which line fails to compile?
Which line fails to compile?
class B { protected: int x = 1; };
class D : public B {
public:
int f(D& d) { return d.x; } // L1
int g(B& b) { return b.x; } // L2
};- A. L2 only
- B. L1 only
- C. Both L1 and L2
- D. Neither
Correct answer: A. L2 only
Inside D, a protected member of B may be accessed only through an object of type D (or derived from D). b might be some other subclass of B, so b.x is not allowed.
More OOP with C++ and Java questions
- What is printed? cpp struct B { int v = 7; }; class D1 : B { }; struct D2 : B { }; int main() { D1 a; D2 b; cout << b.v << a.v; }
- 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&…