The array starts as [5, 10, 15]. The statement arr[1] = 20; changes the element at index 1 from 10 to 20.
Step 2: Determine final array state
After update, the array becomes [5, 20, 15]. The console.log prints this updated array.
Final Answer:
[5, 20, 15] -> Option C
Quick Check:
Update index 1 to 20 changes array to [5, 20, 15] [OK]
Hint: Remember index 1 is second element, update it directly [OK]
Common Mistakes:
Not updating the element, so output stays same
Mixing up indexes and values
Assuming update changes order
4. Identify the error in the following TypeScript code that tries to update an array element:
let fruits = ['apple', 'banana', 'cherry'];
fruits(1) = 'orange';
medium
A. Using parentheses instead of square brackets for indexing
B. Array is declared with let instead of const
C. Missing semicolon at the end of the line
D. Trying to assign a string to an array element
Solution
Step 1: Check array element update syntax
Array elements must be accessed or updated using square brackets [], not parentheses ().
Step 2: Identify the error in code
The code uses fruits(1) = 'orange'; which is invalid syntax. It should be fruits[1] = 'orange';.
Final Answer:
Using parentheses instead of square brackets for indexing -> Option A
Quick Check:
Use square brackets [] to access/update array elements [OK]
Hint: Use [] not () to access or update array elements [OK]
Common Mistakes:
Using parentheses instead of brackets
Thinking let vs const causes error here
Ignoring syntax errors due to missing semicolon
5. Given the array let scores = [10, 20, 30, 40, 50];, which code snippet correctly doubles the value at index 3 and then prints the updated array?
hard
A. scores[3] = scores[3] * 2;
console.log(scores);
B. scores[3] *= 2;
console.log(scores);
C. scores.update(3, scores[3] * 2);
console.log(scores);
D. scores[3] = scores[3] + scores[3];
console.log(scores);
Solution
Step 1: Understand how to double element at index 3
Index 3 corresponds to the fourth element (value 40). Doubling means multiplying by 2.
Step 2: Check each option for correct syntax and effect
scores[3] = scores[3] * 2;
console.log(scores); uses explicit multiplication and assignment, which works. scores[3] *= 2;
console.log(scores); uses shorthand *= operator, also correct. scores.update(3, scores[3] * 2);
console.log(scores); uses a non-existent method update(). scores[3] = scores[3] + scores[3];
console.log(scores); adds the element to itself, which also doubles it.
Step 3: Identify the best concise and correct option
scores[3] *= 2;
console.log(scores); is the most concise and idiomatic way to double the value at index 3.
Final Answer:
scores[3] *= 2;
console.log(scores); -> Option B
Quick Check:
Use *= operator to double value at index 3 [OK]
Hint: Use *= operator to multiply and update in one step [OK]
Common Mistakes:
Using non-existent update() method
Forgetting to print the array after update
Using addition instead of multiplication
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.