ProgrammingVery hardCognizant GenC · HCL
What is printed? (C++, assume <iostream> and using namespace std)
What is printed? (C++, assume <iostream> and using namespace std)
class B {
public:
void call() { f(); }
private:
virtual void f() { cout << "B"; }
};
class D : public B {
void f() override { cout << "D"; }
};
int main() { D d; B& r = d; r.call(); }- A. B
- B. BD
- C. Compile error because f is private
- D. D
Correct answer: D. D
Access control is checked at the call site (inside B, where f is accessible), while the virtual dispatch still selects D::f.
More OOP with C++ and Java questions
- What is printed? java static void f(long x) { System.out.print("long"); } static void f(Integer x) { System.out.print("Integer"); } static…
- What is printed? java Object o = true ? 1 : 2.0; System.out.print(o);
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct S { void f() { cout << "n"; } void f() const { cout << "c"; }…
- What does this Java method do when called? java static int f() { try { throw new RuntimeException("a"); } finally { return 5; } }
- What is printed? java String a = "ja" + "va"; String b = "java"; String c = "ja"; String d = c + "va"; System.out.print((a == b) + "," +…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct A { A() { cout << "A"; } }; struct B : virtual A { }; struct…