Chhetri AcademyGCSE & A level Paper Builder

6.5Operators

Edexcel GCSE Computer Science (1CP2) · Problem solving with programming

Practise Operators. 15 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.

Build a paper on this topic

▶ Watch videos on Operators (Craig 'n' Dave Edexcel on YouTube) · Practise all of Problem solving with programming

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

Give the logical operator that joins two conditions so that the result is True only when both conditions are True.
and
State the data type of the result of a comparison such as age >= 16.
Boolean
All three errors in this program are the same type of error. State the type of error.
Logic error.

Sample questions

Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.

Question 1Easy4 marks
A program should compare two numbers and state their average and whether they are the same. The program below does this, but it contains errors.
01 # Average of two numbers
02 # Works out the average of two numbers and compares them
03 print("Number comparison tool")
04 print("------------------------")
05 num1 = int(input("Enter first number: "))
06 num2 = int(input("Enter second number: "))
07 average = num1 + num2 / 2
08 print("Number 1:", num1)
09 print("Number 2:", num2)
10 print("The average is", average)
11 if num1 = num2:
12     print("The numbers are the same")
13 else:
14     print("The numbers are different")
15 print("Thank you for using the tool")
(a) Amend the program to:
  1. fix the syntax error on line 11
  2. fix line 07 so that the average is correctly calculated as (num1 + num2) ÷ 2.
Test your amended program with the numbers 4 and 6.[2]
(b) Once the syntax error is fixed but before line 07 is corrected, the program outputs The average is 7.0 for the numbers 4 and 6. Explain why.[2]
Show the answer and mark scheme
(a) Answer:
# Average of two numbers
# Works out the average of two numbers and compares them
print("Number comparison tool")
print("------------------------")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
average = (num1 + num2) / 2
print("Number 1:", num1)
print("Number 2:", num2)
print("The average is", average)
if num1 == num2:
    print("The numbers are the same")
else:
    print("The numbers are different")
print("Thank you for using the tool")
  • line 11: changes = to ==
  • line 07: adds brackets, (num1 + num2) / 2
(b) Answer: Division has a higher precedence than addition, so num2 / 2 is worked out first: 4 + 6 ÷ 2 = 4 + 3 = 7.0.
  • division (/) is carried out before addition (+), because it has a higher precedence
  • so the program works out 4 + (6 ÷ 2) = 4 + 3 = 7.0 instead of (4 + 6) ÷ 2 = 5.0
Question 2Medium7 marks
Look at this program.
01 number = int(input("Enter a whole number: "))
02 if number % 2 == 0:
03     print("Even")
04 else:
05     print("Odd")
06 print("Last digit:", number % 10)
07 print("Tens digit:", number // 10 % 10)
(a) The user enters 347. State the output of the program.[3]
(b) Explain how the expression number // 10 % 10 finds the tens digit of the number.[2]
(c) Write an if statement that outputs Fizz when number is a multiple of 3.[2]
Show the answer and mark scheme
(a) Answer:
Odd
Last digit: 7
Tens digit: 4
  • Odd
  • Last digit: 7
  • Tens digit: 4
(b) Answer: // 10 removes the last digit (347 becomes 34). % 10 then gives the last digit of what is left (4), which was the tens digit. // and % have the same precedence, so they are carried out from left to right.
  • // 10 removes the last digit / divides by 10 ignoring the remainder (347 → 34)
  • % 10 then gives the last digit of the result (34 → 4), which is the tens digit
(c) Answer:
if number % 3 == 0:
    print("Fizz")
  • uses number % 3 == 0 as the condition
  • outputs Fizz inside the if statement
Question 3Hard8 marks
In Python, comparisons such as > are carried out first, then not, then and, and finally or. A program starts with these lines.
a = 12
b = 5
c = 0
(a) Complete {table} to show whether each expression is True or False.[4]
[object Object]
(b) Explain why the first two expressions in {table} give different results.[2]
(c) A heating alarm must sound when the temperature temp is above 30 or below 5, but only while the Boolean variable active is True.
Write the condition for an if statement that is True when the alarm should sound.[2]
Show the answer and mark scheme
(a) Answer: True; False; False; True
  • a > 10 or b > 10 and c > 10: True
  • (a > 10 or b > 10) and c > 10: False
  • not a > b: False
  • a % b == 2 and not c > 0: True
(b) Answer: and is carried out before or, so the first expression means a > 10 or (b > 10 and c > 10), which is True because a > 10 is True. In the second, the brackets make or happen first, giving True and c > 10, which is False.
  • and is carried out before or, so the first is a > 10 or (b > 10 and c > 10), which is True because a > 10 is True
  • the brackets in the second make or happen first, then True and c > 10 (False) gives False
(c) Answer: active and (temp > 30 or temp < 5)
  • temp > 30 or temp < 5 inside brackets
  • combined with active using and

Related subtopics

Stuck? Get 1-to-1 help. Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.

Book a free trial