IPA FE Subject B 2023 Question 2

Topic: Stack

A question IPA Japan published in 令和5年度 基本情報技術者試験 科目B 公開問題 問2, translated into English. ITPEC is based on the same exams.

From the answer group below, select the correct answer to be inserted into blank in the description.

In the following program, when the procedure proc2 is called, blank are output in this order.

[Program]
○ proc1()
output "A"
proc3()
○ proc2()
proc3()
output "B"
proc1()
○ proc3()
output "C"

Answer group

Explanation

“C”, “B”, “A”, “C” — a called procedure runs to its end before the caller goes on to its next statement.

Tracing the call of proc2:

  1. proc2 first calls proc3, which outputs C
  2. back in proc2, it outputs B
  3. proc2 then calls proc1, which outputs A
  4. proc1 then calls proc3, which outputs C again

proc3 runs twice, once from proc2 and once from proc1, so four values are output in all.

Answer (h)

Why not others:

  • •(a), (b) and (c) — start with A, but proc1, the only procedure that outputs A, is called last by proc2
  • •(d) and (e) — start with B, but proc2 outputs B only after its first statement, the call of proc3, has output C
  • •(f) and (g) — stop early, leaving out the output of proc1 or of the proc3 it calls at its end

Key rule: Read each call as "pause here, run the whole procedure, come back", and follow the statements in order inside every procedure, including the calls a called procedure makes.

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.