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

Variables, constants and data types

Practise 29 exam-style questions on Variables, constants and data types 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.

  • 29 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

1 mark

The variable Grade has been declared with this statement:

DECLARE Grade : CHAR

Write a pseudocode statement that stores the letter B in Grade.

Model answer

Grade ← 'B'

Explanation

In Cambridge pseudocode a CHAR literal is written in single quotes, a STRING literal in double quotes. Without any quotes, B would be treated as the name of a variable. Assignment always uses the arrow ←.

Mark scheme
  1. 1 mark

    Grade ← 'B'

    do not accept Grade ← "B" (a string), Grade ← B (B would be read as an identifier) or Grade = 'B'

Question 2

2 marks

Look at this pseudocode.

01DECLARE Age : INTEGER
02DECLARE FirstName : STRING
03DECLARE Initial : CHAR
04Age ← 15
05FirstName ← "Priya"
06Initial ← "Pr"

Identify the line number that contains a data type error, and explain the error.

Model answer

Line 06. Initial has been declared as a CHAR, which stores a single character, but "Pr" is a string of two characters. It should be, for example, Initial ← 'P'.

Explanation

Every value assigned to a variable must match its declared data type. A CHAR holds exactly one character in single quotes; text with more than one character needs a STRING. Lines 04 and 05 are correct: 15 is an integer and "Priya" is a string.

Mark scheme
  1. 1 mark

    Line 06

  2. 1 mark

    Initial is a CHAR, which can hold only one character, but "Pr" is a string of two characters

    accept a correction such as Initial ← 'P' as part of the explanation

Question 3

3 marks

State the data type of the value given by each of these pseudocode expressions:

7 / 2
DIV(7, 2)
7 > 2

Model answer

7 / 2: REAL (3.5)
DIV(7, 2): INTEGER (3)
7 > 2: BOOLEAN (TRUE)

Explanation

The / operator gives a real result even when both numbers are integers, while DIV gives only the whole-number part of the division. A comparison such as 7 > 2 is either TRUE or FALSE, so its result is a Boolean - which is why a comparison can be assigned straight to a BOOLEAN variable.

Mark scheme
  1. 1 mark

    7 / 2: REAL

    the value is 3.5

  2. 1 mark

    DIV(7, 2): INTEGER

    the value is 3

  3. 1 mark

    7 > 2: BOOLEAN

    the value is TRUE

Question 4

4 marks

A program contains these declarations:

CONSTANT MaxSize ← 50
DECLARE Size : INTEGER
DECLARE Price : REAL
DECLARE Code : STRING

Assume Size already holds a whole number. For each of the following statements, state whether it is valid or invalid. For each invalid statement, give the reason.

Statement 1: Size ← 12.5
Statement 2: Price ← Size * 1.5
Statement 3: Code ← 4512
Statement 4: MaxSize ← 60

Model answer

Statement 1: invalid - Size is an INTEGER, so it cannot store the real value 12.5.
Statement 2: valid - a whole number multiplied by 1.5 gives a real result, which suits the REAL variable Price.
Statement 3: invalid - Code is a STRING, so the value must be in double quotes: "4512".
Statement 4: invalid - MaxSize is a constant and cannot be assigned a new value.

Explanation

Before assigning, check two things: is the target a variable (not a constant), and does the value match the variable's data type? Numbers without quotes are numeric, so they cannot go into a STRING variable, and a value with a fractional part cannot go into an INTEGER.

Mark scheme
  1. 1 mark

    Statement 1: invalid - 12.5 is a real value but Size is an INTEGER

    the reason is needed for the mark

  2. 1 mark

    Statement 2: valid

    Price is REAL and the result is real

  3. 1 mark

    Statement 3: invalid - 4512 is an integer but Code is a STRING (it should be "4512")

    the reason is needed for the mark

  4. 1 mark

    Statement 4: invalid - MaxSize is a constant, so its value cannot be changed

    the reason is needed for the mark

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.