ITPEC FE Subject B April 2024 Question 13
From the answer group below, select the correct combination of answers to be inserted into A through C in the program. Here, the array index starts at 1.
The function are_brackets_balanced checks for balanced brackets. It parses the given array of characters and when an opening bracket (“(”, “[”, “{”) is encountered, this is pushed onto the stack. When a closing bracket (“)”, “]”, “}”) is encountered, an element is popped from the stack and tested if it corresponds to the opening bracket. If the closing bracket matches its corresponding opening bracket, the process continues. Otherwise, it fails and the function returns false. After all characters have been processed, it returns false if any characters remain on the stack, otherwise it returns true. For simplicity, only brackets are considered as arguments to the function. The table shows examples of arguments provided to are_brackets_balanced and the return values.
Table Examples of arguments provided to the function are_brackets_balanced and the return values
The function are_brackets_balanced uses class Stack. The figure describes class Stack.
Figure Class Stack
Answer group
Explanation
A: ≠ c, B: stack.isEmpty(), C: chars[2] — a closing bracket must be the partner of the last opening bracket, and no opening bracket may be left over at the end.
Each row of brackets is a pair {opening, closing}, so chars[1] is the opening and chars[2] the closing bracket. get_closing_bracket finds the row whose chars[1] is the popped opening bracket and must return its partner, chars[2] (blank C).
When a closing bracket c arrives, the partner of the popped opening bracket must equal c; the function fails when it does not, so blank A is ≠ c.
After the loop, every matched pair has been popped. Anything still on the stack is an opening bracket that was never closed, so the result is true only when the stack is empty: return stack. (blank B). The three table examples:
- •
(,{,},),[,]: every closing bracket matches its popped partner and the stack ends empty, sotrue - •
(,{,},[,]: all matches succeed but(is left on the stack, sofalse - •
(,{,), …:)pops{, whose partner}differs from), sofalse
Answer (h)
Why not others:
- •(a), (b), (c) and (d) — A as
= cfails on a correct match: the first example returnsfalseas soon as}closes{ - •(a), (b), (e) and (f) — B as
not stack.inverts the final test: a balanced expression returnsisEmpty( ) falseand one with an unclosed(returnstrue - •(a), (c), (e) and (g) — C as
chars[1]returns the opening bracket itself, which never equals a closing bracket, so with≠ cany expression that closes a bracket fails
Key rule: With a stack, a closing bracket must match the most recently opened one, and the input is balanced only if the stack is empty once every character has been read.
Stack · AI-written, may contain errors
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.