- Subjects
- Computer Science
- Question bank
- B2.3 Programming constructs
B2.3 Programming constructs questions for IB Computer Science
183 questions on Programming constructs, written by Augment in the style of IB exam papers. Six of them are below in full, to answer on paper.
- IB Computer Science
- Part of B2 Programming
- SL and HL
Questions by paper and level
SL and HL questions are for students at either level. HL only questions cover what only HL students study.
| Paper | SL and HL | HL only | All |
|---|---|---|---|
| Paper 2 | 168 | 15 | 183 |
Counted on
Questions to try
Written by Augment in the style of IB exams, not past papers. Answer them on paper, then check each one against its markscheme with a free account.
A music streaming service checks whether a playlist contains the same song twice. Each song is identified by an integer ID, and a playlist is a list (Python) or array (Java) of IDs called ids.
Python
def has_duplicate(ids):
n = len(ids)
for i in range(n - 1):
for j in range(i + 1, n):
if ids[i] == ids[j]:
return True
return FalseJava
public static boolean hasDuplicate(int[] ids) {
int n = ids.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ids[i] == ids[j]) {
return true;
}
}
}
return false;
}- (a)
State the value returned when
idsholds[12, 7, 33, 7].1 mark
- (b)
Calculate the number of times the comparison
ids[i] == ids[j]is made, in the worst case, for a playlist of 6 songs.2 marks
- (c)
Deduce the worst-case time complexity of
has_duplicate/hasDuplicate, explaining your answer.3 marks
- (d)
Describe the best case for this function/method and state its time complexity.
2 marks
- (e)
The service now keeps every playlist sorted by ID.
Construct, in Python or Java, a function/method
has_duplicate_sorted/hasDuplicateSortedthat returns whether a sorted playlist contains a repeated ID, using a single loop. For example,[7, 12, 12, 33]returns true and[7, 12, 33]returns false.4 marks
- (f)
State the time complexity of your solution to part (e) and outline why it scales better than
has_duplicate/hasDuplicatefor long playlists.2 marks
A data-compression library contains the function/method tally below.
Python
def tally(n):
count = 0
for i in range(n):
j = 1
while j < n:
j = j * 2
count = count + 1
return countJava
public static int tally(int n) {
int count = 0;
for (int i = 0; i < n; i++) {
int j = 1;
while (j < n) {
j = j * 2;
count = count + 1;
}
}
return count;
}- (a)
State the value returned by
tally(8)and bytally(10).2 marks
- (b)
Deduce the time complexity of
tally, explaining your answer.3 marks
A weather station takes one wind-speed reading every minute. The function/method analyse prints the mean reading and counts the pairs of readings that differ by more than limit.
Python
def analyse(readings, limit):
total = 0
for r in readings:
total = total + r
print(total / len(readings))
jumps = 0
for i in range(len(readings)):
for j in range(i + 1, len(readings)):
if abs(readings[i] - readings[j]) > limit:
jumps = jumps + 1
print(jumps)Java
public static void analyse(int[] readings, int limit) {
int total = 0;
for (int r : readings) {
total = total + r;
}
System.out.println((double) total / readings.length);
int jumps = 0;
for (int i = 0; i < readings.length; i++) {
for (int j = i + 1; j < readings.length; j++) {
if (Math.abs(readings[i] - readings[j]) > limit) {
jumps = jumps + 1;
}
}
}
System.out.println(jumps);
}- (a)
State the output when
analyseis called with the readings[12, 15, 11, 19, 14]and a limit of 3.2 marks
- (b)
State the time complexity of the first loop and of the nested loops.
2 marks
- (c)
Explain why the overall time complexity of
analyseis rather than .2 marks
- (d)
Analysing one day of data (1440 readings) takes 0.5 seconds. Calculate the approximate time needed to analyse one week of data (10 080 readings).
2 marks
- (e)
State the space complexity of
analyse.1 mark
- (f)
The meteorologist decides that only consecutive readings matter.
Construct, in Python or Java, a function/method
count_steps/countStepsthat takesreadingsandlimitand returns the number of consecutive pairs of readings that differ by more thanlimit. For example, for[12, 15, 11, 19, 14]and a limit of 3 it returns 3.4 marks
A train operator stores the numbers of the tickets sold for one journey in a list (Python) or array (Java) tickets, in the order they were sold. An inspector's handheld device needs to find a ticket number.
You must not use built-in searching functions or methods such as index() or in (Python) or indexOf() or contains() (Java).
- (a)
Construct, in Python or Java, a function/method
find_ticket/findTicketthat takesticketsand a ticketnumber, and returns the index of the first occurrence ofnumber, or −1 if it is not there. For example, ifticketsholds[4410, 3172, 5098, 3172], searching for 3172 returns 1 and searching for 9999 returns −1.5 marks
- (b)
State the worst-case time complexity of your function/method.
1 mark
- (c)
Outline why returning as soon as the ticket is found improves the average performance of the search.
2 marks
An airline stores the booking numbers for a flight, as integers in ascending order, in a list (Python) or array (Java) bookings. Check-in staff need to find a booking quickly.
You must not use built-in searching functions or methods such as index(), in or the bisect module (Python), or Arrays.binarySearch() or indexOf() (Java).
- (a)
State the condition that
bookingsmust meet for a binary search to work.1 mark
- (b)
Construct, in Python or Java, a function/method
find_booking/findBookingthat uses a binary search to return the index oftargetinbookings, or −1 if it is not present.6 marks
- (c)
The flight has 500 bookings. Calculate the maximum number of times
bookings[mid]is compared withtargetin a search that finds the booking.2 marks
- (d)
Passenger surnames are held in a second list/array in the same order as
bookings. Explain why a binary search cannot be used to find a passenger by surname.2 marks
- (e)
The airline expects flights with up to 50 000 bookings in future. Compare linear search and binary search for finding a booking number in this system.
4 marks
A city bike-sharing scheme receives a status message from each docking station, for example "ST042:12/20". This means station 42 has 12 bikes in its 20 docks. The station number always has three digits, but the other two numbers can have one or two digits. The first character is at index 0.
- (a)
State the value of
status.find(":")(Python) /status.indexOf(":")(Java) whenstatusholds"ST042:12/20".1 mark
- (b)
Construct a line of code that extracts the station number from
statusand stores it as an integer instation.3 marks
- (c)
Construct, in Python or Java, a function/method
free_docks(status)/freeDocks(status)that returns the number of empty docks. For example,"ST042:12/20"returns 8 and"ST107:0/16"returns 16.5 marks
- (d)
Some messages are corrupted, for example
"ST042:1x/20". Describe how exception handling could stop one corrupted message from crashing the program that processes all the stations.3 marks
- (e)
Suggest two further test messages for
free_docks/freeDocks, giving the expected result of each.2 marks
Practice all 183 in Augment
Free with an account. Every question has its markscheme, and Augment keeps the ones you got wrong so you can come back to them.
More questions in Programming
- B2.1Programming fundamentals
- B2.2Data structures
- B2.3Programming constructsThis page
- B2.4Programming algorithms
- B2.5File processing
More on Programming constructs
- B2.3 Programming constructs notesWhat Programming constructs covers, explained, with its key terms.
- IB Computer Science question bankEvery Computer Science topic counted, and one question from each with its markscheme.
- Computer Science exam formatEach Computer Science paper, how long it is and its share of the grade.
Check your answers in Augment
A free account opens all 183 questions on Programming constructs with their markschemes, and keeps a record of what you have marked.