The University of Auckland: Total
The University of Auckland: Total
The University of Auckland: Total
SEMESTER 2, 2015
Campus: City
COMPUTER SCIENCE
TEST SOLUTIONS
Principles of Programming
NOTE:
You must answer all questions in this test.
No calculators are permitted
Answer in the space provided in this booklet.
There is space at the back for answers which overflow the allotted space.
Surname
Forenames
Student ID
Login (UPI)
Lab Time
Q1 Q4
(/39) (/14)
Q2 Q5
(/20) (/13)
Q3
TOTAL
(/14) (/100)
Question/Answer Sheet -2- CompSci 101
ID: ...........................................................
result = 3 * 4 + 4 + 2 // 3 - 3 ** 2
print("Result:", result)
Result: 7
(3 marks)
print(7 % 4, 4 % 4, 5 % 10)
3 0 5
(3 marks)
result = 1 + 2 * 3 ** 2 // 10
print("Result:", result)
Result: 2
(3 marks)
CONTINUED
Question/Answer Sheet -3- CompSci 101
ID: ...........................................................
Result: 33545
(3 marks)
word1 = "pastiche"
word2 = word1[5:] + word1[-1] + word1[2:4]
print("Letters:", word2)
Letters: cheest
(3 marks)
CONTINUED
Question/Answer Sheet -4- CompSci 101
ID: ...........................................................
0 -1 7
(3 marks)
g) Complete the following Python statement which assigns a random number which is either 8, 10
or 12 to the variable, number. You can assume that the random module has been imported..
(3 marks)
CONTINUED
Question/Answer Sheet -5- CompSci 101
ID: ...........................................................
num1 = 32
num2 = 150
num3 = 100
A
C
F
(3 marks)
CONTINUED
Question/Answer Sheet -6- CompSci 101
ID: ...........................................................
num1 = 32
num2 = 150
is_a_gift = True
amount = 200
print("Amount:", amount)
3.
5.
Amount: 178
(3 marks)
CONTINUED
Question/Answer Sheet -7- CompSci 101
ID: ...........................................................
4
8
12
16
(3 marks)
18
15
12
(3 marks)
CONTINUED
Question/Answer Sheet -8- CompSci 101
ID: ...........................................................
def mystery_1(a_list):
length = len(a_list)
last_one = a_list[-1]
count = 0
for index in range(length - 1):
if a_list[index] > last_one:
count += 1
return count
def main():
a_list = [24, 12, 30, 18, 26, 14, 32, 18]
print(mystery_1(a_list))
main()
Result: 4
(3 marks)
m) Given the following code (the right hand side of the first statement is not shown but you can
assume the at the code executes without errors):
thing1 = ...
thing2 = thing1 + [5, 7, 2]
thing2[1] = "Two"
what is the type of the two Python objects thing1 and thing2[1]?
(3 marks)
CONTINUED
Question/Answer Sheet -9- CompSci 101
ID: ...........................................................
part1 = word1[0:how_many]
part2 = word2[0:how_many]
if word1 == word2:
return False
else:
print("F")
a) Complete the call to the process() function so that the following code prints "Yes".
print("Yes")
else:
print("No")
(3 marks)
b) Complete the call to the process() function so that the following code prints "No".
print("Yes")
else:
print("No")
(3 marks)
CONTINUED
Question/Answer Sheet - 10 - CompSci 101
ID: ...........................................................
c) Complete the has_same_ending() function which is passed two string parameters, word1
and word2. The function returns True if both parameter strings have the same last letter,
otherwise the function returns False. You can assume that the parameter strings always
contain at least one character. For example, the following code:
print(has_same_ending("taro", "carrot"))
print(has_same_ending("funny", "day"))
prints:
False
True
word1_last = word1[-1]
word2_last = word2[-1]
return word2_last == word1_last
(7 marks)
CONTINUED
Question/Answer Sheet - 11 - CompSci 101
ID: ...........................................................
print(get_hollow_word("Geraldine"))
print(get_hollow_word("Simon"))
prints:
G-------e
S---n
def get_hollow_word(word):
first = word[0]
last = word[-1]
middle_length = len(word) - 2
return first + "-" * middle_length +
last
(7 marks)
CONTINUED
Question/Answer Sheet - 12 - CompSci 101
ID: ...........................................................
a) Convert the following code which uses a while loop into equivalent code which uses a
for ... in range(...) loop.
num = 2
while num < 34:
print(num)
num = num + 3
(6 marks)
CONTINUED
Question/Answer Sheet - 13 - CompSci 101
ID: ...........................................................
b) Complete the following program which continuously prompts the user for a whole number (using
the prompt "Enter number: ") until the sum of the numbers entered by the user is greater
than 20. Once the sum is greater than 20 the final line of output displays the string "Final
sum: " followed by the final sum reached (a number greater than 20). An example execution
of the completed program is shown below (the user input is shown in bold in a larger font size):
Enter number: 9
Enter number: 4
Enter number: 11
Final sum: 24
def main():
user_num = 0
total = 0
(8 marks)
main()
CONTINUED
Question/Answer Sheet - 14 - CompSci 101
ID: ...........................................................
a_list = [3, 2, 1, 5, 0, 3]
a_list[4] = a_list[a_list[2]]
print("a_list:", a_list)
a_list: [3, 9, 1, 3, 9, 3]
(6 marks)
CONTINUED
Question/Answer Sheet - 15 - CompSci 101
ID: ...........................................................
count = get_count_negatives(a_list)
def get_count_negatives(a_list):
count = 0
for num in a_list:
if num < 0:
count += 1
return count
(8 marks)
CONTINUED
Question/Answer Sheet - 16 - CompSci 101
ID: ...........................................................
a) Using the code tracing technique shown in lectures, perform a code trace for the following
program and give the output. Give the output in the space below and show the code trace in
the space provided on the next page.
def main():
print("A")
partA = "XYZ"
partB = 123
function1(partA, partB)
print("B")
main()
A
C
D XYZ123
F 2
E Z123Y
B
(6 marks)
CONTINUED
Question/Answer Sheet - 17 - CompSci 101
ID: ...........................................................
(7 marks)
CONTINUED