2.3.1 Defensive design Practice Questions
GCSE · Paper 2 · Computational thinking, algorithms and programming · 9 practice questions · 35 marks
Exam questions for 2.3.1 Defensive design
9 practice questions (35 marks) with mark schemes
Answer each question, reveal the mark scheme, then award yourself marks. Scores and answers are saved in this browser so you can see which topics need more work.
Practice questions
- Question 1Practice question · J277/02 style2 marks
A school uses a program to let students enter their lunch order. Give two examples of misuse that the programmer should anticipate.
Show mark scheme
Any two from: Student types text where a number is needed / the wrong data type (1) Student leaves a field empty (1) Student enters a number that is too big or too small, such as 0 or 500 meals (1) Someone tries to log in as another student by guessing a password (1) Student makes a typing mistake such as a wrong format (1)
Mark yourself: how many marks did you get?/ 2 - Question 2Practice question · J277/02 style3 marks
(a) State what is meant by authentication. [1] (b) Give one way a program can authenticate a user. [1] (c) Explain why a program should limit the number of times a user can enter a wrong password. [1]
Show mark scheme
(a) Confirming that a user is who they say they are (1) (b) Username and password (1) (c) It stops someone from guessing the password again and again until they get it right / it protects the account from being broken into (1)
Mark yourself: how many marks did you get?/ 3 - Question 3Practice question · J277/02 style4 marks
A program checks a login.
01 attempts = 0 02 loggedIn = false 03 while attempts < 3 AND loggedIn == false 04 user = input("Username") 05 password = input("Password") 06 if user == "zara" AND password == "Blue77" then 07 loggedIn = true 08 else 09 attempts = attempts + 1 10 endif 11 endwhile 12 if loggedIn == true then 13 print("Welcome") 14 else 15 print("Account locked") 16 endif(a) A user enters these pairs in order: zara and red, then zara and blue77, then zara and Blue77. State what is printed. [2] (b) Another user enters a wrong username and password three times. State what is printed and how many times the user is asked for a username. [2]
Show mark scheme
(a) Welcome (1). Method: the first pair is wrong and the second is wrong because blue77 does not match Blue77 (case matters). That makes attempts 2. The third pair matches, so loggedIn becomes true and the loop ends. (1 for correct working if the answer is wrong) (b) Account locked (1) and 3 times (1). Method: attempts reaches 3, so the loop condition is false.
Mark yourself: how many marks did you get?/ 4 - Question 4Practice question · J277/02 style3 marks
A website has a sign-up form. State the type of validation check that each rule below describes. (a) The postcode box must not be left empty. [1] (b) The number of guests must be between 1 and 8. [1] (c) The password must have at least 10 characters. [1]
Show mark scheme
(a) Presence check (1) (b) Range check (1) (c) Length check (1)
Mark yourself: how many marks did you get?/ 3 - Question 5Practice question · J277/02 style5 marks
A cinema program asks the user for the number of tickets they want. The number must be a whole number from 1 to 6 inclusive. Write an algorithm, in the OCR Exam Reference Language or a high-level programming language, that asks for the number and keeps asking until a valid number is entered. It should print an error message each time the number is not valid. [5]
Show mark scheme
Model answer:
quantity = int(input("How many tickets (1 to 6)?")) while quantity < 1 OR quantity > 6 print("Enter a number from 1 to 6") quantity = int(input("How many tickets (1 to 6)?")) endwhileAward 1 mark for each of: Input of the quantity as an integer, with a prompt (1) A loop that repeats while the value is not valid (1) Correct condition covering both limits, with OR: below 1 or above 6 (1) An error message printed inside the loop (1) The user is asked for a new value inside the loop (1) A do ... until loop with the opposite condition (quantity >= 1 AND quantity <= 6) is also correct. Do not award the condition mark if AND is used with the invalid limits, because that can never be true.
Mark yourself: how many marks did you get?/ 5 - Question 6Practice question · J277/02 style6 marks
A program checks a product code.
01 code = input("Enter product code") 02 if code == "" then 03 print("Nothing entered") 04 elseif code.length != 6 then 05 print("Code must be 6 characters") 06 elseif code.left(2) != "PR" then 07 print("Code must start with PR") 08 else 09 print("Accepted") 10 endif(a) Name the type of validation check made on line 02, line 04 and line 06. [3] (b) State what is printed when the user enters PR4821, and what is printed when the user enters XY1234. [2] (c) The program accepts PR4821, but no such product exists. Explain why validation did not stop this. [1]
Show mark scheme
(a) Line 02: presence check (1) Line 04: length check (1) Line 06: format check (1) (b) PR4821: Accepted (1) XY1234: Code must start with PR (1). Method: XY1234 has 6 characters, so the length check passes, but the first two characters are not PR. (c) Validation only checks that data is sensible and follows the rules. It does not check that the data is correct or that the product exists (1)
Mark yourself: how many marks did you get?/ 6 - Question 7Practice question · J277/02 style6 marks
This program works out the price of a group booking at a theme park. It is hard to read and to maintain.
01 n = int(input("Adults")) 02 m = int(input("Children")) 03 t = n * 12 + m * 7 04 if n + m > 5 then 05 t = t * 0.9 06 endif 07 print(t)Rewrite the program so that it is easier to maintain. You must use a function, meaningful names, indentation and comments. The new program must do the same job. [6]
Show mark scheme
Model answer:
// Work out the price of a group booking function calculateTotal(adults, children) total = adults * 12 + children * 7 // Groups of more than 5 people get 10% off if adults + children > 5 then total = total * 0.9 endif return total endfunctionadults = int(input("Adults")) children = int(input("Children")) print(calculateTotal(adults, children))Award 1 mark for each of: Meaningful names used for the variables (1) Meaningful name for the function and its parameters (1) The code inside the if is indented (1) The code inside the function is indented (1) At least one useful comment (1) The calculation is in a function that takes the numbers as parameters, returns the total, and is called with the two inputs so the output is unchanged (1)
Mark yourself: how many marks did you get?/ 6 - Question 8Practice question · J277/02 style2 marks
Explain how comments make a program easier to maintain. [2]
Show mark scheme
Comments explain what a section of code does / why it was written that way (1) So another programmer can understand it quickly without working it out / so they can safely change or fix it (1)
Mark yourself: how many marks did you get?/ 2 - Question 9Practice question · J277/02 style4 marks
A team of programmers is writing a large program together. (a) Give two reasons why using sub programs makes the program easier to maintain. [2] (b) Explain why the team should agree on a naming convention for their variables. [2]
Show mark scheme
(a) Any two from: Each sub program does one job, so it is easier to find where a fault is (1) A sub program can be tested on its own (1) The code can be reused, so there is only one place to change it (1) The main program is shorter and easier to read (1) (b) Names are consistent, so every programmer can read each other's code (1) Meaningful names show what each variable holds, so the code is easier to understand and to change (1)
Mark yourself: how many marks did you get?/ 4
- •Practice answering questions under timed conditions
- •Check your answers against the mark scheme carefully
- •Focus on understanding the marking points, not just memorising answers
- •Come back to the questions you dropped marks on a few days later