Time allowed: 45 minutes
| Centre number |
| | | |
|
Candidate number |
| | | |
| First name |
|
Last name |
INSTRUCTIONS
- Use black ink.
- Answer all the questions.
INFORMATION
- The total mark for this paper is 40.
- The marks for each question are shown in brackets [ ].
Turn over
Section A: Selection & Logic
1
Complete the code below so that it correctly validates a user's password length.
- If the password is shorter than 5 characters, output "Too short".
- If it is 5 to 10 characters (inclusive), output "Acceptable".
- If it is longer than 10 characters, output "Too long".
password = input("Enter password: ")
length = len(password)
IF length < 5:
print("Too short")
length <= 10:
print("Acceptable")
:
print("Too long")
[2]
2
The OCR Exam Reference Language (ERL) allows the use of switch (or case) statements.
Look at the following ERL code:
switch choice:
case "A":
print("Add")
case "S":
print("Subtract")
default:
print("Invalid")
endswitch
(a) Rewrite this algorithm using Python (or ERL) using only
if,
elif, and
else.
[3]
(b) Explain
one benefit of using a switch statement instead of multiple if statements when checking a single variable against many values.
[1]
3
State the final output of this code. Pay close attention to the order (Sequence) of execution.
x = 10
if x > 5:
x = x + 5
if x > 12:
x = x - 2
print(x)
(a) Output:
[1]
(b) If the second
if was changed to an
elif, what would the output be?
[1]
Turn over
Section B: Iteration Theory & Debugging
4
A programmer is writing a game.
Scenario A: The player runs around the track 5 times.
Scenario B: The player stays in the "Game Over" screen until they press 'Restart'.
(a) Identify the most appropriate type of loop for Scenario A and explain your choice.
[2]
(b) Identify the most appropriate type of loop for Scenario B and explain your choice.
[2]
5
The following code is intended to print the numbers 1, 2, 3, 4, 5.
However, it contains a
Logic Error (Infinite Loop).
num = 1
while num <= 5:
print(num)
# End of loop
(a) Explain why this loop becomes infinite.
[1]
(b) Write the line of code that must be added to fix this error.
[1]
6
The student wants to print the even numbers: 0, 2, 4, 6, 8.
They wrote:
for i in range(8): print(i)
(a) State the actual output of the student's code.
[1]
(b) Rewrite the
for statement (header only) to correctly produce 0, 2, 4, 6, 8.
[2]
Turn over
Section D: Refinement & Mastery
9
A student has written this inefficient code to validate a menu choice (must be 1, 2, or 3).
valid = False
choice = int(input("Enter choice"))
if choice == 1:
valid = True
if choice == 2:
valid = True
if choice == 3:
valid = True
if valid == False:
print("Error")
Rewrite this algorithm to be more efficient.
- Use a single IF statement with logical operators (OR / AND).
- Include the error message using ELSE.
- Do not use the boolean variable
valid.
[4]
10
Write a program that calculates the average score of a class test.
The program must:
- Ask the user to input the number of students in the class.
- Use a Count-Controlled Loop (for) to ask for the score of each student.
- Inside the loop:
- Add the score to a running total.
- If a score is less than 0, output "Invalid" and subtract it from the total (or do not add it).
- After the loop, calculate the average.
- Output the average.
[6]
11
Write an algorithm using a
While Loop that:
- Asks the user to guess a "Secret Number" (e.g., 7).
- Keeps asking until the user guesses correctly.
- Once guessed correctly, output "Found it!".
[3]
END OF QUESTION PAPER