IPA FE Subject B 2025 Question 3
A question IPA Japan published in 令和7年度 基本情報技術者試験 科目B 公開問題 問3, translated into English. ITPEC is based on the same exams.
From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.
The function push stores the integer given as an argument in the stack. It returns true if the integer could be stored, and returns false if it could not be stored.
The function pop takes out a value from the stack and returns it. When the stack is empty, it returns the undefined value.
The stack is represented by the global integer-type array stack, whose number of elements is 4, and the global variable stackPos, which indicates the position where a value is stored next. The initial state of the stack is shown in the figure.
In the program, areas outside of the array must not be referenced.
Figure Initial state of the stack
Answer group
Explanation
A: stackPos, B: stackPos - 1 — stackPos points at the next free element, so push writes there and then moves up, and pop first steps back to the top value and then reads it.
In the figure, elements 1 and 2 hold 4 and 3, and stackPos is 3, the first undefined element: the position where the next value is stored. So push stores inputData in stack[ and then adds 1; A is stackPos. The top value always sits one element below stackPos, so pop must first set stackPos to stackPos - 1, then read stack[ and clear it; B is stackPos - 1. The test stackPos > 1 means the stack is not empty, and stackPos ≤ 4 means it is not full.
Trace from the initial state:
- •
push(:5) stack[3]← 5,stackPosbecomes 4, returnstrue - •
pop(:) stackPosbecomes 3, returns 5 and clearsstack[3] - •two more
pop(calls return 3 and then 4, leaving) stackPosat 1; a furtherpop(returns the undefined value) - •four
pushcalls then fill elements 1 to 4 (stackPos5), and a fifth returnsfalse
No index ever leaves 1 to 4.
Answer (b)
Why not others:
- •(a) — B
stackPos + 1moves away from the top: the firstpop(from the initial state reads) stack[4], which is undefined, instead of 3, and the next one setsstackPosto 5 and reads outside the array - •(c) and (d) — A
stackPos - 1writes over the top value:push(puts 5 into5) stack[2]and the 3 is lost, and on an empty stack (stackPos1) it writes tostack[0], outside the array. After thatpush(, the5) pop(of (c) reads) stack[5], outside the array, and thepop(of (d) reads the undefined) stack[3]
Key rule: Decide whether the stack pointer marks the next free slot or the top item, then make push and pop mirror each other: with a next-free pointer, push writes then increments, pop decrements then reads.
Stack · AI-written, may contain errors
This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.