ProgrammingVery hardWipro Elite · TCS NQT Prime
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct A { virtual void f() { cout << "A"; } };
struct B : A {
private:
void f() override { cout << "B"; }
};
int main() { A* p = new B; p->f(); }- A. A
- B. Compile error: B::f is private
- C. Undefined behaviour
- D. B
Correct answer: D. B
Access is checked against the static type A*, where f is public; dispatch then reaches the private override B::f.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp void f(bool) { cout << "b"; } void f(string) {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp template <class T> void f(T) { cout << "T"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { static int n; static void p() {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp int main() { vector<int> a(3, 1), b{3, 1}…
- What is printed? java class A { A() { init(); } void init() { System.out.print("A"); } } class B extends A { int v = 5; B() {…
- What is printed? java class S { static { System.out.print("s"); } { System.out.print("i"); } S() { System.out.print("c"); } } // main: new…