53 exam-style questions on Programming algorithms — topic B2.4 of Programming in the IB Computer Science syllabus — with answers and explanations. Samples below; the full set is free with an account.
A program must find one surname in an array of 1000 surnames. Which condition has to hold before a binary search may be used on that array?
Answer: The array must already be sorted on the surname
A binary search decides after every comparison which half of the array can be thrown away, and that decision is only valid if the values are in order. Repeated values, the index base and the size of the array make no difference to whether the method works.
A sorted array holds 16 values. What is the greatest number of values a binary search has to examine before it can decide whether a given value is present?
Answer: 5
Each comparison at most halves the part still to be searched: 16 to 8 to 4 to 2 to 1, and the single remaining value must itself be compared, giving five examinations. Stopping at four counts the halvings and forgets that the last survivor still has to be checked.
Which statement correctly distinguishes a selection sort from a bubble sort?
Answer: A selection sort makes at most one exchange per pass
A selection sort searches the unsorted part for the smallest remaining item and swaps it into place once per pass, while a bubble sort may swap many times in the same pass. Both work in the original array and both compare pairs, so the other statements describe neither method.
A list of readings arrives almost in ascending order, with only two readings out of place. Which behaviour of an insertion sort makes it a good choice here?
Answer: It stops moving an item as soon as it meets a smaller one
An insertion sort walks each new item backwards only until it meets a smaller item, so on almost ordered data nearly every item stops immediately and hardly anything moves. It is the selection sort whose comparison count is fixed by the length alone.
A recursive subprogram is written without a base case. What happens when it is called?
Answer: It calls itself again and again until the call stack is exhausted
The base case is the only thing that stops a recursive definition, so without it every call makes another call and the memory holding the unfinished calls fills up. The definition is syntactically fine, which is why the problem only shows itself at run time.
A linear search runs over an array that happens to be sorted in ascending order. When may the search stop early even though the target has not been found?
Answer: As soon as a value greater than the target is reached
Once a larger value appears, every remaining value is at least as large, so the target cannot be further on and the search can report failure immediately. This shortcut is only available because the data are sorted, which is also the condition that makes a binary search possible and far faster.
That was 6 of 53 Programming algorithms questions.
Practise all 53 freeOutlineAO1 · 1–2 marks
A brief account or summary: the main points in two or three sentences, no evaluation.
DistinguishAO2 · 2–4 marks
Make the difference clear for BOTH terms — a definition of one of them is only half an answer.
ConstructAO2 · 2–4 marks
Build it as instructed — a table, a diagram, an argument — with every required element present.
53 Programming algorithms 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.