Oxford Cambridge and RSA
GCSE (9-1) Computer Science
Algorithms & Programming
J277/02
Topic: 2.1 Writing & Fixing Algorithms
Time allowed: 45 minutes
Centre number Candidate number
First name Last name
INSTRUCTIONS INFORMATION
Turn over
Section A: Reading & Tracing Algorithms
1
A program uses a Switch/Case statement to determine a delivery charge based on the destination code.
input destination switch destination: case "UK": cost = 5.00 case "EU": cost = 12.50 case "US": cost = 20.00 default: cost = 35.00 endswitch print(cost)
(a) State the output if the user inputs "EU".
[1]

(b) State the output if the user inputs "Asia".
[1]

(c) Rewrite this algorithm using IF / ELSE IF statements instead of Switch/Case.
[3]
2
Study the following flowchart which calculates a discount.
Girl in a jacket

Complete the trace table below to show the execution of the flowchart if the Input Price is 50.
[3]
Step Price Is Price > 50? Discount Output
1 50 - -
2 - -
3 - - -
3
A student is trying to write a loop that prints the even numbers from 2 to 10 inclusive.
Read the following code:
for i = 2 to 10 print(i) i = i + 1 next i
Explain why this code will not produce the expected output of 2, 4, 6, 8, 10.
[2]
Turn over
Section B: Fixing Algorithms (Debugging)
4
A programmer has written a program to check if a user is old enough to drive (17 or older).
The code contains a logic error.
01 age = input("Enter your age") 02 if age > 17 then 03 print("You can drive") 04 else 05 print("You are too young") 06 endif
(a) Identify the line number containing the logic error.
[1]
Line:

(b) Write the corrected line of code.
[1]

(c) Explain what is meant by a Logic Error.
[2]
5
The following algorithm is designed to ask the user for a password until they enter "Secret".
However, the algorithm contains an infinite loop.
01 password = input("Enter password") 02 while password != "Secret" 03 print("Wrong password") 04 endwhile 05 print("Access Granted")
(a) Explain why this loop never ends if the user enters the wrong password once.
[2]

(b) Write the one line of code that needs to be added inside the while loop to fix this error.
[2]
6
A student writes a condition to check if a colour is either "Red" or "Blue".
if colour == "Red" OR "Blue" then...

Explain why this line of code causes an error or unexpected behaviour in most programming languages, and write the correct version.
[3]
Explanation:
Correct Code:
Turn over
Section C: Writing Algorithms
7
Scenario: A vending machine calculates change.
Write an algorithm using Pseudocode that:
  • Asks the user to input the Item Price.
  • Asks the user to input the Money Inserted.
  • Calculates the Change (Money Inserted - Item Price).
  • If the Change is less than 0, output "Not enough money".
  • Otherwise, output the Change amount.
[5]
8
Scenario: A teacher needs to calculate the average score of a class.
Write an algorithm using Pseudocode that:
  • Uses a Loop to ask for 5 scores.
  • Adds each score to a Total.
  • After the loop finishes, calculates the Average.
  • Outputs the Average.
[6]
Turn over
9
Flowchart Construction
Draw a flowchart to represent the following algorithm:
  • Start
  • Input temperature
  • If temperature is less than 0, output "Freezing".
  • If temperature is between 0 and 100 (inclusive), output "Liquid".
  • If temperature is greater than 100, output "Gas".
  • Stop
[6]
10
An array called students contains 30 names.
Write an algorithm using iteration (a loop) to search through the array to find if the name "Sarah" exists.
  • If "Sarah" is found, output "Found" and stop the loop.
  • If the loop finishes and "Sarah" has not been found, output "Not Found".
[7]
END OF QUESTION PAPER