$30
CMPT 120-D400 Midterm Exam Sample
Coding Questions
This is a 60 minute closed book exam: notes, books, computers, calculators, electronic devices, etc. are not
permitted. Do not speak to any other students during their exam or look at their work. Please remain seated and
raise your hand if you have a question.
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
Turtle Graphics
(10 marks) Write a complete Python turtle graphics program that draws this picture:
• Assume the turtle starts at the black dot in the lower left corner facing east (i.e. to the right).
• The dimensions of the outer square are 300 by 300, and the dimensions of the inner square are 200 by 200.
• As shown by the dashed line, there is a 50 pixel gap all around.
• The only module you should import is turtle. You don’t need to write any of your own functions, but can
if you like.
• Just draw the two squares. Don’t draw the dot, numbers, or dashed lines.
300
300
200
50 200
50
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
Functions
a) (5 marks) The volume of a cone is given by this formula:
1
3
𝜋𝑟
2ℎ
h is the height of the cone, and r is the radius of the bottom circle.
The variable l in the diagram is not used in the formula.
Write a Python function called cone_volume(r, h) that uses this formula to calculate and return the volume
of a cone with the given radius and height. If r or h is 0, or less, then the function should return 0.
Use 3.14 for the value of π.
Here’s how it should work:
>>> cone_volume(2, 10.8)
45.21600000000001
>>> cone_volume(-2, 10)
0
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
b) (5 marks) Write a Python function called describe(country, capital) that returns a string saying that
capital is the capital city of country. Format the string exactly as in these examples:
>>> describe('Canada', 'Ottawa')
"Ottawa is Canada's capital city"
>>> describe('Australia', 'Canberra')
"Canberra is Australia's capital city"
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
Selection
(5 marks) Write a complete Python program that asks the user enter two numbers, x and y, and then prints:
• “x and y are the same” if x and y are equal; replace x and y in the string with their values (as shown below)
• “a is less than b”, where is a is the min of x and y, and b is the max of x and y (as shown below)
Important Don’t use the built-in Python min and max functions in your program, and don’t import any
modules.
Assume the user enters valid floats.
Here’s a sample run:
What is x? 4.2
What is y? -7
-7.0 is less than 4.2
And another:
What is x? 3.1
What is y? 3.1
3.1 and 3.1 are the same
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
Iteration
a) (5 marks) Write a complete Python program that uses a while-loop (and no other kind of loop!) to print the
multiples of 5 from 1 to 1000:
5
10
15
20
…
995
1000
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
b) (5 marks) Write a complete program that asks the user to enter a string. If they don’t enter “yes” or “no” then
it asks them to try again (as shown in the sample run). It keeps re-asking them until they enter “yes” or “no”, and
finally prints “Got it!” when they do
Here are three sample runs:
Yes or no? yep
Sorry, I don't understand that.
Yes or no? nope
Sorry, I don't understand that.
Yes or no? yes
Got it!