TypeScript Readonly and Immutability Quiz

TypeScript
0 Passed
0% acceptance

A 30-question TypeScript quiz covering readonly properties, immutable arrays and tuples, const behavior, structural immutability, shallow freezing, and readonly utility types.

30 Questions
~60 minutes
1

Question 1

What does the readonly modifier prevent?

A
Reassignment of the property after initialization
B
Reading the property
C
Using the property in functions
D
Exporting the property
2

Question 2

Readonly affects:

A
Compile-time assignment checks
B
Runtime mutation blocking
C
Automatic JSON serialization
D
Module imports
3

Question 3

Why does this error?

typescript
const user = { name: 'A', age: 20 } as const;
user.age = 30;
A
Properties inferred from 'as const' become readonly
B
age cannot be a number
C
user must be a class
D
const cannot be used on objects
4

Question 4

What does readonly not guarantee?

A
Deep immutability at runtime
B
Static prevention of reassignment
C
More predictable state
D
Protection from accidental writes
5

Question 5

Why is push not allowed?

typescript
const nums: readonly number[] = [1, 2, 3];
nums.push(4);
A
readonly arrays disallow mutation methods like push
B
push only works on strings
C
number[] cannot be readonly
D
The array is frozen at runtime
6

Question 6

Which statement is true about readonly tuples?

A
Their length and item types cannot change
B
They support push by default
C
They are mutable like normal arrays
D
They convert automatically to enums
7

Question 7

What type is inferred here?

typescript
const coord = [10, 20] as const;
A
readonly [10, 20]
B
number[]
C
readonly number[]
D
[number, number]
8

Question 8

ReadonlyArray<T> prevents:

A
All mutating array methods
B
Iteration
C
Index access
D
Length reading
9

Question 9

What does const prevent?

A
Reassignment of the variable binding
B
Mutation of nested object values
C
Reading the value
D
Using the value inside functions
10

Question 10

Which statement is true?

A
readonly applies to properties; const applies to bindings
B
readonly and const are identical
C
const freezes nested values
D
readonly works only on arrays
11

Question 11

What does Readonly<T> do?

typescript
type User = { name: string; age: number };
type RU = Readonly<User>;
A
Makes all properties readonly
B
Makes properties optional
C
Removes all keys
D
Converts values to unknown
12

Question 12

ReadonlyArray<T> is equivalent to:

A
readonly T[]
B
T[][]
C
T[]
D
Partial<T>
13

Question 13

Immutability encourages:

A
Predictable program state
B
Automatic runtime freezing
C
Tuple-only code
D
Removing all objects
14

Question 14

Which operation is allowed?

typescript
const settings: Readonly<{ dark: boolean }> = { dark: true };
A
Reading settings.dark
B
Reassigning settings.dark
C
Adding new properties
D
Mutating settings as a function
15

Question 15

Which is a benefit of immutable data?

A
Easier reasoning about shared state
B
Automatic performance boosts
C
Guaranteed deep freezing
D
Removal of all side effects
16

Question 16

Why is this allowed?

typescript
const obj = { inner: { x: 1 } } as const;
obj.inner.x = 2;
A
Readonly is shallow and inner object is still mutable
B
x must always change
C
Literal types freeze deep objects
D
inner must be a number
17

Question 17

Readonly<T> creates:

A
Shallow readonly properties
B
Deeply immutable structures
C
Unions of values
D
Unknown types
18

Question 18

What does Object.freeze do at runtime?

typescript
const data = Object.freeze({ a: 1 });
A
Prevents adding/removing/changing properties
B
Creates a union type
C
Forces boolean values
D
Enables template literal inference
19

Question 19

Which limitation does Object.freeze have?

A
It freezes only shallow properties
B
It converts numbers to strings
C
It disables iteration
D
It alters prototype chains
20

Question 20

Why can't arr[0] be reassigned?

typescript
const arr: readonly [string, number] = ['x', 10];
arr[0] = 'y';
A
Readonly tuples prevent element reassignment
B
Strings cannot be reassigned
C
Index 0 is numeric only
D
Literal types must remain constant
21

Question 21

Immutability helps avoid:

A
Side effects caused by shared mutable state
B
Using arrays
C
All runtime errors
D
Strict type checking
22

Question 22

Why is assignment invalid?

typescript
class Point {
  readonly x: number;
  constructor() { this.x = 5; }
}
const p = new Point();
p.x = 10;
A
readonly class fields cannot be reassigned after construction
B
Class fields must be mutable
C
x must be a string
D
Classes cannot contain readonly fields
23

Question 23

Readonly in classes ensures:

A
Instances cannot modify the property after construction
B
The class cannot be instantiated
C
The constructor cannot run
D
The property becomes optional
24

Question 24

What is a recommended immutability pattern?

A
Returning new objects rather than mutating existing ones
B
Modifying arrays directly
C
Using const only
D
Always freezing deep objects manually
25

Question 25

Readonly applies primarily to:

A
Preventing reassignment, not restricting logic
B
Guaranteeing deep immutability
C
Runtime performance improvements
D
Automatic runtime validation
26

Question 26

Immutable structures often lead to:

A
Fewer unintended side effects
B
Forced synchronous code
C
No need for type checking
D
Restrictions on function usage
27

Question 27

Which is NOT a feature of readonly?

A
Guaranteeing deep object immutability
B
Preventing reassignment in code
C
Improving clarity in APIs
D
Reducing accidental mutations
28

Question 28

Which syntax makes an array deeply readonly?

A
TypeScript alone cannot guarantee deep readonly without custom utilities
B
readonly number[]
C
const arr = []
D
Object.freeze only
29

Question 29

Readonly is helpful in large projects because:

A
It restricts mutation, making bugs easier to track
B
It eliminates runtime errors
C
It forces all variables to be constants
D
It removes the need for function parameters
30

Question 30

Overall, readonly helps developers:

A
Build more predictable and stable code
B
Avoid all compile errors
C
Automatically freeze deep values
D
Remove type annotations

QUIZZES IN TypeScript