🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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));
    }
}
  1. A.Paid 250
  2. B.Compile error
  3. C.UPI Paid 250
  4. 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 →