ProgrammingVery hardProduct-company placement · C++
Which line fails to compile?
Which line fails to compile?
struct A { int x = 1; };
struct B : protected A { };
struct C : B { int get() { return x; } };
int main() {
C c;
cout << c.get(); // L1
cout << c.x; // L2
}- A. L2 only
- B. L1 only
- C. Both
- D. Neither
Correct answer: A. L2 only
Protected inheritance makes A's public members protected in B; C inherits them as protected, so C's own member function can use x, but code outside the hierarchy (main) cannot.
More OOP with C++ and Java questions
- std::sort(v.begin(), v.end(), [](int a, int b) { return a <= b; }); What is wrong with this call?
- What is printed? cpp int main() { priorityqueue<int> a; priorityqueue<int, vector<int>, greater<int>> b; for (int x : {3, 1, 4}) {…
- What happens when this is compiled? cpp struct Pt { int x, y; }; int main() { map<Pt, int> m; m[{1, 2}] = 5; }
- What is printed? cpp int main() { vector<int> a(3, 5), b{3, 5}; cout << a.size() << a[0] << b.size() << b[0]; }
- What is printed? cpp void f(int a[10]) { cout << (sizeof(a) == sizeof(int)); } int main() { int arr[10]; cout << sizeof(arr) /…
- What is printed? cpp struct S { int next() { static int c = 0; return ++c; } }; int main() { S a, b; a.next(); b.next(); cout << a.next(); }