MOCK EXAM_________________________________________________________
FACULTY OF LIBERAL ARTS & SCIENCES
Campus Maritime Greenwich
School Computing & Mathematical Sciences
Level 4
Academic Stage Undergraduate
TITLE OF PAPER Programming Foundations – MOCK PAPER 1
COURSE CODE COMP1753
Time 1 hour 30 minutes
Answer ALL questions
You may use your log book and a text book during the exam. You may also use a
computer and online Python documentation as reference material.
To submit your answers, go to the COMP1753 Moodle page and
enter your solutions in the “mock exam 1” submission link.
Programming Foundations
COMP1753
Page 1 of 9
MOCK EXAM_________________________________________________________
Answer all questions
1. Which of the following are keywords in Python (choose 2)?
A. and
B. do
C. elseif
D. var
E. break
[5 marks]
2. Which of the following are legal variable names (choose 2)?
A. #five_pence_piece
B. five pence piece
C. 5_pence_piece
D. five_pence_piece
E. fivepencepiece
[5 marks]
3. How many times will xx appear if printx is called with the parameter i being
set to 0 (choose 1)?
def printx(i):
while i < 3:
print("xx")
i += 1
return i
A. xx will not appear
B. xx will appear 2 times
C. xx will appear 3 times
D. xx will appear 4 times
E. xx will go on printing forever
[5 marks]
Programming Foundations
COMP1753
Page 2 of 9
MOCK EXAM_________________________________________________________
4. Identify all correct list declarations (choose 2).
A. alphabet = "A", "B", "C"
B. alphabet = ["A", "B", "C"]
C. alphabet = {"A", "B", "C"}
D. alphabet = "ABC"
E. alphabet = []
[5 marks]
5. What will be printed out when you run the following code (choose 1)?
nums = ["1", "2", "3", "4", "5"]
print(nums[1] + nums[2])
A. 12
B. "1""2"
C. 23
D. "2""3"
E. There will be a run-time error
[5 marks]
6. Identify all legal Python operators (choose 2).
A. +=
B. =+
C. !=
D. +-
E. <>
[5 marks]
7. Identify the lines containing a legal Python comment (choose 2).
A. # This is a comment
B. // This is a comment
C. " This is a comment "
D. """"This is a comment""""
E. / * This is a comment */
[5 marks]
Programming Foundations
COMP1753
Page 3 of 9
MOCK EXAM_________________________________________________________
8. What value will be printed if f1 is called (choose 1)?
def f1():
result = f2(4)
print(result)
def f2(par):
if par < 3:
return par
elif par == 3:
return par * 2
else:
return par * 3
A. There will be a run-time error because f1 does not return anything
B. 4
C. 8
D. 12
E. 16
[5 marks]
9. How do you declare a function in Python (choose 1)?
A. def func()
B. def = func()
C. def: func()
D. def func():
E. func():
[5 marks]
10. How do you invoke (call) a function named "func" (choose 1)?
A. func()
B. func.invoke()
C. call func()
D. call function func()
E. call.func()
[5 marks]
Programming Foundations
COMP1753
Page 4 of 9
MOCK EXAM_________________________________________________________
11. What will be printed out if this code is run (choose 1)?
helloWorld = ["hello", "world", "!"]
print(helloWorld[3])
A. There will be a run-time error
B. helloWorld[3]
C. hello world !
D. !
E. undefined
[5 marks]
The following code is used for questions 12 and 13.
01 n = 4
02 for i in range(1, n+1):
03 output = ""
04 for j in range(1, 6):
05 output += f" {j * i}"
06 print(output)
12. What will be output when the code is run (choose 1)?
A. 1
2 4
3 6 9
4 8 12 16
B. 1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
C. 1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
D. 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
E. There will be a run-time error
[5 marks]
Programming Foundations
COMP1753
Page 5 of 9
MOCK EXAM_________________________________________________________
13. Assuming any run-time errors are fixed, the output still has a formatting
problem where the numbers 10 and above take up two spaces, whilst numbers
below 10 take up 1 space. What should be done to fix this and make all the
numbers line up (choose 1)?
A. The following code should be inserted between lines 04 and 05
if j * i > 10:
output += " "
B. The following code should be inserted between lines 04 and 05
if j * i >= 10:
output += " "
C. The following code should be inserted between lines 04 and 05
if j * i < 10:
output += " "
D. The following code should be inserted between lines 04 and 05
if j * i <= 10:
output += " "
E. There is no way to fix this
[5 marks]
14. What will be printed out when you run the following code (choose 1)?
sum = 0
for count in range(3):
sum = sum + 3
print(f"The value of sum = {sum}")
A. There will be a run-time error
B. The value of sum = 0
C. The value of sum = 3
D. The value of sum = 6
E. The value of sum = 9
[5 marks]
Programming Foundations
COMP1753
Page 6 of 9
MOCK EXAM_________________________________________________________
The following code is used for questions 15 and 16. It is part of a program which
calculates the cost of tickets.
The user inputs the number and type of tickets that they require and the program
calculates the cost and prints the result.
number = int(input("How many tickets? "))
if number <= 0:
print("Please enter a positive number")
return
cost = 0
type = input("What type? ")
if type == "S": # Standard
cost = 8
elif type == "C": # Concession
cost = 6
elif type == "P": # Premium
cost = 10
else:
print(f"Ticket type {type} unrecognised")
return
if number > 5: # discount
cost -= 1
cost *= number
print(f"That will be £{cost:.2f}")
15. What is the output if the user enters “6” and “Premium” (choose 1)?
A. There will be a run-time error
B. That will be £10.00
C. That will be £54.00
D. That will be £60.00
E. Ticket type Premium unrecognised
[5 marks]
16. What is the output if the user enters “10” and “C” (choose 1)?
A. There will be a run-time error
B. That will be £50.00
C. That will be £60.00
D. That will be £10.00
E. Ticket type C unrecognised
[5 marks]
Programming Foundations
COMP1753
Page 7 of 9
MOCK EXAM_________________________________________________________
17. What will be printed out when you run the following code (choose 1)?
n = 2
for i in range(2, 0, -1):
n = n + n
print(f"The value of n = {n}")
A. There will be a run-time error
B. The value of n = 2
C. The value of n = 4
D. The value of n = 8
E. The loop will never finish executing
[5 marks]
The following code is used for questions 18, 19 and 20.
It is part of a program which interacts with files.
def my_function(dirname, search):
files = os.listdir(dirname)
for file in files:
path = dirname + "\\" + file
if os.path.isdir(path):
print(path)
my_function(path, search)
elif path.endswith(".py"):
if search in file.lower():
print("FOUND: " + path)
try:
root_path = os.curdir
search = input("Filename? ").lower()
if os.path.isdir(root_path):
my_function(root_path, search)
except OSError as err:
print(err)
print("Stopping, can't access files.")
Programming Foundations
COMP1753
Page 8 of 9
MOCK EXAM_________________________________________________________
18. What does this code do (choose 2)?
A. It lists any folders it finds
B. It lists any files it finds
C. It searches for files containing the search string
D. It searches for files with names that match the search string
E. It searches for files with names that contain the search string
[5 marks]
19. What programming techniques are used in this code (choose 2)?
A. Boolean variables
B. Dictionaries
C. Lists
D. Iteration (loops)
E. Sets
[5 marks]
20. What would happen if the line
my_function(path, search)
in the middle of the program, was commented out (choose 1)?
A. The code would not run because of a syntax error
B. The code would still work but there would be a run-time error
C. The code would still work because this line does nothing
D. The code would still work but not report any files it finds
E. The code would still work but only for files in the current folder
[5 marks]
Programming Foundations
COMP1753
Page 9 of 9