← Quiz: Polymorphism: all 10 questions
Quiz: Polymorphism · Question 7 of 10
What does this print? (code)
What does this print?
class Payment {
String pay(int amt) { return "Paid " + amt; }
}
class Upi extends Payment {
@Override
String pay(int amt) { return "UPI " + amt; }
}
public class Main {
public static void main(String[] args) {
Payment p = new Upi();
System.out.println(p.pay(250));
}
}
- A.Paid 250
- B.Compile error
- C.UPI Paid 250
- D.UPI 250
Answer & explanation
Correct answer: UPI 250
The variable type is Payment, but the object is Upi, so Upi's pay() runs at runtime.
Can you get the rest right without peeking?
Take the timed Quiz: Polymorphism quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →