Initially, stack is empty []. After push(10), stack = [10]. After push(20), stack = [10, 20]. After push(30), stack = [10, 20, 30].
Step 2: Understand stack order
The first pushed element is at index 0, last pushed at the end. So the array shows bottom to top as left to right.
Final Answer:
[10, 20, 30] -> Option B
Quick Check:
Push order = [10, 20, 30] [OK]
Hint: Stack array shows bottom to top left to right [OK]
Common Mistakes:
Reversing the order of elements
Confusing push with pop output
Assuming push adds at start of array
4. Identify the error in the following TypeScript code that tries to push an element into a stack:
let stack: number[] = [1, 2, 3];
stack.push 4;
medium
A. Missing parentheses after push
B. Stack should be declared as const
C. push cannot be used on arrays
D. Missing semicolon after array declaration
Solution
Step 1: Check push method syntax
The push method requires parentheses to pass the element: stack.push(4);. The code uses stack.push 4; which is invalid.
Step 2: Verify other parts
Declaring stack with let is valid. push is valid on arrays. Semicolon is optional but not an error here.
Final Answer:
Missing parentheses after push -> Option A
Quick Check:
push() needs parentheses [OK]
Hint: Always use parentheses with push method [OK]
Common Mistakes:
Omitting parentheses on method calls
Thinking push is a property, not a method
Confusing let and const errors
5. Given a stack implemented as an array, how can you push multiple elements [4, 5, 6] onto the stack in one operation while preserving their order (4 at bottom, 6 at top)?
hard
A. Use stack.unshift(...[4, 5, 6])
B. Use stack.push([4, 5, 6])
C. Use a loop to push each element from left to right
D. Use stack.push(...[4, 5, 6])
Solution
Step 1: Understand spread operator with push
Using stack.push(...[4, 5, 6]) spreads the array elements as separate arguments, pushing 4, then 5, then 6 in order.
Step 2: Compare other options
stack.push([4, 5, 6]) pushes the entire array as one element, not separate elements. Looping works but is multiple operations, not one. unshift adds elements at the start, not top of stack.
Final Answer:
Use stack.push(...[4, 5, 6]) -> Option D
Quick Check:
Spread operator with push adds multiple elements [OK]
Hint: Use spread operator ... with push for multiple elements [OK]
Common Mistakes:
Pushing array as single element instead of separate
Using unshift which adds at bottom
Forgetting spread operator syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.