Episode 1 — Fundamentals / 1.21 — Arrays
1.21 -- Exercise Questions: JavaScript Arrays
Practice questions for all seven subtopics in Section 1.21. Mix of short-answer, prediction, code-writing, and debugging tasks.
How to use this material (instructions)
- Read lessons in order --
README.md, then1.21.a->1.21.g. - Answer closed-book first -- then compare to the matching lesson.
- Use the Console -- run code snippets in DevTools or Node.js to verify.
- Interview prep --
1.21-Interview-Questions.md. - Quick review --
1.21-Quick-Revision.md.
1.21.a -- Creating Arrays (Q1--Q10)
Q1. Write two ways to create an array containing the numbers 1, 2, and 3.
Q2. What does new Array(5) create? How is it different from [5]?
Q3. What does Array.of(5) return? Why was this method introduced?
Q4. Write a line that creates an array of the characters in the string "hello" using Array.from().
Q5. What does typeof [] return? How do you reliably check if a value is an array?
Q6. Predict the output:
const a = [1, 2, 3];
const b = a;
b.push(4);
console.log(a);
Q7. Explain why [1, 2, 3] === [1, 2, 3] evaluates to false.
Q8. Can you modify the contents of an array declared with const? Explain.
Q9. What is a sparse array? Give one way to accidentally create one.
Q10. Write a line that creates an array of 10 zeros using fill().
1.21.b -- Accessing Elements (Q11--Q20)
Q11. What is the index of the first element in a JavaScript array?
Q12. Given const arr = ["a", "b", "c", "d", "e"], write two ways to access the last element.
Q13. What does arr.at(-2) return for ["x", "y", "z"]?
Q14. What happens when you access arr[100] on a 3-element array? Is an error thrown?
Q15. Write destructuring that extracts the first two elements and collects the rest:
const nums = [10, 20, 30, 40, 50];
// const [?, ?, ...?] = nums;
Q16. Write a one-line swap of arr[0] and arr[2] using destructuring.
Q17. What is the difference between indexOf() and includes()?
Q18. Why does [NaN].indexOf(NaN) return -1?
Q19. Write a line using find() that returns the first number greater than 50 from [10, 30, 60, 80].
Q20. What does findIndex() return when no element matches the condition?
1.21.c -- Array Operations (Q21--Q32)
Q21. What does push() return -- the array or something else?
Q22. Write code that removes and logs the last element of [10, 20, 30].
Q23. What is the time complexity of unshift() and why?
Q24. Predict the output:
const arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
console.log(arr.length);
Q25. Name two ways to merge [1, 2] and [3, 4] into [1, 2, 3, 4].
Q26. What is the key difference between concat() and spread [...a, ...b] regarding iterables?
Q27. Does reverse() mutate the original array? How do you reverse without mutating?
Q28. Predict the output:
const arr = new Array(3).fill([]);
arr[0].push("hello");
console.log(arr);
Q29. What does copyWithin(0, 2) do to [1, 2, 3, 4, 5]?
Q30. Classify each as mutating or non-mutating: push, concat, reverse, pop, slice.
Q31. Implement a stack using array operations. Show push and pop.
Q32. Implement a queue using array operations. Show enqueue and dequeue.
1.21.d -- Length Property (Q33--Q40)
Q33. Is length a method or a property?
Q34. What does setting arr.length = 0 do?
Q35. Predict the output:
const arr = ["a", "b", "c", "d", "e"];
arr.length = 2;
console.log(arr);
console.log(arr[3]);
Q36. You write const a = []; a[99] = "end"; -- what is a.length?
Q37. Why is if ([]) { ... } always truthy, even for an empty array?
Q38. Write a correct check for an empty array.
Q39. Describe two ways to completely empty an array that is referenced by another variable (so the other variable also sees the change).
Q40. In a for loop, what is the maximum valid index for an array of length n?
1.21.e -- Basic Iteration (Q41--Q50)
Q41. Write a for loop that prints each element of ["a", "b", "c"] with its index.
Q42. Rewrite Q41 using for...of with entries().
Q43. Name three problems with using for...in on an array.
Q44. Can you use break inside a forEach callback? Explain.
Q45. Write a while loop that removes and prints elements from an array until it is empty.
Q46. Why should you iterate backwards when removing elements from an array by index?
Q47. Write code that sums only the positive numbers in [3, -1, 4, -5, 2] using a for...of loop.
Q48. What does arr.keys() return? What about arr.values()?
Q49. Predict the output:
const arr = [10, 20, 30];
for (const key in arr) {
console.log(typeof key);
}
Q50. Write a function linearSearch(arr, target) that returns the index of target or -1.
1.21.f -- Practice Problems (Q51--Q56)
Q51. Write a function sum(arr) that returns the sum of all numbers in an array.
Q52. Write a function countOccurrences(arr, value) that counts how many times value appears.
Q53. Write a function removeDuplicates(arr) using a Set.
Q54. Write a function reverseArray(arr) without using .reverse() -- swap elements from both ends.
Q55. Merge [1, 3, 5] and [2, 4, 6] into a sorted array [1, 2, 3, 4, 5, 6] using the two-pointer technique.
Q56. Write a function secondLargest(arr) that finds the second largest unique value.
1.21.g -- Multidimensional Arrays (Q57--Q66)
Q57. Create a 3x3 matrix of zeros using Array.from().
Q58. Why does new Array(3).fill(new Array(3).fill(0)) create a broken matrix?
Q59. Access the element at row 1, column 2 of:
const m = [[10, 20, 30], [40, 50, 60], [70, 80, 90]];
Q60. Write nested loops to print every element of a 2D array.
Q61. What is a jagged array? Give an example.
Q62. Write a function that sums all elements in a 2D matrix.
Q63. Write a function that transposes a matrix (rows become columns).
Q64. Create a tic-tac-toe board as a 2D array and place "X" in the center.
Q65. Write a function that checks if all elements in a 2D array are equal to a given value.
Q66. How do you get the number of rows and columns in a rectangular 2D array?
Answer hints
| Q | Hint |
|---|---|
| Q2 | new Array(5) creates 5 empty slots (sparse); [5] creates [5] (one element) |
| Q6 | [1, 2, 3, 4] -- arrays are reference types |
| Q7 | Each [] creates a new object; === compares references |
| Q14 | Returns undefined -- no error |
| Q18 | indexOf uses === which says NaN !== NaN |
| Q21 | Returns the new length, not the array |
| Q23 | O(n) -- all elements must shift right |
| Q28 | [["hello"], ["hello"], ["hello"]] -- same reference in all slots |
| Q34 | Empties the array completely |
| Q37 | Arrays are objects; all objects are truthy |
| Q43 | String keys, prototype pollution, non-guaranteed order |
| Q44 | No -- break is a syntax error inside forEach; use for or for...of |
| Q46 | Forward iteration + splice causes index shifting; elements are skipped |
| Q49 | All three print "string" -- for...in keys are always strings |
| Q58 | fill uses the same reference for every slot; mutating one row mutates all |
<- Back to 1.21 -- JavaScript Arrays (README)