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
What is the default value of the first member in a TypeScript numeric enum?
A) 0
B) 1
C) Undefined
D) Custom value
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
Which keyword is used to define an enum in TypeScript?
A) enum
B) const
C) type
D) class
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
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
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
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" }
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
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}
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
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
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 }
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
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
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
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
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
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 }
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
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
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
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
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
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
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
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
Which type of enums allow reverse mapping in TypeScript?
A) String enums
B) Heterogeneous enums
C) Numeric enums
D) const enums
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
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()
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
Qno
Answer (Option with the text)
1
A) 0
2
C) Values increment by 1 from 5
3
A) enum
4
C) By using enum.MemberName
5
B) 2
6
B) Each member must be initialized with a string literal
7
A) enum Colors { Red = "RED", Green = "GREEN" }
8
C) W
9
D) Using template literals like ${Directions.West}
10
C) INACTIVE
11
A) An enum with both numeric and string members
12
A) enum Mixed { A = 1, B = "Two" }
13
B) 0
14
A) They can cause type checking issues
15
A) Yes, but it is not recommended
16
A) Undefined
17
B) They produce more efficient compiled JavaScript code