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
}- A. Line 3
- B. Line 1
- C. Line 2
- 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
- Which line fails to compile? cpp struct M { int v; M(int x) : v(x) { } M operator+(const M& o) const { return M(v + o.v); } }; int main()…
- What is printed? cpp int main() { vector<int> a(3, 5); vector<int> b{3, 5}; vector<int> c; c.reserve(10); cout << a.size() << b.size() <<…
- Which line fails to compile? cpp struct S { int v = 1; static int count; static int get() { return v; } // line 1 static int all() {…
- What is printed? cpp struct B { virtual void f() { cout << "B"; } void g() { f(); } }; struct D : B { void f() override { cout << "D"; }…
- Which statement about this class is correct? cpp struct Buf { int p; Buf() : p(new int[8]) { } ~Buf() { delete[] p; } }; void use() { Buf…
- What is printed? cpp struct A { int x; A(int v) : x(v) { } }; struct B : A { int y; B(int v) : A(v 2), y(x + 1) { } }; int main() { B…