ProgrammingModerateWipro NLTH · Accenture
What is printed?
What is printed?
class P { static String w() { return "P"; } }
class Q extends P { static String w() { return "Q"; } }
// in main:
P p = new Q();
System.out.print(p.w());- A. Q
- B. P
- C. PQ
- D. Compile error
Correct answer: B. P
Static methods are hidden, not overridden; the call is bound at compile time using the declared type P.
More OOP with C++ and Java questions
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f(int x = 1) { cout << "B" << x; } }; struct…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void…
- What is printed? java class A { int x = 1; } class B extends A { int x = 2; } // in main: A a = new B(); System.out.print(a.x + " " + ((B)…
- What is printed? java class P { P() { init(); } void init() { } } class C extends P { int v = 5; void init() { System.out.print(v); } } //…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct M { M() { cout << "M"; } }; struct B { B() { cout << "B"; }…
- What is printed? java Integer a = 127, b = 127, c = 128, d = 128; System.out.print((a == b) + " " + (c == d));