ITPEC FE Morning October 2023 Question 7
ITPEC FE Morning October 2023 — Question 7 of 80
Stack Operations (PUSH / POP / READ) — trace stack state to find what the last READ returns.
Given operations on an empty stack:
PUSH 2 → READ → PUSH 3 → PUSH 6 → POP → READ → PUSH 4 → READ → PUSH 7 → PUSH 5 → POP → POP → READ
Step-by-step trace (top of stack on the right):
- •
PUSH 2→[2] - •
READ→ peek2 - •
PUSH 3→[2, 3] - •
PUSH 6→[2, 3, 6] - •
POP→ remove6→[2, 3] - •
READ→ peek3 - •
PUSH 4→[2, 3, 4] - •
READ→ peek4 - •
PUSH 7→[2, 3, 4, 7] - •
PUSH 5→[2, 3, 4, 7, 5] - •
POP→ remove5→[2, 3, 4, 7] - •
POP→ remove7→[2, 3, 4] - •
READ→ peek `4`
Answer: c) 4
Why not others:
- (a) 2 — sits at the bottom of the stack, never reached by the last READ
- (b) 3 — below 4 at the time of the last READ
- (d) 6 — was already removed by POP in step 5
Key rule: READ (peek) returns the top element without removing it; POP removes the top element. Trace the full sequence to know what sits on top at any given moment.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.