ProgrammingModerateProduct-company placement · Java
Collections.sort(list, comparator) lets the caller pass the comparison algorithm as an object, and the same sort code works with any comparator. Which design pattern does the Comparator represent?
- A. Template Method
- B. Observer
- C. Strategy
- D. Visitor
Correct answer: C. Strategy
Strategy encapsulates an interchangeable algorithm in an object chosen by the client. Template Method would fix the steps in a base class and let SUBCLASSES override them.
More OOP with C++ and Java questions
- Square extends Rectangle and overrides setWidth and setHeight so that both always change together. Client code written for Rectangle does…
- A Department has Professors who continue to exist if the department is closed, while a House is made of Rooms that cannot exist without…
- What is printed? java // static methods of the main class: static void p(Object o) { System.out.print("O"); } static void p(String s) {…
- What is printed? java Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2)); for (Map.Entry<String, Integer> e : m.entrySet())…
- What is printed? java Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2, "c", 3)); m.keySet().remove("b")…
- Which statement about the protected modifier is true in Java but NOT in C++?