Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Edexcel GCSE CS · 6.2

Programming Fundamentals in Python

Section A: 10 multiple choice questions, 10 marks. Section B: 4 written questions, 13 marks. Total 23 marks.

Name

Class

Date

Mark  / 23

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What is the value of 17 // 5 in Python?1
    1. A2
    2. B3
    3. C3.4
    4. D85
  2. 2What is the value of 17 % 5?1
    1. A2
    2. B3
    3. C3.4
    4. D12
  3. 3What does input() always return?1
    1. AA float
    2. BAn integer
    3. CThe type the user typed
    4. DA string
  4. 4Which values does range(1, 5) produce?1
    1. A1, 2, 3, 4, 5
    2. B1, 5
    3. C0, 1, 2, 3, 4
    4. D1, 2, 3, 4
  5. 5What is wrong with if grade == 'A' or 'B':?1
    1. AThe right hand side of or is not a complete comparison
    2. Bor is not valid in Python
    3. Cgrade has not been defined
    4. DStrings cannot be compared
  6. 6What does n % 2 == 0 test?1
    1. AWhether n is a whole number
    2. BWhether n is even
    3. CWhether n is greater than 2
    4. DWhether n is positive
  7. 7Which loop should be used when the number of repeats is not known in advance?1
    1. AA for loop
    2. BEither, they behave identically
    3. CA while loop
    4. DA range loop
  8. 8Why does Python indentation matter?1
    1. AIt affects how fast the program runs
    2. BIt determines the data type
    3. CIt decides which statements are inside an if or a loop
    4. DIt is only a style convention
  9. 9What is the result of print('5' + '3') in Python?1
    1. A8
    2. B53
    3. CAn error
    4. D5 3
  10. 10Why should a while loop's condition variable be given a value before the loop?1
    1. AOtherwise the condition cannot be evaluated and the program fails
    2. BTo make the loop count controlled
    3. CSo indentation is correct
    4. DSo the loop runs faster

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1StateState the difference between a variable and a constant.2
  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
  3. 3ExplainExplain the difference between a for loop and a while loop, and give one situation in which each would be the better choice.4
  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

1B2A3D4D5A6B7C8C9B10A

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.