MCQs on Advanced Types | TypeScript

Chapter 8 of TypeScript delves into advanced types such as union and intersection types, literal types, type guards, nullable types, and index signatures. These concepts help in creating flexible and type-safe code.


Multiple Choice Questions (MCQs)

Section 1: Union and Intersection Types

  1. What is a union type in TypeScript?
    a) A type that combines two or more types using |
    b) A type that restricts values to one specific type
    c) A type that combines multiple values into an array
    d) A type that enforces an intersection of types
  2. Which of the following is a valid union type?
    a) let x: number | string;
    b) let x: number & string;
    c) let x: number -> string;
    d) let x: string | boolean | 42;
  3. How do intersection types differ from union types in TypeScript?
    a) Intersection types combine types, while union types restrict to one type
    b) Union types combine multiple types into one, while intersection types merge all properties
    c) Intersection types can be used only with objects
    d) Union types are for primitive types only
  4. Which of the following is an example of an intersection type?
    a) let x: number | string;
    b) let x: string & { age: number };
    c) let x: number & string;
    d) let x: string | boolean;
  5. In TypeScript, which operator is used to define intersection types?
    a) |
    b) &
    c) =>
    d) *
  6. Which of the following defines a union type of string and number correctly?
    a) let x: string | number;
    b) let x: string, number;
    c) let x: string & number;
    d) let x: number -> string;
  7. What is the result of an intersection type in TypeScript?
    a) A single value that can be one of several types
    b) A value that must fulfill all types it intersects
    c) An array of combined types
    d) A new type that combines primitive types only
  8. How would you define a union of string, boolean, and null types?
    a) let x: string | boolean | null;
    b) let x: string & boolean & null;
    c) let x: string -> boolean | null;
    d) let x: string, boolean, null;

Section 2: Literal Types and Type Guards

  1. What are literal types in TypeScript?
    a) Types that represent exact values instead of general types
    b) Types that can only be used with numbers
    c) Types that define an object’s structure
    d) Types that define the exact type and its possible variations
  2. Which of the following is an example of a literal type?
    a) let x: 10;
    b) let x: string;
    c) let x: any;
    d) let x: boolean;
  3. What does TypeScript type guarding allow you to do?
    a) Combine two types into one
    b) Narrow down a type to a specific subtype
    c) Define complex union types
    d) Allow variables to be assigned any value
  4. Which of the following is an example of a type guard in TypeScript?
    a) if (x instanceof Date)
    b) let x: number & string;
    c) let x: string | number;
    d) function checkType(val: string | number) { return val instanceof String; }
  5. How does a type guard help narrow down types?
    a) It merges the types of multiple values
    b) It allows you to specify additional constraints
    c) It ensures that variables are always nullable
    d) It enables conditional checks to specify the exact type
  6. In TypeScript, which of the following can be used as a type guard?
    a) typeof
    b) instanceof
    c) in
    d) All of the above
  7. How do you implement a custom type guard in TypeScript?
    a) Use the as keyword to specify types
    b) Create a function that returns a boolean indicating the type
    c) Define the guard inside the constructor function
    d) Use type to specify guard rules
  8. Which of the following is the correct syntax for a literal type that allows only the string ‘true’?
    a) let x: 'true';
    b) let x: string = 'true';
    c) let x: boolean = true;
    d) let x: 'true' | 'false';

Section 3: Nullable Types

  1. What does the null type represent in TypeScript?
    a) A variable that is not defined
    b) A variable with a value of null
    c) A variable that has no type assigned
    d) A variable that is undefined
  2. How do you specify that a value can be either string or null in TypeScript?
    a) let x: string | null;
    b) let x: string, null;
    c) let x: string & null;
    d) let x: null => string;
  3. Which of the following would allow a variable to be assigned a null value in TypeScript?
    a) let x: string;
    b) let x: string | null;
    c) let x: undefined;
    d) let x: any;
  4. What happens if you try to assign a null to a variable that is not nullable?
    a) It is converted to undefined
    b) TypeScript throws an error
    c) It works without issues
    d) The variable becomes of type null
  5. Which of the following is correct when allowing a variable to be nullable?
    a) let x: null;
    b) let x: null | string;
    c) let x: string;
    d) let x: string => null;
  6. How do nullable types impact type safety in TypeScript?
    a) They ensure all variables are assigned types
    b) They allow variables to have a null value, preventing errors
    c) They prevent null values from being assigned
    d) They restrict variables to specific values only

Section 4: Index Signatures

  1. What is an index signature in TypeScript?
    a) A type that defines the shape of objects with dynamic keys
    b) A function used to access object properties
    c) A method for declaring arrays
    d) A rule for specifying an object’s property values
  2. How do you declare an index signature in TypeScript?
    a) let obj: { [key: string]: number; }
    b) let obj: { index: string; }
    c) let obj: { number[]; }
    d) let obj: string = {} ;
  3. Which of the following is an example of an index signature in TypeScript?
    a) let obj: { [key: string]: string; };
    b) let obj: { key: string; value: number; };
    c) let obj: { key => string; };
    d) let obj: { [key, number]: string; };
  4. Can an index signature have a type other than string or number?
    a) No, only string and number are allowed
    b) Yes, it can be symbol
    c) Yes, it can be boolean
    d) Yes, it can be any type
  5. How does TypeScript handle index signatures when an object has dynamic properties?
    a) It restricts the keys to a predefined set
    b) It allows any key and ensures the type matches the value type
    c) It automatically infers the key type
    d) It throws an error if a dynamic key is used
  6. What happens if you use an index signature on an object with a fixed structure?
    a) It overrides the fixed structure
    b) The object must adhere to both the fixed structure and the index signature
    c) It causes a runtime error
    d) TypeScript automatically infers the correct structure
  7. How do you enforce a type for all properties in an object using an index signature?
    a) let obj: { [key: string]: any; };
    b) let obj: { [key: string]: string; };
    c) let obj: { [key: number]: boolean; };
    d) let obj: { [key: symbol]: number; };
  8. Which of the following is true about the use of index signatures?
    a) They allow defining any property type on an object
    b) They restrict objects to only one type of key
    c) They only work with arrays
    d) They are limited to string keys only

Answer Key

QnoAnswer
1a) A type that combines two or more types using `
2a) `let x: number
3b) Union types combine multiple types into one, while intersection types merge all properties
4b) let x: string & { age: number };
5b) &
6a) `let x: string
7b) A value that must fulfill all types it intersects
8a) `let x: string
9a) Types that represent exact values instead of general types
10a) let x: 10;
11b) Narrow down a type to a specific subtype
12a) if (x instanceof Date)
13b) It allows you to specify additional constraints
14d) All of the above
15b) Create a function that returns a boolean indicating the type
16a) let x: 'true';
17b) A variable with a value of null
18a) `let x: string
19b) `let x: string
20b) TypeScript throws an error
21b) `let x: null
22b) They allow variables to have a null value, preventing errors
23a) A type that defines the shape of objects with dynamic keys
24a) let obj: { [key: string]: number; }
25a) let obj: { [key: string]: string; };
26b) Yes, it can be symbol
27b) It allows any key and ensures the type matches the value type
28b) The object must adhere to both the fixed structure and the index signature
29b) let obj: { [key: string]: string; };
30a) They allow defining any property type on an object

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