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

Quiz: Observer, Strategy and Decorator · Question 5 of 10

What does this print? (code)

What does this print?

interface Text { String show(); }

class Plain implements Text {
    public String show() { return "hi"; }
}

class Stars implements Text {
    private final Text inner;
    Stars(Text inner) { this.inner = inner; }
    public String show() { return "*" + inner.show() + "*"; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Stars(new Stars(new Plain())).show());
    }
}
  1. A.hi
  2. B.hi**
  3. C.hi
  4. D.Compilation error
Answer & explanation

Correct answer: hi

Each Stars wrapper adds one star on each side of whatever it wraps.

Can you get the rest right without peeking?

Take the timed Quiz: Observer, Strategy and Decorator quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →