ProgrammingHardIBM · Deloitte pattern
Which statement is correct?
Which statement is correct?
class Event {
protected void log() { System.out.print("Event.log"); }
}
class ClickEvent extends Event {
void log(String tag) { System.out.print("ClickEvent.log " + tag); }
}
public class Main {
public static void main(String[] args) {
ClickEvent c = new ClickEvent();
c.log();
}
}- A. It fails to compile: log() is ambiguous between the two
- B. It prints 'ClickEvent.log '
- C. It prints 'Event.log' — log(String) is an overload, not an override, so the no-arg call resolves to the inherited method
- D. It fails to compile: log(String) must call log() internally
Correct answer: C. It prints 'Event.log' — log(String) is an overload, not an override, so the no-arg call resolves to the inherited method
log(String) in ClickEvent has a different parameter list from Event's log(), so it's an unrelated overload, not an override — Event.log() remains intact and reachable. c.log() with no arguments resolves to the inherited no-arg method, printing 'Event.log'.
More OOP with C++ and Java questions
- What is printed? java class Base { public Base() { System.out.print("Base "); } public Base(Base other) { System.out.print("BaseCopy "); }…
- What is printed? cpp struct Base { virtual void run() { step1(); step2(); } virtual void step1() { cout << "B1 "; } virtual void step2() {…
- What is printed? java class Account { protected double balance; Account(double b) { balance = b; } void transfer(Account to, double amt) {…
- What is printed? cpp struct Base { Base() { cout << "Base "; } Base(int x) { cout << "Base(" << x << ") "; } }; struct Derived : Base {…
- What is printed? java class Node<T> { T value; Node<T> next; Node(T v) { value = v; } } public class Main { static <T> int length(Node<T>…
- What is printed? cpp struct Shape { virtual ~Shape() = default; }; struct Circle : Shape {}; struct Square : Shape {}; int main() { Shape…