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?

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?

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?

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?

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?

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?

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()?

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?

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)?

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?

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
Stacks MCQ Practice Test | Mind Hustle