ProgrammingModerateProduct-company placement · C++
What is printed?
What is printed?
struct B { virtual void f() { cout << "B"; } };
struct D : B { void f() override { cout << "D"; } };
int main() { D d; B* p = &d; p->f(); p->B::f(); }- A. DD
- B. DB
- C. BB
- D. BD
Correct answer: B. DB
A qualified call p->B::f() suppresses virtual dispatch and calls exactly B::f; the unqualified p->f() dispatches to D::f.
More OOP with C++ and Java questions
- What is printed? java class Util { static int say(String s) { System.out.print(s); return 1; } } interface I { int X = Util.say("I"); }…
- What is printed? java class P { static int v = 5; static { System.out.print("P"); } } class C extends P { static { System.out.print("C")…
- What is printed? java class C { static { System.out.print("init "); } } // in main: C[] a = new C[2]; Class<?> k = C.class…
- What is printed? java try { try { System.out.print("1"); throw new Exception("a"); } catch (Exception e) { System.out.print("2"); throw…
- What happens when this is compiled (inside a method declared throws IOException)? java List<String> files = List.of("a.txt")…
- Which line fails to compile? java List<?> l = new ArrayList<String>(); l.add(null); // L1 l.add("x"); // L2 Object o = l.isEmpty() ? null…