TypeScript Enums and Tuples Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz covering numeric enums, string enums, const enums, reverse mapping, tuple types, readonly tuples, labeled tuples, and practical usage patterns.

40 Questions
~80 minutes
1

Question 1

What keyword creates an enum in TypeScript?

A
enum
B
set
C
map
D
group
2

Question 2

What values does Status.Active and Status.Inactive have?

typescript
enum Status { Active, Inactive }
A
0 and 1
B
1 and 2
C
true and false
D
'Active' and 'Inactive'
3

Question 3

Which type of enum supports reverse mapping?

A
Numeric enums
B
String enums
C
Const enums
D
Union enums
4

Question 4

Which statement about string enums is true?

A
Each member must be assigned a string literal
B
They auto-increment like numbers
C
They support reverse mapping
D
They can compute numeric values
5

Question 5

What is Direction.Left?

typescript
enum Direction { Left = 'L', Right = 'R' }
A
'L'
B
0
C
'Left'
D
undefined
6

Question 6

What is a computed enum member?

A
A member whose value is calculated from an expression
B
A member with no name
C
A member that is always numeric
D
A member that must be abstract
7

Question 7

What value does E.B get?

typescript
enum E { A = 2, B }
A
3
B
2
C
1
D
undefined
8

Question 8

Which enum type is fully erased during compilation?

A
const enums
B
numeric enums
C
string enums
D
merged enums
9

Question 9

What is printed?

typescript
enum X { A, B }
console.log(X[1]);
A
'B'
B
'A'
C
1
D
undefined
10

Question 10

Which statement describes enum merging?

A
Two enum declarations with same name merge
B
Enums merge with objects automatically
C
Enums never merge
D
Only string enums can merge
11

Question 11

What values are valid members?

typescript
enum Flags { A = 1 << 0, B = 1 << 1 }
A
1 and 2
B
0 and 1
C
true and false
D
'A' and 'B'
12

Question 12

Why use enums instead of objects?

A
They provide stricter value sets with type checking
B
They run faster at runtime
C
They always reduce bundle size
D
They create instances automatically
13

Question 13

What is a tuple in TypeScript?

A
A fixed-length array with ordered element types
B
A dynamic map
C
A type alias for any array
D
A numeric enum
14

Question 14

What type is Pair?

typescript
type Pair = [number, string];
A
A tuple with number then string
B
An object type
C
A union
D
A readonly array
15

Question 15

Which operation is valid on a tuple?

A
Accessing known index types
B
Adding arbitrary elements anywhere
C
Changing tuple length freely
D
Assigning a larger array without checks
16

Question 16

What prints?

typescript
const t: [number, number] = [1, 2];
console.log(t[1]);
A
2
B
1
C
undefined
D
Error
17

Question 17

What distinguishes a tuple from an array?

A
Tuples enforce fixed positions and types
B
Tuples guarantee deep immutability
C
Tuples must contain primitives only
D
Arrays are compile-time only
18

Question 18

Optional tuple elements use which notation?

A
?
B
*
C
+
D
#
19

Question 19

Which tuple definition allows extra elements?

typescript
type T = [string, ...number[]];
A
A leading string and any amount of numbers
B
Only strings
C
Only two elements
D
A fixed-length triple
20

Question 20

What do labeled tuples improve?

A
Readability of tuple positions
B
Runtime performance
C
Reverse mapping
D
Enum merging
21

Question 21

What does this define?

typescript
type Coords = [x: number, y: number];
A
A labeled tuple with x then y
B
A union type
C
An enum
D
A dictionary map
22

Question 22

What does readonly do for tuples?

A
Prevents modifying elements
B
Prevents reading elements
C
Forces all elements to be strings
D
Extends tuple length
23

Question 23

What error occurs?

typescript
const p: readonly [number, number] = [3, 4];
p[0] = 10;
A
Cannot assign to read-only property
B
10 logs
C
undefined
D
No error
24

Question 24

Which is a benefit of readonly tuples?

A
Safer representation of fixed structured data
B
Automatic tuple expansion
C
Runtime locking of memory
D
Increased performance by default
25

Question 25

What type is returned?

typescript
function use(): [string, number] { return ['x', 1]; }
A
A tuple containing a string then number
B
An array of strings
C
A number array
D
A union
26

Question 26

Why use tuples for function returns?

A
To group multiple values without objects
B
To enforce named parameters
C
To allow infinite length always
D
To create classes
27

Question 27

What prints?

typescript
const [a, b] = ['first', 2];
console.log(a);
A
'first'
B
2
C
undefined
D
Error
28

Question 28

Which tuple feature improves API clarity?

A
Labels for each element
B
Automatic type merging
C
Runtime metadata
D
Unlimited length
29

Question 29

What advantage do tuples have over arrays?

A
Exact lengths and element types
B
Dynamic size at all times
C
Require only numeric elements
D
Infer readonly by default
30

Question 30

What statement describes tuple assignment rules?

A
Assignments must match length and types
B
Tuples accept any array
C
Tuples ignore element order
D
Tuples merge with enums
31

Question 31

Which describes a heterogeneous tuple?

A
Mixed element types
B
Only numbers
C
Unlimited size
D
All elements identical
32

Question 32

What are enum members treated as when used in types?

A
Literal types
B
any
C
unknown
D
const assertions
33

Question 33

Const enums are inlined at:

A
Compile time
B
Runtime only
C
During garbage collection
D
In the type checker only
34

Question 34

Why should const enums be used carefully?

A
They require full compilation to avoid errors
B
They behave differently at runtime
C
They allow object merging
D
They disable tuple types
35

Question 35

Which describes tuple spreading?

A
Expanding tuple elements into another tuple
B
Converting tuples to objects automatically
C
Reversing tuple order
D
Merging enums
36

Question 36

Which describes a union of tuples?

A
A type that may be one of several tuple formats
B
A merged tuple
C
A numeric enum
D
A readonly type only
37

Question 37

What happens when accessing a tuple index out of range?

A
It returns the array element type union
B
It guarantees undefined
C
It throws a compile error always
D
It becomes readonly
38

Question 38

Why might tuples be inappropriate for complex data?

A
They lack named properties
B
They cannot contain objects
C
They are slower than arrays
D
They break type inference always
39

Question 39

Which feature do enums provide?

A
Predefined set of named constant values
B
Arbitrary object merging
C
Mutation of tuple indices
D
Automatic function creation
40

Question 40

What advantage do tuples offer compared to multi-value objects?

A
Compact, index-based data structures
B
Automatic naming of properties
C
Self-merging behaviour
D
Runtime enforcement of types

QUIZZES IN TypeScript