MCQs on TypeScript Design Patterns | TypeScript

Chapter 18 of TypeScript delves into design patterns such as Dependency Injection, Singleton, Factory, and Builder patterns. Understanding these patterns enhances your ability to write scalable, maintainable, and testable code in TypeScript.


Multiple Choice Questions (MCQs)

Section 1: Dependency Injection

  1. What is the primary purpose of Dependency Injection (DI)?
    a) To reduce the complexity of code
    b) To control the flow of data
    c) To provide components with their dependencies externally
    d) To enhance the performance of applications
  2. Which of the following is NOT a benefit of using Dependency Injection?
    a) Improved testability
    b) Reduced code duplication
    c) Tight coupling of components
    d) Easier maintenance
  3. What is the core idea behind Dependency Injection in TypeScript?
    a) Injecting data into a class directly
    b) Passing dependencies into classes at runtime
    c) Creating classes manually without relying on external dependencies
    d) Injecting classes as a function return value
  4. In TypeScript, what is an example of a constructor-based Dependency Injection?
    a) class MyClass { constructor(private dependency: Service) {}
    b) class MyClass { dependency = new Service()}
    c) class MyClass { constructor() {}
    d) class MyClass { dependency: Service; }
  5. Which of the following is a typical characteristic of Dependency Injection in TypeScript?
    a) Dependencies are passed into a class constructor or methods
    b) Dependencies are hard-coded within the class
    c) Dependencies are fetched using require()
    d) Dependencies are only used in static methods
  6. What does Dependency Injection help to achieve in TypeScript applications?
    a) Easier debugging
    b) Increased memory usage
    c) Loose coupling between components
    d) Decreased code readability
  7. What is an example of a TypeScript Dependency Injection container?
    a) InversifyJS
    b) React
    c) Angular
    d) Lodash
  8. How can Dependency Injection improve testability in TypeScript?
    a) By allowing mocking and stubbing of dependencies in unit tests
    b) By making dependencies public
    c) By using a global state for the entire application
    d) By reducing the number of required tests
  9. Which of the following is a common disadvantage of Dependency Injection?
    a) It can result in more complex setup and configuration
    b) It reduces the flexibility of the application
    c) It makes the codebase smaller
    d) It makes applications faster
  10. What kind of testing is Dependency Injection commonly used for?
    a) Performance testing
    b) Unit testing
    c) Integration testing
    d) UI testing

Section 2: Singleton and Factory Patterns with TypeScript

  1. What is the main purpose of the Singleton design pattern?
    a) To create multiple instances of a class
    b) To allow only one instance of a class throughout the application
    c) To create a class with dynamic properties
    d) To manage component lifecycles
  2. Which of the following best describes the Singleton pattern?
    a) It is a pattern where a class can have multiple instances
    b) It restricts a class to a single instance
    c) It is used to extend classes
    d) It is used for creating interfaces
  3. How does the Singleton pattern ensure only one instance of a class?
    a) By using a static method to return the same instance
    b) By creating a new instance every time
    c) By limiting the class constructor to one parameter
    d) By implementing an event listener pattern
  4. In TypeScript, which method ensures only one instance of a class in Singleton pattern?
    a) A private constructor and a static method returning the single instance
    b) A public constructor and a private static instance variable
    c) A factory function that creates instances
    d) A class that doesn’t allow inheritance
  5. Which of the following best defines the Factory design pattern?
    a) It defines how to create an object but lets subclasses alter the type of objects that will be created
    b) It is used to create one instance of a class
    c) It is used to define an interface for creating families of related or dependent objects
    d) It is a pattern where an object’s behavior can be defined dynamically
  6. How does the Factory pattern differ from the Singleton pattern?
    a) Factory creates multiple instances; Singleton ensures one instance
    b) Singleton creates multiple instances; Factory ensures one instance
    c) Factory pattern is only for static classes
    d) Singleton can create multiple objects with shared data
  7. How do you implement the Singleton pattern in TypeScript?
    a) By creating a class with a static method returning the instance
    b) By using a constructor with a return type of void
    c) By using a factory function to create instances
    d) By creating a static instance variable and instantiating it in a constructor
  8. In which situation is the Singleton pattern most useful?
    a) When you need to control access to a shared resource
    b) When you need to create several instances of a class
    c) When you want to decouple code
    d) When creating independent objects
  9. Which is the main benefit of the Factory pattern?
    a) It isolates the client from the creation process of objects
    b) It guarantees only one instance of a class
    c) It uses fewer resources
    d) It makes all objects mutable
  10. How does TypeScript support the Factory pattern?
    a) By using constructors with optional arguments
    b) By allowing factory functions to return instances of different types
    c) By using class inheritance
    d) By allowing abstract classes

Section 3: Builder Pattern in TypeScript

  1. What is the purpose of the Builder design pattern?
    a) To create an object by constructing it step by step
    b) To allow objects to be shared among multiple classes
    c) To create different objects based on a shared interface
    d) To store objects in a collection
  2. Which scenario is ideal for using the Builder pattern?
    a) When you want to create objects with a complex structure and multiple configurations
    b) When you want to restrict object creation to one instance
    c) When you need a shared pool of objects
    d) When you need to define an interface for creating objects
  3. What does the Director do in the Builder pattern?
    a) It manages the construction process by calling methods on the builder
    b) It creates the objects based on provided configurations
    c) It defines the interfaces for the Builder
    d) It keeps track of created objects
  4. In TypeScript, how is the Builder pattern typically implemented?
    a) By using classes and methods to construct parts of an object
    b) By using a static factory method to create objects
    c) By using singletons to control object creation
    d) By extending a base class and overriding methods
  5. Which of the following is a key benefit of using the Builder pattern in TypeScript?
    a) It allows complex object creation to be broken down into simpler, reusable methods
    b) It reduces the number of classes needed
    c) It guarantees only one instance of an object
    d) It creates lightweight objects
  6. How can you customize the product created by the Builder pattern in TypeScript?
    a) By passing configuration options to the builder methods
    b) By using a single method to create all objects
    c) By subclassing the builder class
    d) By changing the object’s type dynamically
  7. How does the Builder pattern help in managing complex object creation?
    a) By breaking down the construction process into smaller, more manageable steps
    b) By avoiding the use of constructors entirely
    c) By creating singletons for complex objects
    d) By allowing direct object manipulation without methods
  8. In TypeScript, which of the following classes can be used to build a product in the Builder pattern?
    a) The Builder class itself
    b) The Product class
    c) The Director class
    d) The Client class
  9. Which of the following is a drawback of the Builder pattern?
    a) It can result in code duplication if not used properly
    b) It requires significant overhead to implement
    c) It does not allow flexible object configurations
    d) It is difficult to use in a multi-threaded environment
  10. How does TypeScript’s type system support the Builder pattern?
    a) By enforcing strict interfaces for builders and products
    b) By providing a way to define object properties dynamically
    c) By allowing inheritance in builder classes
    d) By using generics to define product types

Answer Key

QnoAnswer
1c) To provide components with their dependencies externally
2c) Tight coupling of components
3b) Passing dependencies into classes at runtime
4a) class MyClass { constructor(private dependency: Service) {}
5a) Dependencies are passed into a class constructor or methods
6c) Loose coupling between components
7a) InversifyJS
8a) By allowing mocking and stubbing of dependencies in unit tests
9a) It can result in more complex setup and configuration
10b) Unit testing
11b) To allow only one instance of a class throughout the application
12b) It restricts a class to a single instance
13a) By using a static method to return the same instance
14a) A private constructor and a static method returning the single instance
15a) It defines how to create an object but lets subclasses alter the type of objects that will be created
16a) Factory creates multiple instances; Singleton ensures one instance
17a) By creating a class with a static method returning the instance
18a) When you need to control access to a shared resource
19a) It isolates the client from the creation process of objects
20b) By allowing factory functions to return instances of different types
21a) To create an object by constructing it step by step
22a) When you want to create objects with a complex structure and multiple configurations
23a) It manages the construction process by calling methods on the builder
24a) By using classes and methods to construct parts of an object
25a) It allows complex object creation to be broken down into smaller, reusable methods
26a) By passing configuration options to the builder methods
27a) By breaking down the construction process into smaller, more manageable steps
28b) The Product class
29b) It requires significant overhead to implement
30a) By enforcing strict interfaces for builders and products

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top