🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: SOLID in Java: all 10 questions

Quiz: SOLID in Java · Question 7 of 10

What does this print? (code)

What does this print?

interface Notifier { String send(String m); }

class Sms implements Notifier {
    public String send(String m) { return "SMS:" + m; }
}

class Email implements Notifier {
    public String send(String m) { return "MAIL:" + m; }
}

class Service {
    private final Notifier n;
    Service(Notifier n) { this.n = n; }
    String run() { return n.send("hi"); }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Service(new Email()).run() + " " + new Service(new Sms()).run());
    }
}
  1. A.SMS:hi MAIL:hi
  2. B.MAIL:hi SMS:hi
  3. C.MAIL:hi MAIL:hi
  4. D.hi hi
Answer & explanation

Correct answer: MAIL:hi SMS:hi

The same Service class behaves differently depending on which Notifier is injected.

Can you get the rest right without peeking?

Take the timed Quiz: SOLID in Java quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →