$30
CMPT 120-D400 Mini Midterm Exam 1
This is a 30 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.
1/3 the size!
3/5 the fun!
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
Security Codes
a) (5 marks) Write a function call rand_digit() that returns, as a string, a random digit (0 to 9). Import any
modules required to make it work.
Make this function reasonably efficient: it shouldn’t do any unnecessary work.
>>> rand_digit()
'4'
>>> rand_digit()
'0'
>>> rand_digit()
'4'
>>> rand_digit()
'7'
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
b) (7 marks) Write a function called rand_digits(n) that returns a string of n randomly chosen digits. Use
rand_digit() from the previous question to help write this function. You must use a loop and the
accumulation pattern in your answer.
If n is 0, or less, then return the empty string.
Make this function reasonably efficient: it shouldn’t do any unnecessary work.
>>> rand_digits(3)
'824'
>>> rand_digits(10)
'3487725783'
>>> rand_digits(-2)
''
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
c) (5 marks) Write a function called security_code(style) that returns a string according to these rules:
• If style is 'short', a string of 5 randomly chosen digits is returned.
• If style is 'long', a string of the form 'dddd-dddddd', where each d is a randomly chosen digit. In
other words, the returned string consists of 4 random digits, followed by a '-', and then 6 more random
digits.
• If style is anything other than 'short' or 'long', return the string 'error'.
Use rand_digits from the previous question to help write this function.
Make this function reasonably efficient: it shouldn’t do any unnecessary work.
>>> security_code('short')
'32461'
>>> security_code('short')
'40103'
>>> security_code('long')
'4965-106233'
>>> security_code('long')
'0581-327653'
>>> security_code('medium')
'error'
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
Multiple Choice
For each of the following questions, fill in the one best answer on the answer sheet.
Every correct answer is worth 1 mark. Incorrect answers, unanswered questions, questions with more than one
answer, or questions with illegible answers, are worth 0.
1) Consider these two statements:
i) Python is popular language for data science and machine learning.
ii) Python has a reputation for making extremely fast-running programs.
Which one of these statements most accurately describes the truth values of i) and ii)?
A. i) and ii) are both true
B. i) and ii) are both false
C. i) is false and ii) is true
D. i) is true and ii) is false
2) Consider these two statements:
i) A Python variable name can start with a digit.
ii) A Python variable name can contain a period character.
Which one of these statements most accurately describes the truth values of i) and ii)?
A. i) and ii) are both true
B. i) and ii) are both false
C. i) is false and ii) is true
D. i) is true and ii) is false
Instructor: T. Donaldson CMPT 120-D400, SFU Surrey Fall 2022
i = 0 # line 1
while i < 100: # line 2
print(i) # line 3
i += 1 # line 4
Program Listing 1
3) This question is about Program Listing 1.
What is the last number printed?
A. 98
B. 99
C. 100
D. 101
E. some other number
4) This question is about Program Listing 1.
Suppose line 3 and line 4 are swapped. What is the last number printed?
A. 98
B. 99
C. 100
D. 101
E. some other number
5) This question is about Program Listing 1.
The program would print the same output if “i = 0” in line 1 was replaced with “i=0” (i.e.
spaces are removed).
A. True
B. False
6) This question is about Program Listing 1.
In line 1, it’s possible to initialize i with some int value so that the loop runs forever.
A. True
B. False