ProgrammingHardProduct-company placement · C++
Which lines fail to compile?
Which lines fail to compile?
struct B { virtual void f() final { } virtual void g() { } };
struct D : B {
void f() { } // L1
void g() final { } // L2
};
struct E final : D { }; // L3
struct F : E { }; // L4- A. L1 only
- B. L1 and L4
- C. L1, L2 and L4
- D. L4 only
Correct answer: B. L1 and L4
A final virtual function cannot be overridden (L1) and a final class cannot be derived from (L4). Marking an override as final (L2) and declaring a final class (L3) are fine.
More OOP with C++ and Java questions
- What is printed? cpp struct B { virtual ~B() { } }; struct D : B { }; struct E : B { }; int main() { B p = new D; cout <<…
- What is printed? cpp struct A { }; struct B : A { }; struct P { virtual ~P() { } }; struct Q : P { }; int main() { B b; Q q; A& ra = b; P&…
- What is printed? cpp struct A { A(int x) { cout << x; } A() { cout << 0; } }; struct B : virtual A { B() : A(1) { cout << "B"; } }; struct…
- What is printed? cpp void f(long) { cout << "L"; } void f(double) { cout << "D"; } int main() { f(5L); f(2.5f); f(5); }
- What is printed? cpp struct B { int b; B(int x) : b(x) { } }; struct D : B { int d; D(int x, int y) : B(x), d(y) { } }; int main() { D…
- How many of these statements about C++ dynamic memory are correct? (i) Memory obtained with new[] must be released with delete[]. (ii)…