← Free sample
Free sample · B2 Programming · extended exam practice

Fifteen exam-style question sets covering every B2 lesson, including HL recursion. This is the members’ workbook in full — only the worked answer section at the back is held back.

Get full access
IB Computer Science · Theme B · Programming

B2 Extended Exam Practice Workbook

Every B2 lesson, one full exam-style question set each, with tracing, code-writing and evaluation practice. In the members’ edition the worked answers are collected in a reference section at the back; this free sample carries the complete question set. Section 15 (marked HL) is Higher Level only. All code is Python.

Contents

1 Input, Output & Variables · 2 Numeric Types & Operations · 3 String Types & Operations · 4 Arrays & Lists · 5 Sequence & Selection · 6 Iteration & Loops · 7 Functions & Modularisation · 8 Exception Handling & Debugging · 9 Stacks · 10 Queues · 11 Big O Notation · 12 Search Algorithms · 13 Sort Algorithms · 14 File Processing · 15 Recursion (HL)

How marks are earned in code questions

In a Construct question, marks come from the structure — correct loop or condition, correct use of the accumulator, correct output — not from perfect syntax. A missing colon does not cost a mark; a loop that never terminates does. In a Trace question, show every variable at every step: a right answer with no working scores less than a wrong answer with a complete table. In Explain, name the mechanism (what the interpreter actually does) rather than restating the code in words.

1 — Input, Output & Variables

Q1. Explain why age = input("Age: ") followed by if age > 17: raises an error in Python. [3]

Q2. State the difference between a variable and a constant, and explain why constants are used. [3]

Q3. Construct a program that reads a name and a year of birth and prints Hi NAME, you turn AGE this year. [4]

2 — Numeric Types & Operations

Q1. State the value and type of each: 7 / 2, 7 // 2, 7 % 2, 2 ** 10. [4]

Q2. Explain why 0.1 + 0.2 == 0.3 evaluates to False, and state how a program should compare two floats instead. [4]

Q3. A shop needs the number of full boxes of 12 eggs from n eggs, and the eggs left over. Construct the two expressions required. [2]

3 — String Types & Operations

code = "IB-CS-2027"
print(code[3:5], len(code), code.lower().replace("-", ""))

Q1. Deduce the output of the code above. [3]

Q2. Explain what is meant by saying Python strings are immutable, and state what s.upper() therefore returns. [3]

Q3. Construct a function initials(full_name) that returns the capitalised initials of every word, e.g. "ada lovelace" → "AL". [4]

4 — Arrays & Lists

Q1. Distinguish between a static array and a Python list. [3]

Q2. temps = [18, 21, 25, 19]. State the output of temps[-1] and explain why temps[4] raises an IndexError. [3]

Q3. Construct code that finds the highest temperature in temps and the day index it occurred on, without using max(). [5]

Q4. A 2D list grid holds 5 rows of 4 marks. State the expression for the mark in row 3, column 2 (counting from 1), and outline how you would total one whole row. [3]

5 — Sequence & Selection

mark = 62
if mark >= 40:
    grade = "pass"
if mark >= 60:
    grade = "merit"
if mark >= 80:
    grade = "distinction"
print(grade)

Q1. Deduce the output, and explain why using elif instead would change how many comparisons are made. [4]

Q2. The order of the three conditions is reversed (80 first, then 60, then 40) and elif is used. Explain the effect. [3]

Q3. Construct a condition that is true only when a member is aged 13–17 and has either a signed form or a parent present. [3]

6 — Iteration & Loops

total = 0
for i in range(1, 6):
    if i % 2 == 1:
        total += i
    else:
        total -= i
print(total)

Q1. Construct a trace table and state the output. [5]

i i % 2 == 1? total
——0

Q2. Distinguish between a counted loop and a conditional loop, and state which suits reading input until a sentinel value. [3]

Q3. Identify the error in while count < 10: print(count) and state the correction. [2]

Q4. Construct a nested loop that prints a 4×4 multiplication grid. [4]

7 — Functions & Modularisation

Q1. Distinguish between a parameter and an argument, and between returning and printing a value. [4]

Q2. Explain what is meant by the scope of a variable, using a local variable inside a function as your example. [3]

Q3. Construct a function fee(age, is_member) returning 0 for under-6s, 4 for members, and 7 otherwise. [4]

Q4. Explain two benefits of splitting a 300-line program into functions. [4]

8 — Exception Handling & Debugging

Q1. Distinguish between a syntax error, a run-time error and a logic error, with one example of each. [6]

Q2. Construct a loop that keeps asking for a whole number until the user enters one, handling the exception rather than crashing. [5]

Q3. Explain why except: on its own is considered poor practice. [3]

9 — Stacks

Q1. Define a stack and state its two core operations. [3]

Q2. A stack starts empty. Deduce its contents and any returned values after: push 3, push 8, pop, push 5, push 1, pop, pop. [4]

Q3. Explain why a stack is the correct structure for an undo feature. [3]

Q4. Construct code using a Python list that pops safely from a stack, returning None when it is empty. [3]

10 — Queues

Q1. Distinguish between a stack and a queue, and give one realistic use of each. [4]

Q2. A print queue receives jobs A, B, C; one job is served; jobs D, E arrive; two jobs are served. Deduce the queue contents and the order served. [4]

Q3. Explain one problem with implementing a queue as a Python list using pop(0). [3]

11 — Big O Notation

Q1. State the time complexity of: a single loop over n items; two nested loops over n items; binary search; looking up a value by index. [4]

Q2. Explain why constants and lower-order terms are dropped, so that 3n + 200 is written O(n). [3]

Q3. An O(n²) algorithm takes 4 seconds on 1 000 records. Estimate the time on 5 000 records, showing your reasoning. [3]

12 — Search Algorithms

Q1. The list [3, 8, 12, 19, 27, 34, 41, 55] is searched for 27 using binary search. Construct a trace table of the low, high, mid and value examined at each step. [5]

step low high mid list[mid] action
107
2
3
4

Q2. Explain why binary search cannot be used on an unsorted list, and outline when linear search is still the better choice. [4]

Q3. Construct a linear search function returning the index of a target, or −1 if absent. [4]

13 — Sort Algorithms

Q1. Construct the list state after each full pass of bubble sort on [5, 2, 9, 1]. [4]

Q2. Compare bubble sort and selection sort in terms of comparisons, swaps and complexity. [4]

Q3. Explain why a sorting algorithm's worst case matters more than its best case when choosing one for a live system. [3]

14 — File Processing

Q1. A file scores.txt holds one integer per line. Construct code that reads it and prints the mean, handling a missing file. [6]

Q2. Distinguish between opening a file in "w" and "a" mode, and explain the consequence of choosing wrongly. [3]

Q3. Explain why with open(...) as f: is preferred to calling open() and close() yourself. [3]

15 (HL) — Recursion

def f(n):
    if n <= 1:
        return 1
    return n * f(n - 1)

print(f(4))

Q1. Identify the base case and the recursive case, and deduce the output by showing the call stack. [5]

Q2. Explain what happens if the base case is removed, in terms of the call stack. [3]

Q3. Evaluate the use of recursion rather than iteration for summing a list of 100 000 numbers. [4]

Locked in the free sample

Answers & worked solutions

The answer section at the back of this workbook — worked solutions with mark allocations for every question in all fifteen sections — is part of the members’ library. Every question itself is above this page, unabridged.

What you have here, free

All fifteen exam-style question sets in full, with real IB command terms, per-part mark allocations and ruled answer space — enough assessed material to cover the whole unit, ready to set tomorrow morning.

What members also get

The full mark scheme with per-point allocations, model Python solutions, accepted alternatives, and marker guidance on partial credit.

This is one unit workbook out of seven. Members get the same treatment across Themes A and B — plus the lesson decks the questions were written against, printable handouts, the nine practical scenario books, full practice papers and the AI revision tools.

Unlock the mark schemes →