ITPEC FE Subject B October 2024 Question 3

Source exam: ITPEC FE Subject B October 2024Topic: Array Manipulation

ITPEC FE Subject B October 2024 — Question 3 of 20

Array Manipulation — finding the second-largest element by tracking two maximums.

The program maintains two variables: max1 (largest so far) and max2 (second-largest so far), both initialized to -∞.

For each element array[i]:
- Blank A: array[i] > max1 — if the current element exceeds the largest, the old max1 is demoted to max2, and max1 takes the new value.

- Blank B: max2 ← array[i] — if the element doesn't beat max1 but beats max2, it becomes the new second-largest.

Trace with {2, 6, 9, 1, 7, 5}:

iarray[i]max1max2
122-∞
2662
3996
4196
5797
6597

Output: max2 = 7

Why not others:
- (a) array[i] < max1 — entering the "new max" branch when the element is smaller makes no sense

- (b) Same wrong condition as (a), plus B updates max2 instead of promoting correctly

- (c)/(d) array[i] < max2 — triggers on values smaller than the second-max, which is irrelevant

- (e) Correct condition for A, but B sets max1 ← array[i]max1 is already set on the next line, so this overwrites max2 incorrectly

- (g)/(h) array[i] > max2 — this is the elseif condition, not the primary if condition

Key rule: To find the k-th largest, maintain k tracking variables; when a new maximum is found, cascade the old values down.

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.