Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

Library routines: MOD, DIV, ROUND and RANDOM

Practise 24 exam-style questions on Library routines: MOD, DIV, ROUND and RANDOM for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 24 questions
  • 1–6 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

2 marks

State the value of each of these expressions:
DIV(29, 4)
MOD(29, 4)

Model answer

DIV(29, 4) = 7
MOD(29, 4) = 1

Explanation

4 goes into 29 seven whole times (4 × 7 = 28), so DIV gives 7. The amount left over is 29 - 28 = 1, so MOD gives 1. A useful check: DIV(29, 4) × 4 + MOD(29, 4) gets you back to 29.

Mark scheme
  1. 1 mark

    DIV(29, 4) = 7

  2. 1 mark

    MOD(29, 4) = 1

Question 2

2 marks

Look at this pseudocode:

Count ← 0
FOR Number ← 10 TO 30
    IF MOD(Number, 4) = 0 OR MOD(Number, 6) = 0
      THEN
        Count ← Count + 1
    ENDIF
NEXT Number
OUTPUT Count

State the values of Number that cause Count to increase, and state the output.

Model answer

Count increases for 12, 16, 18, 20, 24, 28 and 30.
The output is 7.

Explanation

MOD(Number, 4) = 0 picks out multiples of 4 (12, 16, 20, 24, 28) and MOD(Number, 6) = 0 picks out multiples of 6 (12, 18, 24, 30). With OR, a number counts once even if it is a multiple of both, so 12 and 24 are not counted twice.

Mark scheme
  1. 1 mark

    12, 16, 18, 20, 24, 28, 30

    All seven values and no others.

  2. 1 mark

    Output: 7

    Follow through from a list given.

Question 3

3 marks

This pseudocode should simulate rolling a six-sided dice 50 times and output the percentage of rolls that were a 6, rounded to 1 decimal place.

01Sixes ← 0
02FOR Roll ← 0 TO 50
03    Dice ← ROUND(RANDOM() * 6, 0)
04    IF Dice = 6
05      THEN
06        Sixes ← Sixes + 1
07    ENDIF
08NEXT Roll
09OUTPUT ROUND(Sixes / 50 * 100)

There are three errors. Identify the line number of each error and write the corrected line.

Model answer

Line 2: FOR Roll ← 1 TO 50
Line 3: Dice ← ROUND(RANDOM() * 5, 0) + 1
Line 9: OUTPUT ROUND(Sixes / 50 * 100, 1)

Explanation

A loop from 0 to 50 runs 51 times, not 50. ROUND(RANDOM() * 6, 0) gives 0 to 6, so 0 is possible and 6 is less likely than it should be; multiplying by 5 and adding 1 gives 1 to 6. ROUND always needs a second parameter, the number of decimal places, which here is 1.

Mark scheme
  1. 1 mark

    Line 2: FOR Roll ← 1 TO 50

    The line number and the correction are both needed for each mark. Accept FOR Roll ← 0 TO 49.

  2. 1 mark

    Line 3: Dice ← ROUND(RANDOM() * 5, 0) + 1

    Accept any correct statement giving whole numbers 1 to 6 only.

  3. 1 mark

    Line 9: OUTPUT ROUND(Sixes / 50 * 100, 1)

Question 4

5 marks

Write an algorithm in pseudocode that inputs 20 whole numbers, one at a time, and then outputs how many of the numbers were even and how many were odd.

Model answer

Evens ← 0
Odds ← 0
FOR Count ← 1 TO 20
    INPUT Number
    IF MOD(Number, 2) = 0
      THEN
        Evens ← Evens + 1
      ELSE
        Odds ← Odds + 1
    ENDIF
NEXT Count
OUTPUT "Even numbers: ", Evens
OUTPUT "Odd numbers: ", Odds

Explanation

MOD(Number, 2) is 0 for every even number and 1 for every odd number, so one IF ... ELSE decides which counter to increase. The counters start at 0 before the loop and are output only once all 20 numbers have been processed.

Mark scheme
  1. 1 mark

    MP1 two counters (even and odd) initialised to 0 before the loop

    max 5; accept one counter with the other worked out as 20 minus it

  2. 1 mark

    MP2 loop that repeats 20 times

  3. 1 mark

    MP3 inputs a number inside the loop

  4. 1 mark

    MP4 uses MOD(Number, 2) = 0 (or equivalent) to test for an even number

  5. 1 mark

    MP5 adds 1 to the correct counter inside the loop

  6. 1 mark

    MP6 outputs both counts, with messages, after the loop

    Do not accept output inside the loop.

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.