Accessing arr[2] returns the third element, which is 30.
Step 2: Update array and check second console.log
Setting arr[2] = 50 changes the third element. Printing arr shows [10, 20, 50, 40].
Final Answer:
30 and then [10, 20, 50, 40] -> Option A
Quick Check:
Index access then update = 30 and then [10, 20, 50, 40] [OK]
Hint: Arrays allow element update by index directly [OK]
Common Mistakes:
Thinking arrays are immutable in TypeScript
Confusing index 2 with index 3
Expecting original array after update
4. Identify the error in the following TypeScript code that tries to add an element to an array:
const numbers = [1, 2, 3];
numbers.add(4);
medium
A. The array must be initialized with 'new Array()' to add elements.
B. The array should be declared with 'let' to add elements.
C. The number 4 must be inside square brackets.
D. Arrays don't have an 'add' method; use 'push' instead.
Solution
Step 1: Check method used on array
The method add does not exist for arrays in TypeScript or JavaScript.
Step 2: Identify correct method to add elements
To add elements, use push method like numbers.push(4).
Final Answer:
Arrays don't have an 'add' method; use 'push' instead. -> Option D
Quick Check:
Use push, not add, to add array elements [OK]
Hint: Use push() to add elements to arrays [OK]
Common Mistakes:
Using non-existent 'add' method
Thinking 'let' is required to modify array contents
Trying to add elements with wrong syntax
5. You need to store a large collection of items where you frequently access elements by index and rarely insert or delete items. Which data structure should you choose and why?
hard
A. Array, because it provides fast index-based access and low memory overhead.
B. Linked List, because it allows fast insertions and deletions.
C. Hash Map, because it stores key-value pairs efficiently.
D. Binary Tree, because it keeps data sorted automatically.
Solution
Step 1: Analyze access and modification needs
Frequent index-based access means we want constant time access by position.
Step 2: Compare data structures for this use case
Arrays provide O(1) access by index and use less memory. Insertions/deletions are rare, so array's slower modification is acceptable.
Final Answer:
Array, because it provides fast index-based access and low memory overhead. -> Option A
Quick Check:
Fast index access + rare changes = Array [OK]
Hint: Choose arrays for fast index access and few modifications [OK]
Common Mistakes:
Choosing linked list despite rare insertions
Confusing hash maps with index-based access
Assuming binary trees are best for all data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.