Question bank › IB Computer Science › B2 Programming

Programming constructs: sequence, selection, loops and functions

Answer on paper first, then reveal the mark scheme one mark at a time.

1Standard4 marks

Consider the following Python program.

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

(a) Complete a trace table showing the value of total at the end of each iteration, and state the output. [3]

(b) State the output if range(1, 6) is changed to range(1, 5). [1]

2Standard4 marks

Write a Python function count_vowels(text) that returns the number of vowels (a, e, i, o, u) in the string text. Upper-case and lower-case vowels must both be counted. For example, count_vowels("Education") returns 5.

[4]

3Core4 marks

In a program, the variables below have been assigned these values.

x = 7
y = 12
flag = False

(a) State whether each of the following conditions evaluates to True or False.

(i) x > 5 and y < 10

(ii) not flag or x == y

(iii) x != 7 or (y >= 12 and not flag) [3]

(b) A youth club accepts members aged 13 to 19 inclusive. Write a single condition, using the variable age, that is true only when age is in this range. [1]

Answers may be written in Python or Java.

Stuck on Programming constructs: sequence, selection, loops and functions?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Core5 marks

A car park calculates the charge, in pounds, using the function below. Drivers with a blue badge park for free.

def charge(hours, blue_badge):
    if blue_badge:
        return 0
    elif hours <= 1:
        return 2
    elif hours <= 4:
        return 5
    else:
        return 5 + (hours - 4) * 3

(a) State the value returned by each of the following calls.

(i) charge(1, False)

(ii) charge(6, False)

(iii) charge(6, True) [3]

(b) A programmer swaps the order of the two conditions so that hours <= 4 is tested before hours <= 1. Explain why this would cause incorrect output. [2]

5Standard5 marks

The program below is meant to read exam marks until the user enters -1, then print the mean of the marks entered. The statements inside the loop are in the wrong order.

total = 0
count = 0
mark = int(input("Mark: "))
while mark != -1:
    mark = int(input("Mark: "))
    total = total + mark
    count = count + 1
print(total / count)

(a) The user enters 60, 70, 80 and then -1. State the output. [1]

(b) Explain why this output is incorrect. [2]

(c) Rewrite the body of the while loop so that the program gives the correct mean. [1]

(d) State what would happen if the line mark = int(input("Mark: ")) inside the loop were deleted and the user entered 60 first. [1]

6Standard5 marks

Consider the following Python program.

n = 40213
result = 0
count = 0
while n > 0:
    d = n % 10
    if d % 2 == 1:
        result = result * 10 + d
    else:
        count = count + 1
    n = n // 10
print(result, count)

(a) Copy and complete the trace table below, showing the values of the variables at the end of each pass through the loop, and state the output.

dresultcountn
0040213

[4]

(b) State the output if the first line is changed to n = 0, and explain why. [1]

7Standard5 marks

Consider the following Python program.

score = 10

def bonus(points):
    score = points * 2
    return score + 1

def add_bonus(points):
    global score
    score = score + bonus(points)

result = bonus(3)
print(score, result)
add_bonus(4)
print(score)

(a) State the output of the program. [3]

(b) Explain why the first print statement does not display 6 for score, even though bonus assigns score = points * 2. [2]

8Hard6 marks

A cinema booking program has been split into functions. The structure chart shows which function calls which, and the data passed into each function (arrow pointing down) and returned from it (arrow pointing up).

Ticket prices are £9.50 per adult and £6.00 per child. If five or more tickets are bought in one booking, a 10% discount is applied to the whole cost. The function apply_discount(cost, rate) has already been written; for example, apply_discount(40.0, 0.10) returns 36.0.

(a) Outline two benefits of writing this program as separate functions rather than as one block of code. [2]

(b) Construct the function calculate_cost(adults, children), using apply_discount where needed, so that it matches the structure chart. [4]

Answers may be written in Python or Java.

main get_booking calculate_cost print_ticket apply_discount adults,children adults, children cost cost cost, rate new cost
9Hard5 marks

A program runs two threads at the same time. Each thread needs exclusive use of a shared printer and a shared log file, which are protected by the locks printer_lock and file_lock. acquire() waits until the lock is free and then takes it.

# Thread A                    # Thread B
printer_lock.acquire()        file_lock.acquire()
file_lock.acquire()           printer_lock.acquire()
print_report()                write_log()
file_lock.release()           printer_lock.release()
printer_lock.release()        file_lock.release()

(a) Explain how this sequence of instructions can lead to deadlock. [3]

(b) Describe how changing the order of instructions in Thread B prevents deadlock. [2]

10Stretch6 marks

Consider the following Python function.

def grid(n):
    total = 0
    for i in range(1, n + 1):
        line = ""
        for j in range(i, n + 1):
            if (i * j) % 2 == 0:
                line = line + "#"
                total = total + 1
            else:
                line = line + "."
        print(line)
    return total

(a) State the output printed by the call grid(4). [2]

(b) State the value returned by grid(4). [1]

(c) In terms of n, state how many times the if condition is tested during one call of grid(n). [1]

(d) Rewrite the inner for loop as a conditional (while) loop that produces the same result. [2]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session