What does it mean to access an element in an array by index?
Accessing an element by index means retrieving the value stored at a specific position in the array, where the index starts at 0 for the first element.
Click to reveal answer
beginner
How do you update an element at a specific index in an array?
To update an element, you assign a new value to the array at that index, replacing the old value.
Click to reveal answer
intermediate
What happens if you try to access an index outside the array bounds?
Accessing an index outside the array bounds returns undefined or causes an error, depending on the language or environment.
Click to reveal answer
beginner
TypeScript code snippet: How to access the third element of an array named 'numbers'?
numbers[2]; // Because array indexes start at 0, the third element is at index 2.
Click to reveal answer
beginner
TypeScript code snippet: How to update the first element of an array 'colors' to 'red'?
colors[0] = 'red'; // This changes the first element of the array to 'red'.
Click to reveal answer
In an array, what is the index of the first element?
ADepends on the array size
B1
C-1
D0
✗ Incorrect
Array indexes start at 0, so the first element is at index 0.
What happens if you assign a value to an index that is within the array length?
AThe array size increases
BAn error occurs
CThe element at that index is updated
DThe array is cleared
✗ Incorrect
Assigning a value to an existing index updates the element at that position.
What will be the result of accessing an index outside the array bounds in TypeScript?
Aundefined
BThe last element
CAn error is thrown
DThe first element
✗ Incorrect
Accessing an out-of-bounds index returns undefined in TypeScript.
How do you update the value at index 4 in an array named 'items'?
Aitems[4] = newValue;
Bitems.update(4, newValue);
Citems.add(4, newValue);
Ditems.set(4, newValue);
✗ Incorrect
Direct assignment using the index updates the element in TypeScript arrays.
Which of these is a valid way to access the second element of an array 'arr'?
Aarr.second()
Barr[1]
Carr.get(1)
Darr[2]
✗ Incorrect
Array indexes start at 0, so the second element is at index 1.
Explain how to access and update elements in an array using their index.
Think about how you find and change items in a list by their position.
You got /4 concepts.
Describe what happens if you try to access or update an array element at an index that does not exist.
Consider what happens when you look for something beyond the end of a list.
You got /4 concepts.
Practice
(1/5)
1. What does the expression arr[2] do in an array arr?
easy
A. Accesses the element at index 2 of the array
B. Adds a new element at index 2
C. Removes the element at index 2
D. Returns the length of the array
Solution
Step 1: Understand array indexing
Arrays use zero-based indexing, so index 2 means the third element.
Step 2: Access element at index 2
The expression arr[2] retrieves the element stored at the third position.
Final Answer:
Accesses the element at index 2 of the array -> Option A
Quick Check:
arr[2] means get element at index 2 [OK]
Hint: Remember arrays start at 0, so index 2 is third element [OK]
Common Mistakes:
Thinking index 2 adds or removes elements
Confusing index with length
Assuming indexing starts at 1
2. Which of the following is the correct way to update the element at index 1 in a TypeScript array nums to 10?
easy
A. nums.add(1, 10);
B. nums.update(1, 10);
C. nums.set(1, 10);
D. nums[1] = 10;
Solution
Step 1: Recall array element update syntax
In TypeScript, to update an element at a specific index, use array[index] = value;.
Step 2: Apply to given array
So, nums[1] = 10; correctly updates the element at index 1 to 10.
Final Answer:
nums[1] = 10; -> Option D
Quick Check:
Use bracket notation with assignment for update [OK]
Hint: Use bracket and equals sign to update array element [OK]
Common Mistakes:
Using non-existent methods like update(), set(), add()
Confusing update syntax with other languages
Trying to use push() to update element
3. What will be the output of the following TypeScript code?
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.