IPA FE Subject B 2025 Question 3

Topic: Stack

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.

Index1234stack43stackPos
Note:Shading indicates that the element is undefined.

Figure Initial state of the stack

[Program]
global: integer: stackPos ← 3
global: integer []: stack ← {4, 3, undefined, undefined}
○ boolean: push(integer: inputData)
if (stackPos ≤ the number of elements in stack)
stack[A] ← inputData
stackPos ← stackPos + 1
return true
else
return false
endif
○ integer: pop()
integer: popData ← undefined
if (stackPos > 1)
stackPos ← B
popData ← stack[stackPos]
stack[stackPos] ← undefined
endif
return popData

Answer group

OptionAB

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[stackPos] 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[stackPos] 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, stackPos becomes 4, returns true
  • •pop(): stackPos becomes 3, returns 5 and clears stack[3]
  • •two more pop() calls return 3 and then 4, leaving stackPos at 1; a further pop() returns the undefined value
  • •four push calls then fill elements 1 to 4 (stackPos 5), and a fifth returns false

No index ever leaves 1 to 4.

Answer (b)

Why not others:

  • •(a) — B stackPos + 1 moves away from the top: the first pop() from the initial state reads stack[4], which is undefined, instead of 3, and the next one sets stackPos to 5 and reads outside the array
  • •(c) and (d) — A stackPos - 1 writes over the top value: push(5) puts 5 into stack[2] and the 3 is lost, and on an empty stack (stackPos 1) it writes to stack[0], outside the array. After that push(5), the pop() of (c) reads stack[5], outside the array, and the pop() 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.