ProgrammingModerateMicrosoft/Amazon SDE MCQ · GATE CS
What is printed?
What is printed?
struct B { int x = 5; };
struct D : B { int x = 7; };
int main() {
D d;
B& b = d;
cout << d.x << b.x << d.B::x;
}- A. 777
- B. 757
- C. 755
- D. 775
Correct answer: C. 755
Data members are never virtual. D::x hides B::x for expressions of type D (7), while access through B& uses B's member (5), as does the qualified d.B::x (5).
More OOP with C++ and Java questions
- What is printed? java class P { String f = "P"; static String s() { return "P"; } String i() { return "P"; } } class C extends P { String…
- What is printed? java // static methods of the main class: static void f(long x) { System.out.print("L"); } static void f(Integer x) {…
- What is printed? java String x = "a"; final String y = "a"; System.out.print(("ab" == x + "b") + " "); System.out.print(("ab" == y + "b")…
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { return x; } finally { x = 5; } } static…
- What is printed? java class A { static int x = f(); static int y = 10; static int f() { return y 2; } } // in main: System.out.print(A.x +…
- What is printed? java class P { P() { show(); } void show() { System.out.print("P"); } } class C extends P { int v = 7; String s = "x"…