MCQs on Decorators | TypeScript

Unlock the power of TypeScript decorators with 30 MCQs designed to test your knowledge on class, method, property decorators, parameter decorators, and the metadata reflection API to improve your coding skills.


Chapter 14: Decorators in TypeScript

Introduction to Decorators

  1. What is a decorator in TypeScript?
    • a) A function that modifies the behavior of a class, method, or property
    • b) A way to define variables in TypeScript
    • c) A function that adds event listeners to elements
    • d) A class that handles metadata
  2. Which of the following is the correct syntax to define a decorator in TypeScript?
    • a) @decorator()
    • b) @decorator
    • c) function decorator() {}
    • d) decorator@()
  3. Where can decorators be applied in TypeScript?
    • a) Only to classes
    • b) Only to methods
    • c) Classes, methods, properties, and parameters
    • d) Only to functions
  4. Which TypeScript feature must be enabled to use decorators?
    • a) strictNullChecks
    • b) experimentalDecorators
    • c) esModuleInterop
    • d) noImplicitAny
  5. What is the role of a decorator factory?
    • a) It creates a new class
    • b) It is used to create a decorator with parameters
    • c) It defines the type of a class
    • d) It adds methods to a class

Class, Method, and Property Decorators

  1. Which of the following is a valid class decorator?
    • a) @classDecorator()
    • b) @classDecorator
    • c) @decoratorClass()
    • d) @decorator()
  2. What is the main purpose of a class decorator?
    • a) To modify the class’s prototype
    • b) To modify the class’s constructor or add properties
    • c) To attach event listeners to the class
    • d) To prevent the class from being instantiated
  3. What is the correct syntax for a method decorator?
    • a) @methodDecorator()
    • b) @methodDecorator
    • c) method @decorator()
    • d) method @decorator
  4. What does a method decorator receive as arguments?
    • a) The class constructor, property key, and descriptor
    • b) Only the property key
    • c) The class and the method parameters
    • d) The instance of the class
  5. Which of the following describes a property decorator?
    • a) It can be applied only to static properties
    • b) It can modify a property in a class
    • c) It is used to bind methods to events
    • d) It adds private or protected properties

Parameter Decorators

  1. What is a parameter decorator used for in TypeScript?
    • a) To modify the behavior of a method
    • b) To add metadata to method parameters
    • c) To define the type of a method parameter
    • d) To prevent method invocation
  2. How is a parameter decorator defined in TypeScript?
    • a) @parameterDecorator(parameterIndex)
    • b) @parameterDecorator()
    • c) parameterDecorator(parameterIndex)
    • d) @parameterDecorator(parameterIndex, target)
  3. What information does a parameter decorator have access to?
    • a) The method’s name and the class’s constructor
    • b) The parameter’s index and the method’s prototype
    • c) The parameter’s type
    • d) The method’s return type
  4. Which of the following is true about parameter decorators?
    • a) They can be used to modify the return value of methods
    • b) They only work on instance methods
    • c) They can’t modify parameter values, only add metadata
    • d) They are used to add event listeners
  5. When are parameter decorators invoked in TypeScript?
    • a) During method definition
    • b) After the method is called
    • c) When the method is invoked
    • d) When the class is instantiated

Metadata Reflection API

  1. What does the Metadata Reflection API in TypeScript provide?
    • a) A way to reflect on and analyze the types and metadata of classes and their members
    • b) A way to modify a class’s metadata dynamically
    • c) A way to generate classes dynamically
    • d) A method for type checking at runtime
  2. Which library needs to be imported to use the Metadata Reflection API?
    • a) reflect-metadata
    • b) ts-metadata
    • c) type-reflect
    • d) meta-reflection
  3. How do you define metadata using the reflection API?
    • a) Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey)
    • b) Reflect.metadata(metadataKey, metadataValue)
    • c) Reflect.setMetadata(target, metadataKey, metadataValue)
    • d) metadata.set(target, metadataKey, metadataValue)
  4. What does Reflect.getMetadata(metadataKey, target, propertyKey) return?
    • a) The class constructor
    • b) The metadata defined on a class or method
    • c) The property value
    • d) The method’s arguments
  5. Which decorator enables the use of the Metadata Reflection API?
    • a) @Reflect
    • b) @metadata
    • c) @ReflectMetadata
    • d) @decorator
  6. How can the Metadata Reflection API help with decorators?
    • a) It allows decorators to retrieve and manipulate metadata attached to class members
    • b) It replaces the need for decorators
    • c) It helps to prevent class instantiation
    • d) It allows decorators to define type-checking rules
  7. What is the purpose of Reflect.getOwnMetadata in the Metadata Reflection API?
    • a) To check the type of a class
    • b) To retrieve metadata for the target object only
    • c) To check if metadata is present on a class
    • d) To modify metadata dynamically
  8. What does the @Reflect.metadata decorator do in TypeScript?
    • a) It creates a static property on a class
    • b) It adds metadata to a class or method for reflection
    • c) It prevents method overriding
    • d) It checks the method’s parameter types
  9. How can you retrieve metadata of a class method in TypeScript?
    • a) Reflect.getMetadata(metadataKey, target, propertyKey)
    • b) Reflect.getMetadata(metadataKey, propertyKey)
    • c) Reflect.getTargetMetadata(target)
    • d) Reflect.fetchMetadata(target, propertyKey)
  10. Can you use the Metadata Reflection API without decorators in TypeScript?
    • a) No, decorators are required for metadata reflection
    • b) Yes, you can manually define metadata
    • c) Yes, but it requires complex manual configuration
    • d) No, it is not possible
  11. How do you access metadata defined on a method in TypeScript?
    • a) Using Reflect.getMetadata
    • b) Using method.getMetadata
    • c) Using Reflect.accessMetadata
    • d) Using metadata.get()
  12. What is the main advantage of using decorators with the Metadata Reflection API?
    • a) It simplifies type-checking and metadata management in TypeScript
    • b) It reduces the runtime overhead of classes
    • c) It automatically applies validations to method parameters
    • d) It allows automatic code generation
  13. Which of the following is a correct use of metadata in decorators?
    • a) Reflect.defineMetadata('isRequired', true, target, 'name')
    • b) Reflect.setMetadata(target, 'name', 'isRequired')
    • c) Reflect.getMetadata(target, 'isRequired')
    • d) Reflect.deleteMetadata('isRequired', target)
  14. What happens if metadata is not found when using Reflect.getMetadata?
    • a) It throws an error
    • b) It returns null or undefined
    • c) It returns the default metadata
    • d) It ignores the metadata request
  15. How can you ensure decorators and the Metadata Reflection API work together effectively?
    • a) By importing reflect-metadata and enabling emitDecoratorMetadata
    • b) By using @Reflect decorators only
    • c) By not using the reflection API with decorators
    • d) By using manual reflection instead of decorators

Answer Key

QnoAnswer
1a) A function that modifies the behavior of a class, method, or property
2a) @decorator()
3c) Classes, methods, properties, and parameters
4b) experimentalDecorators
5b) It is used to create a decorator with parameters
6b) @classDecorator
7b) To modify the class’s constructor or add properties
8a) @methodDecorator()
9a) The class constructor, property key, and descriptor
10b) It can modify a property in a class
11b) To add metadata to method parameters
12a) @parameterDecorator(parameterIndex)
13b) The parameter’s index and the method’s prototype
14c) They can’t modify parameter values, only add metadata
15a) During method definition
16a) A way to reflect on and analyze the types and metadata of classes and their members
17a) reflect-metadata
18a) Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey)
19b) The metadata defined on a class or method
20c) @ReflectMetadata
21a) It allows decorators to retrieve and manipulate metadata attached to class members
22b) To retrieve metadata for the target object only
23b) It adds metadata to a class or method for reflection
24a) Reflect.getMetadata(metadataKey, target, propertyKey)
25b) Yes, you can manually define metadata
26a) Reflect.getMetadata(metadataKey, target, propertyKey)
27a) It simplifies type-checking and metadata management in TypeScript
28a) Reflect.defineMetadata('isRequired', true, target, 'name')
29b) It returns null or undefined
30a) By importing reflect-metadata and enabling emitDecoratorMetadata

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