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 console.log output
Logging stack prints the array as is: [10, 20, 30].
Final Answer:
[10, 20, 30] -> Option C
Quick Check:
Push adds at end, so stack = [10,20,30] [OK]
Hint: Push adds at end, array grows left to right [OK]
Common Mistakes:
Assuming push adds at start (top as front)
Confusing stack order with queue
Expecting reversed array output
4. Identify the error in the following code snippet that tries to push an element into a stack:
let stack = 5;
stack.push(10);
medium
A. stack is not an array, so push method is undefined
B. push method requires two arguments
C. stack.push should be stack.add
D. No error, code runs fine
Solution
Step 1: Check variable type of stack
stack is assigned number 5, not an array. Numbers do not have push method.
Step 2: Understand push method applicability
push is an array method. Calling push on a number causes error: push is not a function.
Final Answer:
stack is not an array, so push method is undefined -> Option A
Quick Check:
push only works on arrays [OK]
Hint: Only arrays have push method [OK]
Common Mistakes:
Assuming push works on numbers
Thinking push needs multiple arguments
Confusing push with add method
5. Given a stack implemented as an array, how would you push multiple elements [1, 2, 3] onto it so that 3 is on top after all pushes? Choose the correct code snippet.
hard
A. stack.push(1); stack.push(2); stack.push(3);
B. stack.push(3); stack.push(2); stack.push(1);
C. stack.push([1, 2, 3]);
D. stack = [1, 2, 3];
Solution
Step 1: Understand push order for stack
Push adds elements on top. To have 3 on top, push 1 first, then 2, then 3.
Step 2: Analyze options
Sequential pushes of 1 then 2 then 3 add elements individually with 3 on top. Pushing [1,2,3] adds a single array element with [1,2,3] on top. Reverse pushes put 1 on top. Assignment replaces the stack entirely.
Final Answer:
stack.push(1); stack.push(2); stack.push(3); -> Option A
Quick Check:
Push in order to have last element on top [OK]
Hint: Push elements in order, last pushed is top [OK]
Common Mistakes:
Pushing array [1,2,3] as single element
Pushing in reverse order
Replacing stack instead of pushing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.