← 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());
}
}
- A.hi
- B.hi**
- C.hi
- 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 →