DigiBacc

Practice questions › IB Computer Science › Programming › Programming constructs
IB Computer Science · Programming

Programming constructs practice questions.

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.

At a glance

About this topic

Syllabus code
B2.3
Subject
IB Computer Science
Topic
B2 · Programming
Questions
40 with worked answers
Question types
13 multiple-choice questions · 9 fill-in-the-gap questions · 8 flashcards · 4 matching tasks · 4 true/false questions · 2 structured questions
Try it

Sample questions on Programming constructs

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?

  • loop until, because its condition is tested after the body runs
  • loop while, because its condition is tested before the body runs
  • loop for, because the number of repetitions is fixed in advance
  • loop until, because it stops as soon as the condition becomes true

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?

  • Four separate if statements, one for each band
  • A loop while that repeats until a band matches the mark
  • A chain of if ... then ... else if ... else statements
  • A loop for that steps through the four bands in turn

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?

  • SECONDS div 60 and SECONDS mod 60
  • SECONDS mod 60 and SECONDS div 60
  • SECONDS div 100 and SECONDS mod 100
  • SECONDS div 60 and SECONDS div 3600

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?

  • Store its result in a variable that is global to the program
  • Be declared before every other subprogram in the listing
  • Return a value to the point at which it was called
  • Output its result before the calculation continues

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 free
Exam tips

Command terms in this topic

Keep going

More topics in Programming

All of Programming →

40 Programming constructs questions — free with an account. Spaced repetition, streaks and full exam simulations included.

Practise them free

This 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.