MCQs on Generics | TypeScript

Master TypeScript’s powerful generics with 30 MCQs designed to boost your understanding. Dive into generic functions, classes, interfaces, and constraints to enhance your TypeScript skills and flexibility.


Chapter 9: Generics in TypeScript

Introduction to Generics

  1. What is the purpose of generics in TypeScript?
    • a) To create functions that accept any type of data
    • b) To enable the reuse of a function with different data types
    • c) To enforce type safety without specifying the exact types
    • d) To define a constant value for each type
  2. How do you define a generic function in TypeScript?
    • a) function name<T>(value: T): T { return value; }
    • b) function <T> name(value: T): T { return value; }
    • c) function name<T>(value) { return value; }
    • d) generic function<T> name(value: T): T { return value; }
  3. Which of the following is a correct example of a generic type parameter in a function?
    • a) function identity(value): T { return value; }
    • b) function identity<T>(value: T): T { return value; }
    • c) function identity(value: T): T { return value; }
    • d) function<T> identity(value: T): T { return value; }
  4. What does the <T> represent in a generic function?
    • a) A placeholder for any type
    • b) A specific data type
    • c) A number type
    • d) A reference to the function itself
  5. Which of the following is NOT a benefit of using generics in TypeScript?
    • a) Code reusability with type safety
    • b) Enabling dynamic type assignment
    • c) Preventing runtime type errors
    • d) Enabling the use of any data type

Generic Functions

  1. How do you call a generic function with a specific type in TypeScript?
    • a) identity<string>("Hello World");
    • b) identity<string>()("Hello World");
    • c) identity("Hello World");
    • d) identity("Hello World"): string;
  2. Which function signature below defines a generic function correctly?
    • a) function<T>(value: T): T { return value; }
    • b) function identity<T>(value: T): T { return value; }
    • c) function identity(value: T): T { return value; }
    • d) function identity<T> (value) { return value; }
  3. What happens when you omit the generic type in a function call?
    • a) TypeScript infers the type automatically
    • b) An error occurs
    • c) The function cannot be called without a type
    • d) TypeScript defaults to any type
  4. In the following generic function, what will the type of output be? function echo<T>(value: T): T { return value; } const output = echo(5);
    • a) string
    • b) number
    • c) any
    • d) T
  5. How do you specify multiple generic type parameters in a function?
    • a) function myFunction<T, U>(param1: T, param2: U): T { return param1; }
    • b) function myFunction<T|U>(param1: T, param2: U): T { return param1; }
    • c) function myFunction<T U>(param1: T, param2: U): T { return param1; }
    • d) function myFunction<T, U>(param1, param2): T { return param1; }

Generic Classes and Interfaces

  1. How do you define a generic class in TypeScript?
    • a) class Box<T> { value: T; }
    • b) class Box { T: type; }
    • c) class Box<T> { T value; }
    • d) class Box { value: T; }
  2. In a generic class, how can you specify the type of a property?
    • a) value: T;
    • b) T value;
    • c) T: value;
    • d) value: T<Type>;
  3. Which of the following is the correct way to instantiate a generic class with a specific type?
    • a) const box = new Box<number>();
    • b) const box = new Box();
    • c) const box = Box<number>();
    • d) const box = Box();
  4. How do you create a generic interface in TypeScript?
    • a) interface Container<T> { value: T; }
    • b) interface Container { value: T; }
    • c) interface Container<T> { value: string; }
    • d) interface<T> Container { value: T; }
  5. Which of the following is an example of using a generic interface?
    • a) const container: Container<string> = { value: "Hello" };
    • b) const container = { value: "Hello" };
    • c) const container: Container = { value: "Hello" };
    • d) const container: string = { value: "Hello" };

Constraints in Generics

  1. What is a constraint in TypeScript generics?
    • a) A way to restrict the types that can be passed to a generic function
    • b) A way to create a constant type
    • c) A method for defining default values for types
    • d) A way to convert one type to another
  2. How do you apply a constraint to a generic type parameter in TypeScript?
    • a) <T extends Type>
    • b) <T is Type>
    • c) T: Type
    • d) T => Type
  3. Which constraint ensures that the generic type extends from a specific class?
    • a) T extends ClassName
    • b) T is ClassName
    • c) T implements ClassName
    • d) T = ClassName
  4. In the following code, what does T extends { length: number } do?
    function getLength<T extends { length: number }>(item: T): number { return item.length; }
    • a) Restricts T to types that have a length property
    • b) Ensures that T is always a string
    • c) Limits T to types that are numbers
    • d) Defines T as a string only
  5. Which of the following demonstrates using a constraint in a generic function?
    • a) function merge<T extends { name: string }>(object: T) { return object; }
    • b) function merge<T extends string>(object: T) { return object; }
    • c) function merge<T>(object: T) { return object; }
    • d) function merge(object: T) { return object; }
  6. What is the result of using a constraint like T extends number?
    • a) Only numbers can be passed to the function
    • b) The type T will default to number
    • c) It allows any type but infers number automatically
    • d) The type T will be forced to a string
  7. How would you create a generic function that only accepts objects with id properties?
    • a) function getId<T extends { id: number }>(object: T): number { return object.id; }
    • b) function getId<T extends { id: string }>(object: T): number { return object.id; }
    • c) function getId<T extends { id: number }>(object): T { return object.id; }
    • d) function getId(object: T): number { return object.id; }
  8. Can you combine multiple constraints for a generic type in TypeScript?
    • a) Yes, using the extends keyword
    • b) No, only one constraint can be applied
    • c) Yes, using the & operator
    • d) Yes, using the or operator
  9. What is the correct way to apply a constraint to a generic type that must extend both A and B?
    • a) <T extends A & B>
    • b) <T extends A or B>
    • c) <T extends A | B>
    • d) <T extends A, B>
  10. Can a constraint be applied to an array in TypeScript?
    • a) Yes, using the array constructor
    • b) No, only objects can have constraints
    • c) Yes, using T[] extends Object
    • d) Yes, using T extends Array<any>
  11. What is the role of extends in a generic constraint?
    • a) It specifies that the type must be a subclass
    • b) It defines that the type must inherit from another class
    • c) It enforces that the type must be an array
    • d) It allows any type to be passed
  12. How can we ensure that a generic type can only be a string or a number?
    • a) <T extends string | number>
    • b) <T is string | number>
    • c) <T extends string & number>
    • d) <T extends string | number[] >
  13. Which of the following is a valid constraint to use in a generic function?
    • a) <T extends { length: number }>
    • b) <T extends string | number>
    • c) <T extends { length: number; toString(): string }>
    • d) All of the above
  14. What does the extends constraint in a generic function ensure?
    • a) The generic type conforms to the specified type’s properties or methods
    • b) The type can be anything
    • c) The type must be an object
    • d) The type can only be a string
  15. How does TypeScript infer the type in a generic function with constraints?
    • a) It uses the type provided at the time of function call
    • b) It defaults to any type
    • c) It always tries to infer string
    • d) It ignores the type and uses a default generic

Answer Key

QnoAnswer
1b) To enable the reuse of a function with different data types
2a) function name<T>(value: T): T { return value; }
3b) function identity<T>(value: T): T { return value; }
4a) A placeholder for any type
5b) Enabling dynamic type assignment
6a) identity<string>("Hello World");
7b) function identity<T>(value: T): T { return value; }
8a) TypeScript infers the type automatically
9b) number
10a) function myFunction<T, U>(param1: T, param2: U): T { return param1; }
11a) class Box<T> { value: T; }
12a) value: T;
13a) const box = new Box<number>();
14a) interface Container<T> { value: T; }
15a) const container: Container<string> = { value: "Hello" };
16a) A way to restrict the types that can be passed to a generic function
17a) <T extends Type>
18a) Restricts T to types that have a length property
19a) Restricts T to types that have a length property
20a) function merge<T extends { name: string }>(object: T) { return object; }
21a) Only numbers can be passed to the function
22a) function getId<T extends { id: number }>(object: T): number { return object.id; }
23a) Yes, using the extends keyword
24a) <T extends A & B>
25a) Yes, using the array constructor
26a) It specifies that the type must be a subclass
27a) `<T extends string
28d) All of the above
29a) The generic type conforms to the specified type’s properties or methods
30a) It uses the type provided at the time of function call

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