ProgrammingVery hardHCL · Tech Mahindra pattern
What is printed?
What is printed?
class A { static void who() { System.out.print("A"); } }
class B extends A { static void who() { System.out.print("B"); } }
public class Main {
public static void main(String[] args) {
A a = new B();
a.who();
}
}- A. A
- B. B
- C. Compilation error
- D. AB
Correct answer: A. A
Static methods are not polymorphic — calling a static method through a reference is resolved at compile time using the reference's declared type (A), not the object's runtime type. This is method hiding, not overriding, so A::who runs and prints A.
More OOP with C++ and Java questions
- What is printed? java class K { int a, b; K(int a, int b) { this.a = a; this.b = b; } public int hashCode() { return 1; } public boolean…
- What is printed? java class Counter { private int count = 0; void inc() { count++; } int get() { return count; } } class LoggingCounter…
- What is printed? java String a = "hel" + "lo"; String b = "hello"; System.out.println(a == b);
- Which statement about this code is correct? java class P { P(int x) { System.out.print("P" + x + " "); } } class C extends P { C() {…
- What is printed? cpp struct Base { virtual void show() { cout << "Base "; } }; struct Derived : Base { void show() override { cout <<…
- What is printed? java // in main: List<String> ls = new ArrayList<>(); List<Integer> li = new ArrayList<>()…