Edexcel GCSE CS · 6.2
Programming Fundamentals in Python
Name
Class
Date
Mark / 23
Section A
Circle the letter of the correct answer. One mark each.
- 1What is the value of 17 // 5 in Python?1
- A2
- B3
- C3.4
- D85
- 2What is the value of 17 % 5?1
- A2
- B3
- C3.4
- D12
- 3What does input() always return?1
- AA float
- BAn integer
- CThe type the user typed
- DA string
- 4Which values does range(1, 5) produce?1
- A1, 2, 3, 4, 5
- B1, 5
- C0, 1, 2, 3, 4
- D1, 2, 3, 4
- 5What is wrong with if grade == 'A' or 'B':?1
- AThe right hand side of or is not a complete comparison
- Bor is not valid in Python
- Cgrade has not been defined
- DStrings cannot be compared
- 6What does n % 2 == 0 test?1
- AWhether n is a whole number
- BWhether n is even
- CWhether n is greater than 2
- DWhether n is positive
- 7Which loop should be used when the number of repeats is not known in advance?1
- AA for loop
- BEither, they behave identically
- CA while loop
- DA range loop
- 8Why does Python indentation matter?1
- AIt affects how fast the program runs
- BIt determines the data type
- CIt decides which statements are inside an if or a loop
- DIt is only a style convention
- 9What is the result of print('5' + '3') in Python?1
- A8
- B53
- CAn error
- D5 3
- 10Why should a while loop's condition variable be given a value before the loop?1
- AOtherwise the condition cannot be evaluated and the program fails
- BTo make the loop count controlled
- CSo indentation is correct
- DSo the loop runs faster
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1StateState the difference between a variable and a constant.2
- 2WriteWrite a Python program that asks the user for a number of seconds and prints the equivalent number of whole minutes and remaining seconds.4
- 3ExplainExplain the difference between a for loop and a while loop, and give one situation in which each would be the better choice.4
- 4ExplainA student writes age = input("How old are you? ") and then if age >= 18:. The program crashes. Explain why and how to correct it.3
Mark scheme
Section A
Section B
1 State the difference between a variable and a constant. 2
One mark for each of the following, to a maximum of 2.
- A variable's value can change while the program is running
- A constant holds a value that should not change while the program runs
Model answer. A variable is a named location whose value can be changed while the program is running. A constant holds a value that is fixed and should not be changed during the run, and in Python this is shown by writing the name in capitals.
2 Write a Python program that asks the user for a number of seconds and prints the equivalent number of whole minutes and remaining seconds. 4
One mark for each of the following, to a maximum of 4.
- Reads input and casts it to an integer
- Uses integer division to find the minutes
- Uses modulus to find the remaining seconds
- Prints both values with a clear message
Model answer. A correct program is: total = int(input("Enter the number of seconds: ")) then minutes = total // 60 then seconds = total % 60 then print(f"That is {minutes} minutes and {seconds} seconds"). The input must be cast with int() because input returns a string, integer division gives the number of complete minutes, and the modulus gives what is left over after those whole minutes have been taken out.
3 Explain the difference between a for loop and a while loop, and give one situation in which each would be the better choice. 4
One mark for each of the following, to a maximum of 4.
- A for loop repeats a known number of times
- A while loop repeats while a condition is True, for an unknown number of repeats
- Gives a sensible for loop use, such as processing every item in a list
- Gives a sensible while loop use, such as validating input until it is acceptable
Model answer. A for loop repeats a number of times that is known before the loop starts, which is why it is called count controlled. Working through every item in a list of forty names is a for loop task, because the length of the list is known. A while loop repeats for as long as its condition remains True, and the number of repeats is not known in advance, which makes it condition controlled. Repeatedly asking a user to enter a value until they enter a valid one is a while loop task, because there is no way to know how many attempts they will need.
4 A student writes age = input("How old are you? ") and then if age >= 18:. The program crashes. Explain why and how to correct it. 3
One mark for each of the following, to a maximum of 3.
- input returns a string, so age holds text rather than a number
- Comparing a string with an integer causes a type error in Python
- Cast the input to an integer, for example age = int(input(...))
Model answer. The input function always returns a string, so after the first line age holds the characters the user typed rather than a number. The comparison then asks Python to decide whether a string is greater than or equal to an integer, which is not a comparison Python can make, so it raises a TypeError and the program stops. The correction is to convert the input as it is read, by writing age = int(input("How old are you? ")), after which the comparison is between two integers and works as intended.