Skills101
← All practice quizzes

Quiz: Observer, Strategy and Decorator

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

Three patterns you will meet in every Java codebase: Observer for notifying many listeners, Strategy for swapping an algorithm, and Decorator for adding features by wrapping an object.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    In the Observer pattern, what does the subject know about its observers?

  2. Question 2

    What does this print?

    import java.util.*;
    
    public class Main {
        interface Listener { void on(String e); }
    
        public static void main(String[] args) {
            List<Listener> ls = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            ls.add(e -> sb.append("A").append(e));
            ls.add(e -> sb.append("B").append(e));
            for (Listener l : ls) l.on("1");
            ls.remove(0);
            for (Listener l : ls) l.on("2");
            System.out.println(sb);
        }
    }
    
  3. Question 3

    Collections.sort(list, comparator) is an example of which pattern?

  4. Question 4

    A canteen bill uses if (type == STUDENT) … else if (type == STAFF) …. Which pattern removes this chain?

  5. Question 5

    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());
        }
    }
    
🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →