MCQs on Advanced Interfaces and Types | TypeScript

Mastering advanced interfaces and types in TypeScript helps developers build robust, scalable, and flexible applications. This set of 30 MCQs covers topics such as extending interfaces, intersection and union of interfaces, mapped and conditional types, and utility types like Partial, Readonly, Pick, and Record.


Advanced Interfaces and Types in TypeScript – MCQs

1. Extending Interfaces

  1. Which keyword is used to extend an interface in TypeScript?
    • a) extends
    • b) include
    • c) inherit
    • d) implements
  2. Can an interface extend multiple interfaces in TypeScript?
    • a) No, interfaces can only extend one interface
    • b) Yes, interfaces can extend multiple interfaces
    • c) Yes, but only classes can extend multiple interfaces
    • d) No, only classes can extend multiple interfaces
  3. What happens when an interface extends multiple interfaces with conflicting properties?
    • a) TypeScript throws a compilation error
    • b) The property from the first interface is used
    • c) The property from the last interface is used
    • d) TypeScript merges the properties and allows the conflict
  4. Which of the following is a correct way to extend multiple interfaces?
    interface A { propA: string; } interface B { propB: string; } interface C extends A, B { propC: string; }
    • a) This is invalid syntax
    • b) This is a correct way to extend multiple interfaces
    • c) Only one interface can be extended
    • d) Interfaces A and B cannot be extended at once
  5. Can an interface extend a class in TypeScript?
    • a) Yes, interfaces can extend classes in TypeScript
    • b) No, interfaces cannot extend classes
    • c) Yes, but only abstract classes
    • d) No, only classes can extend other classes
  6. Which of the following is the result of extending an interface with another interface?
    • a) The properties of the child interface are merged with the parent
    • b) The child interface overrides the parent interface
    • c) The child interface cannot have properties
    • d) The parent interface loses its properties

2. Intersection and Union of Interfaces

  1. What does the intersection of two interfaces do in TypeScript?
    • a) Creates a union type from the two interfaces
    • b) Merges properties from both interfaces
    • c) Creates a type where the properties of both interfaces are required
    • d) Removes duplicate properties between the interfaces
  2. Which symbol is used for creating an intersection type between two interfaces in TypeScript?
    • a) &
    • b) |
    • c) $
    • d) *
  3. What is the output type of the following code?
    interface A { name: string; } interface B { age: number; } type C = A & B;
    • a) { name: string; age: number; }
    • b) { name: string; }
    • c) { age: number; }
    • d) { name: string; age: string; }
  4. What does the union of two interfaces mean in TypeScript?
    • a) Properties of either interface are required, but not both
    • b) The properties of both interfaces must exist in the object
    • c) It merges the properties from both interfaces
    • d) It creates a new interface with no properties
  5. Which symbol is used for creating a union type between two interfaces in TypeScript?
    • a) &
    • b) |
    • c) $
    • d) ^
  6. Which of the following creates a valid union type between two interfaces in TypeScript?
    interface A { name: string; } interface B { age: number; } type C = A | B;
    • a) This creates a type where name or age is required
    • b) This is invalid syntax
    • c) This creates a new interface with properties name and age
    • d) This type requires both name and age properties

3. Mapped Types and Conditional Types

  1. What is a mapped type in TypeScript?
    • a) A type that iterates over an object’s properties
    • b) A type that defines a new structure for an object
    • c) A type used to define a set of possible values
    • d) A type used to convert a class to an interface
  2. Which of the following creates a mapped type for all properties in the object as string type?
    type Stringify<T> = { [K in keyof T]: string };
    • a) It changes the type of all properties to string
    • b) It converts all properties to nullable
    • c) It adds a new property called K to the object
    • d) It removes all properties of the object
  3. What does the keyof keyword do in TypeScript?
    • a) It allows you to iterate over all values of an object
    • b) It gives you the type of an object’s keys
    • c) It returns the value type of an object’s key
    • d) It marks an object property as optional
  4. What is the purpose of conditional types in TypeScript?
    • a) To define types that change based on conditions
    • b) To define types for union types only
    • c) To create union types with specific conditions
    • d) To loop over an object and generate new types
  5. Which of the following is a conditional type that checks if T is a string?
    type IsString<T> = T extends string ? 'Yes' : 'No';
    • a) It returns ‘Yes’ if T is a string, otherwise ‘No’
    • b) It checks if T is a string and throws an error
    • c) It returns ‘No’ for all types
    • d) It always returns ‘Yes’
  6. How do you create a conditional type that checks if a type extends another type?
    • a) Using the extends keyword inside the type definition
    • b) Using the if keyword inside the type definition
    • c) Using the match keyword inside the type definition
    • d) Using the check keyword inside the type definition
  7. What is the output type of the following code?typescriptCopy codetype IsNumber<T> = T extends number ? 'Yes' : 'No'; type Test = IsNumber<string>;
    • a) ‘Yes’
    • b) ‘No’
    • c) string
    • d) number
  8. What does the mapped type { [K in keyof T]: string } do?
    • a) Converts all properties of T to strings
    • b) Makes all properties of T optional
    • c) Creates a new type with the same properties as T, but of type string
    • d) Combines all properties of T into a string

4. Utility Types (Partial, Readonly, Pick, Record)

  1. What does the Partial<T> utility type do?
    • a) Makes all properties in T optional
    • b) Makes all properties in T readonly
    • c) Makes all properties in T nullable
    • d) Picks a subset of properties from T
  2. What is the purpose of the Readonly<T> utility type?
    • a) It makes all properties in T optional
    • b) It ensures all properties in T cannot be modified
    • c) It makes all properties in T mutable
    • d) It defines new properties in T
  3. How does the Pick<T, K> utility type work?
    • a) It makes properties from T of type K
    • b) It picks properties from T specified in K
    • c) It removes properties from T that are in K
    • d) It picks properties from K and adds them to T
  4. What does the Record<K, T> utility type do?
    • a) It creates an object with keys of type K and values of type T
    • b) It creates a set of keys from K
    • c) It ensures the keys in T are of type K
    • d) It creates a new interface with all properties of T
  5. Which of the following code demonstrates the use of the Partial<T> utility type?t
    interface User { name: string; age: number; } const partialUser: Partial<User> = { name: "Alice" };
    • a) Partial<T> makes name and age optional
    • b) Partial<T> makes name optional
    • c) Partial<T> makes age optional
    • d) Partial<T> ensures all properties must be provided
  6. Which utility type makes all properties in an object immutable?
    • a) Partial
    • b) Readonly
    • c) Pick
    • d) Record
  7. Which of the following is true about the Pick<T, K> utility type?
    • a) It removes properties from T based on K
    • b) It creates a new type with only the properties of T that are in K
    • c) It makes the properties of T optional
    • d) It ensures properties of T are readonly
  8. What will be the output of the following code using Record<K, T>?
    type MyRecord = Record<string, number>;
    • a) A record with string keys and number values
    • b) A record with number keys and string values
    • c) A string with numbers concatenated
    • d) An array of numbers
  9. Which utility type would you use to ensure that a type’s properties cannot be changed after initialization?
    • a) Pick
    • b) Partial
    • c) Readonly
    • d) Record
  10. What does the utility type Record<string, any> define?
    • a) An object with string keys and any type of values
    • b) An array of strings
    • c) An object where values are restricted to a certain type
    • d) A string with any value

Answers

QNoAnswer
1a) extends
2b) Yes, interfaces can extend multiple interfaces
3a) TypeScript throws a compilation error
4b) This is a correct way to extend multiple interfaces
5a) Yes, interfaces can extend classes in TypeScript
6a) The properties of the child interface are merged with the parent
7b) Merges properties from both interfaces
8a) &
9a) { name: string; age: number; }
10a) Properties of either interface are required, but not both
11b)
12a) This creates a type where name or age is required
13a) A type that iterates over an object’s properties
14a) It changes the type of all properties to string
15b) It gives you the type of an object’s keys
16a) To define types that change based on conditions
17a) It returns ‘Yes’ if T is a string, otherwise ‘No’
18a) Using the extends keyword inside the type definition
19b) ‘No’
20c) Creates a new type with the same properties as T, but of type string
21a) Makes all properties in T optional
22b) It ensures all properties in T cannot be modified
23b) It picks properties from T specified in K
24a) It creates an object with keys of type K and values of type T
25a) Partial<T> makes name and age optional
26b) Readonly
27b) It creates a new type with only the properties of T that are in K
28a) A record with string keys and number values
29c) Readonly
30a) An object with string keys and any type of values

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