A. Type 'number' is not assignable to type 'readonly number[]'
B. Cannot assign to '0' because it is a read-only property
C. Syntax error: missing semicolon
D. No error, code runs fine
Solution
Step 1: Understand the readonly array behavior
The array nums is readonly, so its elements cannot be changed after creation.
Step 2: Analyze the assignment to an element
Trying to assign nums[0] = 10 causes an error because elements are read-only.
Final Answer:
Cannot assign to '0' because it is a read-only property -> Option B
Quick Check:
Readonly elements cannot be changed [OK]
Hint: Readonly arrays block element assignment [OK]
Common Mistakes:
Thinking only array structure is readonly, not elements
Ignoring TypeScript error messages
Assuming semicolon missing causes error
5. Given a function that accepts a read-only array, which of these operations inside the function is allowed?
function process(items: readonly number[]) {
// Which line is valid?
// A) items.push(5);
// B) items[0] = 10;
// C) const first = items[0];
// D) items.length = 0;
}
hard
A. const first = items[0];
B. items.push(5);
C. items[0] = 10;
D. items.length = 0;
Solution
Step 1: Understand read-only array restrictions
Read-only arrays do not allow modifying methods like push, element assignment, or length changes.
Step 2: Check each operation
Only reading elements, like const first = items[0];, is allowed. Others cause errors.
Final Answer:
const first = items[0]; -> Option A
Quick Check:
Reading elements allowed, modifying disallowed [OK]
Hint: Readonly arrays allow reading, not modifying [OK]
Common Mistakes:
Trying to push or assign elements in readonly arrays
Attempting to change array length
Confusing read-only with mutable arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.