Design Patterns MCQs

  1. Which of the following is true about Singleton pattern?
    a) Ensures only one instance exists
    b) Provides global access point
    c) Can be implemented with enum in Java
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  2. In Java, which is the most thread safe way to implement Singleton?
    a) Double checked locking
    b) Lazy initialization without synchronization
    c) Enum Singleton
    d) Static block initialization
    View Answer

    Correct answer: C — Enum Singleton

  3. What will be printed?
    1 class EduinqSingleton {
    2 private static EduinqSingleton instance = new EduinqSingleton();
    3 private EduinqSingleton(){}
    4 public static EduinqSingleton getInstance(){ return instance; }
    5 }
    6 public class Test {
    7 public static void main(String[] args) {
    8 System.out.println(EduinqSingleton.getInstance()!=null);
    9 }
    10 }
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true

  4. Which of the following is true about Factory pattern?
    a) Creates objects without exposing instantiation logic
    b) Promotes loose coupling
    c) Returns objects via interface/abstract class
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  5. Which of the following is NOT a benefit of Factory pattern?
    a) Encapsulation of object creation
    b) Reduces code duplication
    c) Makes code tightly coupled
    d) Promotes extensibility
    View Answer

    Correct answer: C — Makes code tightly coupled

  6. What will be printed?
    1 interface EduinqProduct { String getName(); }
    2 class EduinqFactory {
    3 static EduinqProduct create(){ return () -> "Eduinq Product"; }
    4 }
    5 public class Test {
    6 public static void main(String[] args) {
    7 System.out.println(EduinqFactory.create().getName());
    8 }
    9 }
    a) Eduinq Product
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Product

  7. Which of the following is true about Observer pattern?
    a) Defines one to many dependency
    b) Notifies observers automatically
    c) Used in event handling systems
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  8. Which of the following is a real world example of Observer pattern?
    a) GUI event listeners
    b) Logging frameworks
    c) Stock market updates
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  9. What will be printed?
    1 import java.util.*;
    2 class EduinqObserver implements Observer {
    3 public void update(Observable o, Object arg){
    4 System.out.println("Eduinq Update: " + arg);
    5 }
    6 }
    7 class EduinqSubject extends Observable {
    8 void change(){
    9 setChanged();
    10 notifyObservers("Changed");
    11 }
    12 }
    13 public class Test {
    14 public static void main(String[] args) {
    15 EduinqSubject s = new EduinqSubject();
    16 s.addObserver(new EduinqObserver());
    17 s.change();
    18 }
    19 }
    a) Eduinq Update: Changed
    b) Eduinq Update: Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Update: Changed

  10. Which of the following is true about Strategy pattern?
    a) Defines family of algorithms
    b) Encapsulates each algorithm
    c) Makes them interchangeable at runtime
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  11. Which of the following is NOT a benefit of Strategy pattern?
    a) Promotes flexibility
    b) Avoids conditional logic
    c) Increases coupling between algorithms
    d) Supports runtime changes
    View Answer

    Correct answer: C — Increases coupling between algorithms

  12. What will be printed?
    1 interface EduinqStrategy { String execute(); }
    2 class StrategyA implements EduinqStrategy { public String execute(){ return "A"; } }
    3 class StrategyB implements EduinqStrategy { public String execute(){ return "B"; } }
    4 public class Test {
    5 public static void main(String[] args) {
    6 EduinqStrategy s = new StrategyB();
    7 System.out.println(s.execute());
    8 }
    9 }
    a) A
    b) B
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — B

  13. Which of the following is true about Builder pattern?
    a) Separates construction from representation
    b) Useful for complex objects
    c) Supports fluent API
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  14. Which of the following is NOT a benefit of Builder pattern?
    a) Simplifies object creation
    b) Provides immutability
    c) Increases constructor complexity
    d) Supports optional parameters
    View Answer

    Correct answer: C — Increases constructor complexity

  15. What will be printed?
    1 class Eduinq {
    2 private String name;
    3 private Eduinq(String name){ this.name=name; }
    4 static class Builder {
    5 private String name;
    6 Builder setName(String n){ name=n; return this; }
    7 Eduinq build(){ return new Eduinq(name); }
    8 }
    9 public String toString(){ return "Eduinq " + name; }
    10 }
    11 public class Test {
    12 public static void main(String[] args) {
    13 Eduinq e = new Eduinq.Builder().setName("Portal").build();
    14 System.out.println(e);
    15 }
    16 }
    a) Eduinq Portal
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Portal

  16. Which of the following is true about Proxy pattern?
    a) Provides surrogate for another object
    b) Controls access to real object
    c) Useful for lazy initialization and security
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  17. Which of the following is a real world example of Proxy pattern?
    a) Virtual proxy for images
    b) Remote proxy for web services
    c) Protection proxy for access control
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  18. What will be printed?
    1 interface EduinqService { String serve(); }
    2 class RealService implements EduinqService { public String serve(){ return "Real"; } }
    3 class ProxyService implements EduinqService {
    4 private RealService real = new RealService();
    5 public String serve(){ return "Proxy -> " + real.serve(); }
    6 }
    7 public class Test {
    8 public static void main(String[] args) {
    9 EduinqService s = new ProxyService();
    10 System.out.println(s.serve());
    11 }
    12 }
    a) Proxy -> Real
    b) Real
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Proxy -> Real

  19. Which of the following is true about combining patterns?
    a) Factory + Singleton for controlled instantiation
    b) Observer + Strategy for event driven algorithms
    c) Builder + Proxy for complex object creation with access control
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  20. Which of the following is NOT a creational pattern?
    a) Singleton
    b) Factory
    c) Observer
    d) Builder
    View Answer

    Correct answer: C — Observer

  21. Which of the following is true about Abstract Factory pattern?
    a) Creates families of related objects
    b) Provides interface for factories
    c) Promotes consistency across products
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  22. Which of the following is NOT a behavioral pattern?
    a) Observer
    b) Strategy
    c) Proxy
    d) Command
    View Answer

    Correct answer: C — Proxy

  23. What will be printed?
    1 class EduinqSingleton {
    2 private static EduinqSingleton instance;
    3 private EduinqSingleton(){}
    4 public static EduinqSingleton getInstance(){
    5 if(instance==null) instance=new EduinqSingleton();
    6 return instance;
    7 }
    8 }
    9 public class Test {
    10 public static void main(String[] args) {
    11 System.out.println(EduinqSingleton.getInstance()!=null);
    12 }
    13 }
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true

  24. Which of the following is true about Factory Method vs Abstract Factory?
    a) Factory Method creates one product
    b) Abstract Factory creates families of products
    c) Both promote loose coupling
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  25. Which of the following is NOT a creational pattern?
    a) Builder
    b) Factory
    c) Proxy
    d) Singleton
    View Answer

    Correct answer: C — Proxy

  26. Which of the following is true about Observer pitfalls?
    a) Too many observers may cause performance issues
    b) Improper deregistration may cause memory leaks
    c) Notification order may be unpredictable
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  27. Which of the following is true about Strategy runtime switching?
    a) Allows changing algorithm dynamically
    b) Promotes flexibility
    c) Avoids hard coded logic
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  28. What will be printed?
    1 interface EduinqStrategy { String run(); }
    2 class StrategyX implements EduinqStrategy { public String run(){ return "X"; } }
    3 class StrategyY implements EduinqStrategy { public String run(){ return "Y"; } }
    4 public class Test {
    5 public static void main(String[] args) {
    6 EduinqStrategy s = new StrategyX();
    7 System.out.println(s.run());
    8 }
    9 }
    a) X
    b) Y
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — X

  29. Which of the following is true about Builder immutability?
    a) Builder constructs immutable objects
    b) Fields set via builder cannot be changed later
    c) Promotes thread safety
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  30. Which of the following is NOT a structural pattern?
    a) Proxy
    b) Adapter
    c) Strategy
    d) Composite
    View Answer

    Correct answer: C — Strategy

  31. Which of the following is true about Proxy security use cases?
    a) Restrict access to sensitive objects
    b) Add authentication checks
    c) Provide audit logging
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  32. Which of the following is true about Singleton drawbacks?
    a) May introduce global state
    b) Difficult to test
    c) May break SRP (Single Responsibility Principle)
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  33. Which of the following is true about Factory Method benefits?
    a) Promotes extensibility
    b) Reduces coupling
    c) Simplifies object creation logic
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  34. Which of the following is true about Observer in Java?
    a) Implemented via java.util.Observer
    b) Deprecated in newer versions
    c) Can be replaced with custom implementations
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  35. Which of the following is true about Strategy pattern drawbacks?
    a) May increase number of classes
    b) Requires client awareness of strategies
    c) May add complexity
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  36. What will be printed?
    1 class EduinqBuilder {
    2 private String name;
    3 EduinqBuilder setName(String n){ name=n; return this; }
    4 String build(){ return "Eduinq " + name; }
    5 }
    6 public class Test {
    7 public static void main(String[] args) {
    8 System.out.println(new EduinqBuilder().setName("Design").build());
    9 }
    10 }
    a) Eduinq Design
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Design

  37. Which of the following is true about Proxy vs Decorator?
    a) Proxy controls access, Decorator adds behavior
    b) Both wrap objects
    c) Both are structural patterns
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  38. Which of the following is NOT a benefit of Builder pattern?
    a) Simplifies object creation
    b) Supports optional parameters
    c) Reduces constructor overloads
    d) Increases coupling
    View Answer

    Correct answer: D — Increases coupling

  39. Which of the following is true about Singleton in multithreading?
    a) Requires synchronization for lazy initialization
    b) Enum Singleton avoids synchronization issues
    c) Double checked locking improves performance
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  40. Which of the following is true about Factory vs Builder?
    a) Factory focuses on object type creation
    b) Builder focuses on object construction process
    c) Both are creational patterns
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  41. Which of the following is true about Observer pattern in event systems?
    a) Decouples event source and listeners
    b) Supports multiple listeners
    c) Common in GUI frameworks
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  42. Which of the following is NOT a benefit of Proxy pattern?
    a) Controls access
    b) Adds security
    c) Simplifies object creation
    d) Supports lazy initialization
    View Answer

    Correct answer: C — Simplifies object creation

  43. Which of the following is true about Strategy pattern in payment systems?
    a) Different payment algorithms encapsulated
    b) Client selects strategy at runtime
    c) Promotes flexibility in payment methods
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  44. Which of the following is true about Builder pattern in Java?
    a) Common in libraries like StringBuilder
    b) Supports fluent API
    c) Used in Lombok @Builder annotation
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  45. Which of the following is true about Proxy pattern drawbacks?
    a) May add overhead
    b) May complicate design
    c) May reduce performance
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  46. What will be printed?
    1 interface Eduinq { String get(); }
    2 class Real implements Eduinq { public String get(){ return "Real"; } }
    3 class Proxy implements Eduinq {
    4 private Real r=new Real();
    5 public String get(){ return "Proxy->"+r.get(); }
    6 }
    7 public class Test {
    8 public static void main(String[] args) {
    9 System.out.println(new Proxy().get());
    10 }
    11 }
    a) Proxy->Real
    b) Real
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Proxy->Real

  47. Which of the following is true about combining Builder + Factory?
    a) Factory decides type, Builder constructs details
    b) Promotes flexibility
    c) Useful for complex hierarchies
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  48. Which of the following is NOT a design pattern category?
    a) Creational
    b) Structural
    c) Behavioral
    d) Functional
    View Answer

    Correct answer: D — Functional

  49. Which of the following is true about Singleton alternatives?
    a) Dependency Injection avoids global state
    b) Static factory methods provide controlled instantiation
    c) Enum Singleton ensures thread safety
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  50. Which of the following is true about design patterns overall?
    a) Provide reusable solutions
    b) Improve maintainability
    c) Enhance communication among developers
    d) All of the above
    View Answer

    Correct answer: D — All of the above

Quick Links to Explore