40 exam-style questions on Programming constructs — topic B2.3 of Programming in the IB Computer Science syllabus — with answers and explanations. Samples below; the full set is free with an account.
A program reads values until a negative value is entered, and it must also cope with the very first value already being negative by doing nothing at all. Which loop construct guarantees that behaviour?
Answer: loop while, because its condition is tested before the body runs
Only a pre-tested loop can run its body zero times, which is exactly what the first-value case demands. A loop until tests after the body, so it always processes at least one value, and that zero-iteration case is the usual oversight.
A routine must place every mark in exactly one of four grade bands. Which construct guarantees this, whatever the mark?
Answer: A chain of if ... then ... else if ... else statements
In an if ... else if chain the later tests are only reached when every earlier test failed, so at most one branch runs and the final else catches everything left over. Four independent if statements can all be true at once if the boundaries overlap, which awards two grades to the same mark.
An algorithm must display a duration held in the variable SECONDS as a number of whole minutes followed by the seconds left over. Which pair of expressions gives those two values in that order?
Answer: SECONDS div 60 and SECONDS mod 60
div discards the remainder and so counts whole groups of 60, while mod keeps only the remainder and so gives the seconds that do not fill a minute. Swapping the two operators is the commonest error and would report 47 minutes and 1 second for 107 seconds.
A subprogram is called from inside a larger expression, so that its result is used immediately in a calculation. What must that subprogram do?
Answer: Return a value to the point at which it was called
A subprogram that returns a value can stand wherever a value is expected, which is why it can appear inside an expression; one that only outputs or stores a result cannot, because the expression has nothing to work with. The order of declaration is a convention of particular languages and is not what decides this.
A variable that can be used only inside the subprogram in which it is declared is described as ___. (one word)
Answer: local
Restricting a variable to one subprogram means another subprogram can reuse the same name without interference, which is what makes subprograms independently testable. A variable declared outside every subprogram is global and can be changed from anywhere.
Two loops of which one is written inside the body of the other are described as ___. (one word)
Answer: nested
The inner loop is carried out in full during every single pass of the outer loop, which is why the total number of passes is the product of the two loop lengths rather than their sum.
That was 6 of 40 Programming constructs questions.
Practise all 40 freeTraceAO2 · 2–4 marks
Follow the algorithm step by step in a trace table, one row per pass, showing every variable that changes.
ExplainAO2 · 2–4 marks
Give reasons or causes: say what happens AND why, linking each step with 'because' or 'so that'.
DistinguishAO2 · 2–4 marks
Make the difference clear for BOTH terms — a definition of one of them is only half an answer.
40 Programming constructs questions — free with an account. Spaced repetition, streaks and full exam simulations included.
Practise them freeThis platform is independently developed and is not endorsed by, affiliated with, or sponsored by the International Baccalaureate Organization. “International Baccalaureate” and “IB” are registered trademarks of the IBO.