Elements are added at the end; top is last element (30).
Final Answer:
[10, 20, 30] -> Option C
Quick Check:
Stack after pushes = [10, 20, 30] [OK]
Hint: Append adds at end; last element is top [OK]
Common Mistakes:
Reversing the order of elements
Confusing stack top with bottom
Assuming append adds at start
4. Identify the error in the following code that tries to push an element 15 into a stack implemented as a list:
stack = [5, 10]
stack.push(15)
medium
A. Missing import statement for stack
B. Using push() method which does not exist for list
C. Stack should be initialized as a tuple
D. Using append() instead of push()
Solution
Step 1: Check list methods for adding elements
Python lists do not have a push() method; they use append().
Step 2: Analyze the code error
Calling stack.push(15) will cause an AttributeError because push is undefined.
Final Answer:
Using push() method which does not exist for list -> Option B
Quick Check:
List has append(), not push() [OK]
Hint: Use append() for list push, not push() [OK]
Common Mistakes:
Assuming push() exists for Python lists
Thinking stack must be tuple
Ignoring AttributeError on push()
5. Given a stack implemented as a list, how would you push multiple elements [1, 2, 3] onto the stack so that 3 is on top after all pushes?
hard
A. Use a loop to append elements in order: 1, then 2, then 3
B. Use a loop to append elements in reverse order: 3, then 2, then 1
C. Use extend() method with reversed([1, 2, 3])
D. Insert elements at index 0 in order: 1, then 2, then 3
Solution
Step 1: Understand stack push order
To have 3 on top, push 1 first, then 2, then 3 so 3 is last pushed.
Step 2: Analyze each option
Loop append in order 1,2,3 puts 3 on top. Reverse order puts 1 on top. extend(reversed([1,2,3])) adds 3,2,1 so 1 on top. Insert at index 0 in order ends with [3,2,1], 1 on top.
Final Answer:
Use a loop to append elements in order: 1, then 2, then 3 -> Option A
Quick Check:
Push in order to get last element on top [OK]
Hint: Push elements in order to have last on top [OK]
Common Mistakes:
Pushing in reverse order causing wrong top
Using insert(0, val) which adds at bottom
Using extend(reversed([1, 2, 3])) which puts wrong order on top
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.