ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class P {
static void sm() { System.out.print("P"); }
void im() { System.out.print("P"); }
}
class C extends P {
static void sm() { System.out.print("C"); }
void im() { System.out.print("C"); }
}
// in main:
P p = new C();
p.sm();
p.im();- A. PC
- B. CC
- C. PP
- D. CP
Correct answer: A. PC
Static methods are not overridden, only hidden: p.sm() is bound at compile time by the declared type P. Instance methods are dispatched on the actual object, a C. So PC.
More OOP with C++ and Java questions
- What is printed? java class P { int x = 10; int getX() { return x; } } class C extends P { int x = 20; int getX() { return x; } } // in…
- What is printed (default JVM settings)? java Integer x = 127, y = 127; Integer p = 128, q = 128; System.out.print((x == y) + " " + (p ==…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("Object"); } static void f(String s)…
- What is printed? java // static methods of the main class: static void f(long x) { System.out.print("long"); } static void f(Integer x) {…
- What is printed? java class P { P() { show(); } void show() { System.out.print("P "); } } class C extends P { int x = 10; void show() {…
- What is printed? java class T { static { System.out.print("S"); } { System.out.print("I"); } T() { System.out.print("C"); } } // in main…