Computer Science · B2.1, B2.3

Programming fundamentals

Variables, selection, loops, strings, functions and errors: the tools you use to build every Paper 2 answer. In 7Quest it is Code Forge: 6 stages, 58 quick questions and 17 exam-style ones.

What the stages cover

Codes follow the 2027 guide (A1–A4 and B1–B4). HL-only stages are marked. Each stage is a short lesson, quick questions, a minigame and an exam-style question.

  1. Variables, data types and operators
    Use variables of the basic data types, convert between types, calculate with the arithmetic operators (including integer division, the remainder and powers), and read and output data in the correct order.
    Syllabus B2.1.1, B2.3.1
  2. Selection: if and else
    Write correct selection structures, with compound and nested conditions, choosing the right order for the checks.
    Syllabus B2.3.2, B2.1.1
  3. Iteration: for and while
    Construct loops with the correct bounds, use counters, accumulators and sentinels, and choose between for and while.
    Syllabus B2.3.3
  4. Strings and substrings
    Index, slice, traverse and transform strings in order to extract and manipulate substrings.
    Syllabus B2.1.2
  5. Functions and modularisation
    Define functions with parameters and a return value, understand local and global scope, and break a program down into modules.
    Syllabus B2.3.4, B2.1.1
  6. Errors, exceptions and debugging
    Distinguish syntax, runtime and logic errors, handle exceptions with try blocks, and debug using trace tables, breakpoints and test data.
    Syllabus B2.1.3, B2.1.4

Two questions to try

Written for 7Quest in the style of the exam, not taken from past papers. Answer first, then open the markscheme.

Trace3 marksIteration: for and while

Trace the following code and state its exact output.

for i in range(1, 4):
    line = ""
    for j in range(i):
        line = line + str(i)
    print(line)
Show the markscheme

Markscheme (3 marks)

  1. First line: 1.
  2. Second line: 22.
  3. Third line: 333 (and there are no more lines: range(1, 4) does not include 4).
  4. Trace table showing line reset to "" for each i and the inner loop running i times (j from 0 to i - 1).

A full-marks answer

One row per change of a variable or output:

i   j   line   output
1       ""
1   0   "1"
1              1
2       ""
2   0   "2"
2   1   "22"
2              22
3       ""
3   0   "3"
3   1   "33"
3   2   "333"
3              333

line is reset to "" on each iteration of the outer loop and the inner loop runs i times. Exact output, on three lines: 1, 22, 333.

Trace the following code and state its exact output.

for (int i = 1; i < 4; i++) {
    String line = "";
    for (int j = 0; j < i; j++) {
        line = line + i;
    }
    System.out.println(line);
}
Show the markscheme

Markscheme (3 marks)

  1. First line: 1.
  2. Second line: 22.
  3. Third line: 333 (and there are no more lines: i < 4 is false when i reaches 4).
  4. Trace table showing line reset to "" for each i and the inner loop running i times (j from 0 to i - 1).

A full-marks answer

One row per change of a variable or output:

i   j   line   output
1       ""
1   0   "1"
1              1
2       ""
2   0   "2"
2   1   "22"
2              22
3       ""
3   0   "3"
3   1   "33"
3   2   "333"
3              333

line is declared again as "" on each iteration of the outer loop and the inner loop runs i times. Exact output, on three lines: 1, 22, 333.

Trace4 marksVariables, data types and operators

Trace the following code using a trace table with columns a, b, c and output.

a = 23
b = 4
c = a // b
a = a % b
b = b + c
a, b = b, a
print(a, b, c)
Show the markscheme

Markscheme (4 marks)

  1. Initial values a = 23, b = 4.
  2. c = 5 (integer division) and a = 3 (remainder).
  3. b = 9 and, after the swap, a = 9 and b = 3.
  4. Output: 9 3 5.

A full-marks answer

One row per line that changes a variable or produces output:

a    b    c    output
23
     4
          5
3
     9
9    3              (a, b = b, a: simultaneous swap)
                    9 3 5

c = 23 // 4 = 5 and a = 23 % 4 = 3; then b = 4 + 5 = 9; the swap uses the values from before the line, so a becomes 9 and b becomes 3.

Trace the following code, which starts with a = 23 and b = 4, using a trace table with columns a, b, c, temp and output.

int a = 23;
int b = 4;
int c = a / b;
a = a % b;
b = b + c;
int temp = a;
a = b;
b = temp;
System.out.println(a + " " + b + " " + c);
Show the markscheme

Markscheme (4 marks)

  1. Initial values a = 23, b = 4.
  2. c = 5 (integer division: both operands are int) and a = 3 (remainder).
  3. b = 9; temp = 3, then a = 9 and b = 3 (swap through temp).
  4. Output: 9 3 5.

A full-marks answer

One row per line that changes a variable or produces output:

a    b    c    temp  output
23
     4
          5
3
     9
               3
9
     3
                     9 3 5

c = 23 / 4 = 5 because / between two ints drops the fractional part, and a = 23 % 4 = 3; then b = 4 + 5 = 9. temp keeps the old a (3) so that it is not lost when a = b runs; finally b = temp.

The other 15 questions in this topic are in the app, marked point by point, and anything you miss comes back in your review. If this is the topic you find hardest, you can make it your free topic and do all of it, with short practice tests on it, without paying or giving a card.

Start Computer Science free