Programming pt1
Programming pt1
Date: ________________________
Time: 42 minutes
Marks: 31 marks
Comments:
• Line numbers are included but are not part of the algorithm.
Figure 1
1 i ← USERINPUT
2 IF i MOD 2 = 0 THEN
3 OUTPUT i * i
4 ELSE
5 OUTPUT i
6 ENDIF
The modulus operator is used to calculate the remainder after dividing one integer by another.
For example:
• 14 MOD 3 evaluates to 2
• 24 MOD 5 evaluates to 4
(a) Shade one lozenge that shows the line number where selection is first used in the
algorithm in Figure 1.
A Line number 1
B Line number 2
C Line number 3
D Line number 4
(1)
(b) Shade one lozenge that shows the output from the algorithm in Figure 1 when the user
input is 4
A 0
B 2
C 4
D 8
E 16
(1)
A Line number 1
B Line number 2
C Line number 3
D Line number 4
(1)
(d) Shade one lozenge that shows the line number that contains a relational operator in the
algorithm in Figure 1.
A Line number 1
B Line number 2
C Line number 3
D Line number 4
(1)
Figure 1
1 i ← USERINPUT
2 IF i MOD 2 = 0 THEN
3 OUTPUT i * i
4 ELSE
5 OUTPUT i
6 ENDIF
(e) Shade one lozenge to show which of the following is a true statement about the algorithm
in Figure 1.
(1)
• Line numbers are included but are not part of the program.
Figure 2
The program in Figure 2 needs to be changed so that it repeats five times using definite
(count controlled) iteration.
Shade one lozenge next to the program that does this correctly.
Write a Python program to calculate the total charge for a group of people visiting the theme
park.
You should use indentation as appropriate, meaningful variable name(s) and Python syntax in
your answer.
The answer grid below contains vertical lines to help you indent your code.
(Total 6 marks)
Extend the program in the figure above. Your answer must be written in Python.
The program should get the user to enter a word and perform a linear search on the list
fruits to find if the word is in the list or not.
You must write your own linear search routine and not use any built-in search function
available in Python.
You should use indentation as appropriate, meaningful variable name(s) and Python
syntax in your answer.
The answer grid below contains vertical lines to help you indent your code.
(7)
___________________________________________________________________
___________________________________________________________________
(1)
(Total 8 marks)
• if the final score is 21, output a message to say the player has won
• if the final score is greater than 21, output a message to say the player has lost
• if the final score is less than 21, the program generates a random number between 15 and
21 inclusive:
○ if this random number is greater than the player’s final score, output a message to
say the player has lost
○ otherwise, output a message to say the player has won.
The figure below shows the output of a program that plays this dice game.
Roll 1: 1
Roll 2: 4
Current score: 5
Would you like to roll again? yes
Roll 1: 1
Roll 2: 6
Current score: 12
Would you like to roll again? yes
Roll 1: 1
Roll 2: 2
Current score: 15
Would you like to roll again? yes
Roll 1: 6
Roll 2: 1
Current score: 22
You lost!
The first line has been written for you in the answer grid.
The dice rolls are carried out by the program generating random numbers between 1 and 6. You
will need to use the Python function random.randrange(a, b) which generates a random
integer in the range a to b starting at a but finishing one before b.
You should use indentation as appropriate, meaningful variable name(s) and Python syntax in
your answer.
The answer grid below contains vertical lines to help you indent your code.
(Total 11 marks)