Design Patterns and Best Practices MCQs

  1. Which of the following is true about Singleton pattern?
    a) Ensures only one instance exists
    b) Provides global access point
    c) Used for logging, configuration, caching
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  2. 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 EduinqSingleton s1 = EduinqSingleton.getInstance();
    12 EduinqSingleton s2 = EduinqSingleton.getInstance();
    13 System.out.println(s1 == s2);
    14 }
    15 }
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true

  3. Which of the following is true about Factory pattern?
    a) Creates objects without exposing instantiation logic
    b) Promotes loose coupling
    c) Used in frameworks for object creation
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  4. What will be printed?
    1 interface EduinqProduct { void show(); }
    2 class EduinqA implements EduinqProduct { public void show(){ System.out.println("Eduinq A"); } }
    3 class EduinqFactory {
    4 static EduinqProduct getProduct(String type) {
    5 if(type.equals("A")) return new EduinqA();
    6 return null;
    7 }
    8 }
    9 public class Test {
    10 public static void main(String[] args) {
    11 EduinqProduct p = EduinqFactory.getProduct("A");
    12 p.show();
    13 }
    14 }
    a) Eduinq A
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq A

  5. Which of the following is true about Builder pattern?
    a) Separates construction from representation
    b) Useful for complex objects
    c) Provides step by step construction
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  6. What will be printed?
    1 class EduinqBuilder {
    2 private String name;
    3 EduinqBuilder setName(String name){ this.name=name; return this; }
    4 Eduinq build(){ return new Eduinq(name); }
    5 }
    6 class Eduinq {
    7 String name;
    8 Eduinq(String name){ this.name=name; }
    9 public String toString(){ return name; }
    10 }
    11 public class Test {
    12 public static void main(String[] args) {
    13 Eduinq e = new EduinqBuilder().setName("Eduinq 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

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

    Correct answer: A — Eduinq Update

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

    Correct answer: D — All of the above

  10. What will be printed?
    1 interface EduinqStrategy { void execute(); }
    2 class EduinqA implements EduinqStrategy { public void execute(){ System.out.println("Eduinq Strategy A"); } }
    3 class Context {
    4 private EduinqStrategy strategy;
    5 Context(EduinqStrategy s){ this.strategy=s; }
    6 void run(){ strategy.execute(); }
    7 }
    8 public class Test {
    9 public static void main(String[] args) {
    10 Context c = new Context(new EduinqA());
    11 c.run();
    12 }
    13 }
    a) Eduinq Strategy A
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Strategy A

  11. Which of the following is true about Dependency Injection (DI)?
    a) Provides dependencies from outside
    b) Promotes loose coupling
    c) Used in frameworks like Spring
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  12. What will be printed?
    1 class EduinqService { void serve(){ System.out.println("Eduinq Service"); } }
    2 class Client {
    3 private EduinqService service;
    4 Client(EduinqService s){ this.service=s; }
    5 void doWork(){ service.serve(); }
    6 }
    7 public class Test {
    8 public static void main(String[] args) {
    9 Client c = new Client(new EduinqService());
    10 c.doWork();
    11 }
    12 }
    a) Eduinq Service
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Service

  13. Which of the following is true about Eager Singleton?
    a) Instance created at class loading
    b) Thread safe by default
    c) May cause resource wastage if unused
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  14. What will be printed?
    1 class EduinqSingleton {
    2 private static final 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

  15. Which of the following is true about Double Checked Locking Singleton?
    a) Reduces synchronization overhead
    b) Uses volatile keyword
    c) Thread safe
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  16. Which of the following is true about Abstract Factory pattern?
    a) Provides interface for creating families of related objects
    b) Promotes consistency among products
    c) Used in GUI toolkits
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  17. What will be printed?
    1 interface EduinqButton { void click(); }
    2 class WinButton implements EduinqButton { public void click(){ System.out.println("Eduinq Windows Button"); } }
    3 class MacButton implements EduinqButton { public void click(){ System.out.println("Eduinq Mac Button"); } }
    4 interface EduinqFactory { EduinqButton createButton(); }
    5 class WinFactory implements EduinqFactory { public EduinqButton createButton(){ return new WinButton(); } }
    6 public class Test {
    7 public static void main(String[] args) {
    8 EduinqFactory f = new WinFactory();
    9 f.createButton().click();
    10 }
    11 }
    a) Eduinq Windows Button
    b) Eduinq Mac Button
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Windows Button

  18. Which of the following is true about Immutable Builder objects?
    a) Builder constructs immutable instances
    b) Fields cannot be changed after creation
    c) Promotes thread safety
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  19. Which of the following is true about Observer pattern in JavaFX?
    a) Uses property listeners
    b) Automatically updates UI components
    c) Implements reactive programming style
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  20. What will be printed?
    1 import javafx.beans.property.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 StringProperty sp = new SimpleStringProperty("Eduinq");
    5 sp.addListener((obs,oldVal,newVal)->System.out.println("Changed to "+newVal));
    6 sp.set("Portal");
    7 }
    8 }
    a) Changed to Portal
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Changed to Portal

  21. Which of the following is true about Strategy pattern with lambdas?
    a) Simplifies implementation
    b) Reduces boilerplate code
    c) Promotes functional style
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  22. What will be printed?
    1 interface EduinqStrategy { void run(); }
    2 public class Test {
    3 public static void main(String[] args) {
    4 EduinqStrategy s = () -> System.out.println("Eduinq Lambda Strategy");
    5 s.run();
    6 }
    7 }
    a) Eduinq Lambda Strategy
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Lambda Strategy

  23. Which of the following is true about Dependency Injection annotations in Spring?
    a) @Autowired injects dependencies
    b) @Component marks beans
    c) @Configuration defines configuration classes
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  24. What will be printed?
    1 import org.springframework.beans.factory.annotation.*;
    2 import org.springframework.stereotype.*;
    3 @Component
    4 class EduinqService { public String serve(){ return "Eduinq Spring Service"; } }
    5 @Component
    6 class Client {
    7 @Autowired EduinqService service;
    8 public void work(){ System.out.println(service.serve()); }
    9 }
    a) Eduinq Spring Service
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Spring Service

  25. Which of the following is true about SOLID principles?
    a) Promote maintainable code
    b) Include Single Responsibility, Open/Closed, Liskov, Interface Segregation, Dependency Inversion
    c) Encourage best practices
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  26. Which of the following is true about DRY principle?
    a) Don't Repeat Yourself
    b) Avoids duplication
    c) Improves maintainability
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  27. Which of the following is true about KISS principle?
    a) Keep It Simple, Stupid
    b) Encourages simplicity
    c) Avoids over engineering
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  28. Which of the following is true about modular design?
    a) Breaks system into modules
    b) Promotes reusability
    c) Improves scalability
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  29. Which of the following is true about Adapter pattern?
    a) Converts interface of class into another
    b) Promotes compatibility
    c) Used in legacy integration
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  30. What will be printed?
    1 interface EduinqTarget { void request(); }
    2 class Legacy { void specificRequest(){ System.out.println("Eduinq Legacy"); } }
    3 class Adapter implements EduinqTarget {
    4 private Legacy legacy = new Legacy();
    5 public void request(){ legacy.specificRequest(); }
    6 }
    7 public class Test {
    8 public static void main(String[] args) {
    9 EduinqTarget t = new Adapter();
    10 t.request();
    11 }
    12 }
    a) Eduinq Legacy
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Legacy

  31. Which of the following is true about Proxy pattern?
    a) Provides surrogate for another object
    b) Controls access
    c) Useful for lazy loading, security
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  32. Which of the following is true about Decorator pattern?
    a) Adds behavior dynamically
    b) Promotes flexibility
    c) Used in I/O streams
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  33. Which of the following is true about Composite pattern?
    a) Treats individual and composite objects uniformly
    b) Represents part whole hierarchy
    c) Used in GUI components
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  34. Which of the following is true about Command pattern?
    a) Encapsulates request as object
    b) Supports undo/redo
    c) Promotes decoupling
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  35. Which of the following is true about Template Method pattern?
    a) Defines skeleton of algorithm
    b) Allows subclasses to override steps
    c) Promotes code reuse
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  36. Which of the following is true about State pattern?
    a) Allows object to change behavior when state changes
    b) Encapsulates states as classes
    c) Promotes cleaner code
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  37. Which of the following is true about Chain of Responsibility?
    a) Passes request along chain of handlers
    b) Decouples sender and receiver
    c) Used in logging, event handling
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  38. Which of the following is true about Mediator pattern?
    a) Defines object to encapsulate communication
    b) Promotes loose coupling
    c) Used in chat systems, UI frameworks
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  39. Which of the following is true about Memento pattern?
    a) Captures object state
    b) Allows restoring state later
    c) Promotes encapsulation without exposing internals
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  40. What will be printed?
    1 class Eduinq {
    2 private String state;
    3 public void setState(String s){ state=s; }
    4 public String getState(){ return state; }
    5 public Memento save(){ return new Memento(state); }
    6 public void restore(Memento m){ state=m.getState(); }
    7 static class Memento {
    8 private String state;
    9 Memento(String s){ state=s; }
    10 String getState(){ return state; }
    11 }
    12 }
    13 public class Test {
    14 public static void main(String[] args) {
    15 Eduinq e = new Eduinq();
    16 e.setState("Eduinq Portal");
    17 Eduinq.Memento m = e.save();
    18 e.setState("Changed");
    19 e.restore(m);
    20 System.out.println(e.getState());
    21 }
    22 }
    a) Eduinq Portal
    b) Changed
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Portal

  41. Which of the following is true about Flyweight pattern?
    a) Shares common state among objects
    b) Reduces memory usage
    c) Used in rendering systems
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  42. Which of the following is true about Interpreter pattern?
    a) Defines grammar representation
    b) Provides interpreter for language
    c) Used in expression evaluation
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  43. Which of the following is true about Visitor pattern?
    a) Separates algorithm from object structure
    b) Adds new operations without modifying classes
    c) Uses double dispatch
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  44. Which of the following is true about Bridge pattern?
    a) Decouples abstraction from implementation
    b) Promotes flexibility
    c) Used in GUI frameworks
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  45. Which of the following is true about Facade pattern?
    a) Provides simplified interface
    b) Hides complexity of subsystems
    c) Promotes ease of use
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  46. Which of the following is true about Best Practices in Singleton?
    a) Use enum for thread safe singleton
    b) Avoid reflection attacks
    c) Ensure serialization safety
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  47. Which of the following is true about Best Practices in Factory?
    a) Use interfaces for products
    b) Avoid hardcoding logic
    c) Promote extensibility
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  48. Which of the following is true about Best Practices in Builder?
    a) Use fluent API style
    b) Ensure immutability of built objects
    c) Provide defaults for optional fields
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  49. Which of the following is true about Best Practices in Observer?
    a) Avoid memory leaks by removing observers
    b) Ensure thread safety in notifications
    c) Use weak references if needed
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  50. Which of the following is true about Best Practices in Dependency Injection?
    a) Prefer constructor injection
    b) Avoid service locator anti pattern
    c) Use DI frameworks for scalability
    d) All of the above
    View Answer

    Correct answer: D — All of the above

Quick Links to Explore