programming
beginner
10 sample questions
Stacks MCQ Practice Test
LIFO (Last-In-First-Out) operations like push and pop.
Q1. A stack-based function evaluator encounters the expression "2 3 + 5 *" (using postfix notation). Assuming the stack initially contains only the integer 1, what is the top element of the stack after the evaluation is complete?
-
A. 17
-
B. 25 ✓
-
C. 16
-
D. 11
Explanation: Starting stack [1]. Push 2 and 3 → [1, 2, 3]; '+' pops 2 and 3 and pushes 5 → [1, 5]; push 5 → [1, 5, 5]; '*' pops both 5s and pushes 25 → [1, 25]. The top element is 25 — the initial 1 sits untouched below because each operator consumes only its own two operands. The old key '17' (from '16 + 1') was a miscalculation, as the old explanation itself demonstrated before contradicting it.
Q2. A stack is used to keep track of function calls in a program. Imagine a scenario where functions A, B, and C are called sequentially, with B calling C. Which function's return will be processed "last" by the program, according to the Last-In, First-Out (LIFO) nature of the stack?
-
A. Function A
-
B. Function B
-
C. Function C ✓
-
D. The order doesn't matter with a stack
Explanation: The stack follows a LIFO (Last-In, First-Out) principle. Function C is called last and placed on top of the stack; therefore, it will be the last function to return and have its processing completed.
Q3. Consider a stack-based language interpreter where function calls are implemented using a stack to manage the function's activation records. Each activation record contains the function's local variables, return address, and arguments. A function 'foo' calls function 'bar', which in turn calls 'baz'. 'baz' returns to 'bar', which then returns to 'foo'. If a runtime error occurs within 'bar', causing an immediate program termination (without unwinding the stack properly), which of the following statements most accurately describes the state of the stack at the point of termination?
-
A. The stack will contain only the activation record for 'bar'.
-
B. The stack will contain the activation records for 'foo', 'bar', and 'baz', in that order (top to bottom).
-
C. The stack will contain the activation records for 'foo' and 'bar', in that order (top to bottom). ✓
-
D. The stack's state is unpredictable and may contain partial activation records.
Explanation: When the runtime error occurs in 'bar', 'baz' has already returned, so its frame was popped. The stack holds 'bar' (top) above 'foo'.
Q4. A stack is implemented using a linked list. Each node contains an integer value and a reference to the next node. What is the correct way to implement the pop operation?
-
A. Create a new node and copy the value of the current top node into it, then update the top reference to point to the new node.
-
B. Update the value of the current top node with the value of the next node.
-
C. Store the value of the current top node, update the top reference to point to the next node, and then return the stored value. ✓
-
D. Delete the current top node and update the top reference to point to the node after the current top node.
Explanation: The pop operation in a stack implemented with a linked list involves removing the top element. This is done by first storing the value of the top node. Then, the top reference is updated to point to the next node in the list, effectively removing the original top node. Finally, the stored value is returned, representing the value that was popped from the stack.
Q5. A stack is implemented as a linked list with a pointer to the top element. When the stack is empty, the pointer points to a sentinel node with a value of -1. What is the purpose of this sentinel node?
-
A. to indicate the end of the stack ✓
-
B. to store the minimum element
-
C. to simplify stack traversal
-
D. to handle push and pop operations
Explanation: The sentinel node is used to indicate the end of the stack when it's empty, allowing for easier handling of stack operations.
Q6. In a stack implementation, what is the correct order of operations when a new element is pushed onto the stack and the top element is immediately popped?
-
A. The new element is added at the beginning of the stack and the top element is removed first.
-
B. The new element is added at the end of the stack and the top element is removed last.
-
C. The new element is added at the end of the stack and the top element is removed first. ✓
-
D. The new element is added at the beginning of the stack and the top element is removed last.
Explanation: When a new element is pushed onto a stack and the top element is immediately popped, the new element is added to the end of the stack. The top element is then removed first, as it was the most recently added element.
Q7. What are the contents of the stack after the following operations: push(5), push(3), pop(), push(7), pop()?
-
A. The stack contains only the value 7 ✓
-
B. The stack contains the values 5 and 7
-
C. The stack is empty
-
D. The stack contains only the value 3
Explanation: After pushing 5 and 3, the pop() operation removes 3. Then, 7 is pushed. The final pop() removes 7, leaving only 5 in the stack. However, the question is asking about the contents of the stack *after* the operations, so the stack should contain 7.
Q8. Given a stack implementation with the following operations: push, pop, top, and isEmpty, which of the following is a correct implementation of the top operation?
-
A. return stack[stack.length - 1]; ✓
-
B. return stack[stack.length];
-
C. return stack.pop();
-
D. return stack.push()
Explanation: The top operation in a stack returns the top element without removing it. This can be achieved by accessing the last element of the array, which is at index stack.length - 1.
Q9. A stack is implemented using a linked list. When a new element is pushed onto the stack, the linked list is inserted at the beginning. Which of the following correctly describes the order of elements in the linked list after the following sequence of operations: push(5), push(10), pop(), push(7)?
-
A. The linked list is in the order: 5, 10, 7
-
B. The linked list is in the order: 10, 7, 5
-
C. The linked list is in the order: 5, 7, 10
-
D. The linked list is in the order: 10, 5, 7 ✓
Explanation: When a new element is pushed onto the stack, it is added to the beginning of the linked list. When an element is popped from the stack, it is removed from the beginning of the linked list. Therefore, after the sequence of operations, the linked list is in the order: 10, 5, 7.
Q10. Given a stack implementation with the following push and pop operations, which of the following is the correct implementation of a function to check if an element is at the top of the stack without popping it?
-
A. function isAtTop(stack, element) { while (stack.size() > 1) stack.pop(); return stack.contains(element); }
-
B. function isAtTop(stack, element) { return stack.peek() == element; } ✓
-
C. function isAtTop(stack, element) { stack.push(element); return true; }
-
D. function isAtTop(stack, element) { stack.pop(); return stack.peek() == element; }
Explanation: This function works by checking if the top element of the stack matches the given element. It does not modify the stack in any way, thus preserving its original state. The other options either modify the stack or do not correctly check if the element is at the top.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free