Programming Constructs and Operators: Question 7
Syllabus 8.1
A training course grades each learner's final score, out of 100, using this pseudocode, with categories nested up to three levels deep.
DECLARE Score : INTEGER
DECLARE Grade : STRING
Score ← 68
IF Score < 40 THEN
Grade ← "Fail"
ELSE
IF Score < 60 THEN
Grade ← "Pass"
ELSE
IF Score < 75 THEN
Grade ← "Merit"
ELSE
Grade ← "Distinction"
ENDIF
ENDIF
ENDIF
OUTPUT Grade
What is output when this algorithm is run?
Show worked solution Hide worked solution
Worked solution
Step 1: Identify the nested structure
There are three levels of nesting here:
- Level 1:
IF Score < 40 THEN ... ELSE ... - Level 2 (inside the level-1
ELSE):IF Score < 60 THEN ... ELSE ... - Level 3 (inside the level-2
ELSE):IF Score < 75 THEN ... ELSE ...
Because each IF sits inside the ELSE of the one before it, exactly one of the four
Grade assignments runs, as soon as one condition is found to be true, none of the deeper
conditions are even reached.
Step 2: Trace with Score = 68
- Level 1:
68 < 40? False → move into theELSEbranch (level 2). - Level 2:
68 < 60? False → move into theELSEbranch (level 3). - Level 3:
68 < 75? True →Grade ← "Merit".
Because the level-3 condition was true, execution never reaches the final ELSE (which would
assign "Distinction").
Step 3: Confirm the output
OUTPUT Grade prints:
Merit
This is option A.
Step 4: Why the other options are wrong
- Option B (“Pass”) assumes that failing the first two conditions is enough on its own to
reach the “Pass” branch. But “Pass” is only assigned when
Score < 60is true. ForScore = 68that condition is false, so the algorithm must continue into the third, innermostIFinstead of stopping at “Pass”. - Option C (“Distinction”) assumes that reaching the innermost
ELSEstructure automatically gives “Distinction”, but the innermostIF Score < 75 THENmust be checked first, since68 < 75is true,Gradeis set to"Merit"and theDistinctionbranch is never reached. - Option D (“Fail”) comes from misreading the very first condition,
Score < 40. Since68 < 40is false, theFailbranch is skipped immediately, before any of the other conditions are even considered.
Final answer
- The algorithm outputs “Merit”, option A.