Paper 2 Programming Fundamentals Drills

These are original Paper 2-style practice questions. They use concrete test calls and expected evidence so your answers can be checked exactly.

Questions 6 and 7 are deliberate bridge tasks from basic text-file input/output, which links this topic to the later section on storing and retrieving data from serial and sequential text files.

Detailed answers and model code are in Paper 2 Programming Fundamentals Answers.

Revise the topic hub first:

Questions

Question 1: Grade Function

Write a function grade_from_mark(mark) that returns:

  • "A" for marks from 75 to 100;
  • "B" for marks from 60 to 74;
  • "C" for marks from 50 to 59;
  • "U" for marks below 50.

Assume mark is an integer from 0 to 100 inclusive.

Test it with the calls below and show the output. [6]

print(grade_from_mark(75))
print(grade_from_mark(74))
print(grade_from_mark(49))

Question 2: List Average

Write a function average(values) that returns the arithmetic mean of a list of numbers. If the list is empty, return 0. Test it with the calls below. [5]

print(average([6, 9, 12]))
print(average([]))

Question 3: 2D List Row Totals

Write a function row_totals(grid) that returns a list containing the total of each row in a 2D list. Test it with the call below. [7]

print(row_totals([[3, 4, 5], [10, 0, 2], [7, 8, 9]]))

Question 4: String Vowel Count

Write a function count_vowels(text) that returns the number of vowels in text. Count both uppercase and lowercase vowels. Treat a, e, i, o, and u as vowels. Test it with the calls below. [6]

print(count_vowels("A quiet room"))
print(count_vowels("SKY"))

Question 5: Recursive Factorial

Write a recursive function factorial(n) for non-negative integers. It should return 1 when n is 0 or 1. Test it with the calls below. [7]

print(factorial(0))
print(factorial(1))
print(factorial(5))

Question 6: Read Lines

Assume a text file marks.txt contains one integer mark per line:

41
77
58
90
33

Write a function count_at_least(filename, threshold) that reads the file and returns how many marks are greater than or equal to threshold. Test it with:

print(count_at_least("marks.txt", 60))

[7]

Question 7: Write Results

The list below stores records in the form [name, mark].

records = [["Asha", 74], ["Ben", 49], ["Chen", 82], ["Divya", 50]]

Write a function write_passes(records, filename) that writes the names and marks of students with marks at least 50 to the output file, one record per line in the format name,mark. Test it by writing to passes.txt and printing the file contents. [6]

Question 8: Student Code Normalisation

Write a function normalise_student_code(code) that:

  • removes leading and trailing spaces;
  • converts letters to uppercase;
  • returns the cleaned code only if it starts with "S" and all remaining characters are digits;
  • returns "INVALID" otherwise.

Test it with:

print(normalise_student_code(" s1042 "))
print(normalise_student_code("T204"))
print(normalise_student_code(" s10a "))

Expected output:

S1042
INVALID
INVALID

[8]

Question 9: First Index Below Limit

Write a function first_below(values, limit) that returns the index of the first value in values that is less than limit.

If no value is less than limit, return -1.

Test it with:

print(first_below([80, 75, 49, 60], 50))
print(first_below([55, 60], 50))
print(first_below([49], 50))

Expected output:

2
-1
0

[8]

Question 10: Function Decomposition

Write two functions:

  • is_pass(mark) returns True when mark >= 50, otherwise False;
  • student_result(name, mark) calls is_pass(mark) and returns a string in the form "name: pass" or "name: resit".

Test it with the calls below. [7]

print(student_result("Asha", 50))
print(student_result("Ben", 49))
print(student_result("Chen", 75))

Expected output:

Asha: pass
Ben: resit
Chen: pass

Review Checklist

After attempting these questions, check whether you can:

  • write functions with clear parameters and return values;
  • test boundary cases such as empty lists and threshold values;
  • process strings using methods, indexing, iteration, and conditions;
  • traverse lists by index and handle not-found cases;
  • decompose a task into helper functions;
  • read and write simple text files;
  • convert string data to integers when needed;
  • show output evidence for each required test.