MCQs on Enums and Constants | TypeScript

Enums and constants are powerful TypeScript features that help improve code readability and maintainability. This guide covers numeric and string enums, heterogeneous enums, and using const with enums for optimal performance.


Enums and Constants in TypeScript – 30 Multiple Choice Questions

1. Numeric and String Enums

  1. What is the default value of the first member in a TypeScript numeric enum?
    • A) 0
    • B) 1
    • C) Undefined
    • D) Custom value
  2. How are the values assigned to subsequent members in a numeric enum if the first member is set to 5?
    • A) All members are set to 5
    • B) Values increase by 2
    • C) Values increment by 1 from 5
    • D) Values are random
  3. Which keyword is used to define an enum in TypeScript?
    • A) enum
    • B) const
    • C) type
    • D) class
  4. How can you access the value of a member in a TypeScript enum?
    • A) By using enum.Member
    • B) By using enum[Member]
    • C) By using enum.MemberName
    • D) By using enum.MemberValue
  5. What will be the output of the following code?typescriptCopy codeenum Colors { Red = 1, Green, Blue } console.log(Colors.Green);
    • A) 1
    • B) 2
    • C) 3
    • D) Undefined
  6. How are string enums different from numeric enums in TypeScript?
    • A) They do not allow custom values
    • B) Each member must be initialized with a string literal
    • C) They cannot be used in switch cases
    • D) They are case-insensitive
  7. How do you define a string enum in TypeScript?
    • A) enum Colors { Red = "RED", Green = "GREEN" }
    • B) enum Colors { Red: "RED", Green: "GREEN" }
    • C) enum Colors { Red = 1, Green = "GREEN" }
    • D) const Colors { Red = "RED", Green = "GREEN" }
  8. What is the output of the following code?typescriptCopy codeenum Directions { North = "N", South = "S", East = "E", West = "W" } console.log(Directions.West);
    • A) N
    • B) S
    • C) W
    • D) Undefined
  9. How can enums be used with string interpolation in TypeScript?
    • A) Using ${enum.Member}
    • B) Using ${enum[Member]}
    • C) Using ${enum.MemberName}
    • D) Using template literals like ${Directions.West}
  10. What will be the output of the following code?typescriptCopy codeenum Status { Active = "ACTIVE", Inactive = "INACTIVE" } console.log(Status['Inactive']);
    • A) active
    • B) inactive
    • C) INACTIVE
    • D) Undefined

2. Heterogeneous Enums

  1. What is a heterogeneous enum in TypeScript?
    • A) An enum with both numeric and string members
    • B) An enum that only accepts strings
    • C) An enum with undefined values
    • D) An enum that only accepts numbers
  2. How do you define a heterogeneous enum?
    • A) enum Mixed { A = 1, B = "Two" }
    • B) enum Mixed { A: 1, B: "Two" }
    • C) enum Mixed { A = "One", B = "Two" }
    • D) enum Mixed { A = true, B = false }
  3. What will be the output of the following code?typescriptCopy codeenum Mixed { Yes = "YES", No = 0 } console.log(Mixed.No);
    • A) YES
    • B) 0
    • C) Undefined
    • D) true
  4. Why are heterogeneous enums generally discouraged?
    • A) They can cause type checking issues
    • B) They increase memory usage
    • C) They do not support numeric values
    • D) They cannot be used in classes
  5. Can a heterogeneous enum be used as a type in TypeScript?
    • A) Yes, but it is not recommended
    • B) No, TypeScript does not allow it
    • C) Only if all members are strings
    • D) Only if all members are numbers
  6. What will be the result of accessing a non-existent enum member?typescriptCopy codeenum Days { Monday = 1, Tuesday, Wednesday } console.log(Days.Thursday);
    • A) Undefined
    • B) 3
    • C) Error
    • D) Null

3. Using const with Enums

  1. What is the benefit of using const enums in TypeScript?
    • A) They allow duplicate values
    • B) They produce more efficient compiled JavaScript code
    • C) They are used for type inference
    • D) They support dynamic values
  2. How do you declare a const enum?
    • A) const enum Colors { Red, Green, Blue }
    • B) enum const Colors { Red, Green, Blue }
    • C) enum Colors { const Red, Green, Blue }
    • D) const Colors = { Red, Green, Blue }
  3. Which of the following statements is true about const enums?
    • A) They do not support string values
    • B) They are fully inlined during compilation
    • C) They allow runtime modification
    • D) They are slower than regular enums
  4. What is the output of the following code?typescriptCopy codeconst enum Flags { On = 1, Off = 0 } console.log(Flags.On);
    • A) 1
    • B) 0
    • C) On
    • D) Undefined
  5. How does TypeScript handle const enums in the compiled JavaScript code?
    • A) Converts them to objects
    • B) Inlines their values directly
    • C) Ignores them entirely
    • D) Wraps them in a function
  6. Which of the following is NOT a valid use case for const enums?
    • A) Constants that are used frequently
    • B) Values that need to be computed at runtime
    • C) Enumerations that do not require reverse mapping
    • D) Performance-critical code
  7. What will be the output of the following code?typescriptCopy codeconst enum Access { Read = "READ", Write = "WRITE" } console.log(Access.Read);
    • A) READ
    • B) Write
    • C) Undefined
    • D) Error
  8. Can const enums be used with for...in loops?
    • A) Yes, they behave like regular enums
    • B) No, they are fully inlined and not iterable
    • C) Only with numeric values
    • D) Only with string values

4. Additional Concepts

  1. How can you ensure that enum members have unique values?
    • A) By using a const prefix
    • B) By explicitly assigning values to each member
    • C) By using the strict keyword
    • D) By using heterogeneous values
  2. What is a reverse mapping in TypeScript enums?
    • A) Accessing string values using numeric indices
    • B) Getting the key from the value
    • C) Mapping enums to functions
    • D) Using enums in reverse order
  3. Which type of enums allow reverse mapping in TypeScript?
    • A) String enums
    • B) Heterogeneous enums
    • C) Numeric enums
    • D) const enums
  4. What does the following TypeScript code output?typescriptCopy codeenum Level { Low, Medium, High } console.log(Level[2]);
    • A) Low
    • B) Medium
    • C) High
    • D) Undefined
  5. How can you convert a numeric enum value to its corresponding string name?
    • A) By using Object.values()
    • B) By using bracket notation like enum[value]
    • C) By using enum.toString()
    • D) By using enum.reverse()
  6. Which of the following is NOT a recommended use of enums in TypeScript?
    • A) For defining a set of related constants
    • B) For replacing type unions
    • C) For type-checking variable values
    • D) For managing application state

Answer Key

QnoAnswer (Option with the text)
1A) 0
2C) Values increment by 1 from 5
3A) enum
4C) By using enum.MemberName
5B) 2
6B) Each member must be initialized with a string literal
7A) enum Colors { Red = "RED", Green = "GREEN" }
8C) W
9D) Using template literals like ${Directions.West}
10C) INACTIVE
11A) An enum with both numeric and string members
12A) enum Mixed { A = 1, B = "Two" }
13B) 0
14A) They can cause type checking issues
15A) Yes, but it is not recommended
16A) Undefined
17B) They produce more efficient compiled JavaScript code
18A) const enum Colors { Red, Green, Blue }
19B) They are fully inlined during compilation
20A) 1
21B) Inlines their values directly
22B) Values that need to be computed at runtime
23A) READ
24B) No, they are fully inlined and not iterable
25B) By explicitly assigning values to each member
26B) Getting the key from the value
27C) Numeric enums
28C) High
29B) By using bracket notation like enum[value]
30D) For managing application state

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