ProgrammingHardProduct-company placement · GATE CS
Which lines fail to compile?
Which lines fail to compile?
struct B { void f() { } };
class D1 : B { };
struct D2 : B { };
class D3 : public B { };
int main() {
D1 a; D2 b; D3 c;
a.f(); // L1
b.f(); // L2
c.f(); // L3
B* p = &a; // L4
}- A. L1 only
- B. L1, L2 and L4
- C. None of them
- D. L1 and L4
Correct answer: D. L1 and L4
Inheritance defaults to private for class and public for struct. D1 inherits B privately, so outside code can neither call a.f() nor convert D1* to the inaccessible base B*. D2 and D3 inherit publicly.
More OOP with C++ and Java questions
- What is printed? cpp template <class T> void f(T) { cout << "T"; } template <class T> void f(T) { cout << "P"; } void f(int) { cout <<…
- What is printed? cpp struct S { void f() { cout << "N"; } void f() const { cout << "C"; } }; int main() { S a; const S b{}; const S& r =…
- What is printed? cpp int a = 1, b = 2; int& r = a; r = b; r = 5; cout << a << b;
- What is printed? cpp struct E { virtual const char n() const { return "E"; } virtual ~E() { } }; struct F : E { const char n() const…
- What is printed? (E and F are as shown.) cpp struct E { virtual ~E() { } }; struct F : E { }; int main() { try { try { throw F(); } catch…
- What happens? java class T { static String s() { return "S"; } String i() { return "I"; } } // in main: T t = null…