You are on page 1of 12

CPSC 231 Practice Final Exam

The University of Calgary


Department of Computer Science
Lectures 01, 02, 03
Time available: 2 hours

Name:

_________________________________________________

Instructions:

This is a closed book exam: No aids allowed.


DO NOT OPEN the exam booklet until you are told to do so.
For the multiple choice questions make sure that you select the best answer to each question on
the computer bubble sheets using an HB pencil or darker.
For the written questions write your answer in the space provided.
Unless otherwise indicated you can assume that the programs and program fragments will
translate and run. Any syntax or run-time errors are unintentional unless it is clearly stated in
the question that such an error could exist in the code.

Page 2 of 12

Student ID ___________________________________
Multiple Choice Questions (Each question is worth 1 mark)
1. When designing a procedural solution, you
a) must consider the tasks that must be completed to solve the problem.
b) must consider the problem data and the operations to be performed on that data.
c) are always guaranteed to produce an error-free solution.
d) must create a solution that has a lot of logic errors.
e) must create a solution with relatively few logic errors.
2. Which of the following describes the object-oriented paradigm?
a) Developers think of programs in terms of interacting entities
b) Developers think of programs as a sequence of function evaluations
c) Developers think of programs as sets of logical expressions
d) Developers think of programs as combinations of one or more procedures
3. What are the contents of the variable X after the following code is executed?
import turtle
X = []
for row in range(0, 10) :
for col in range(0, 5) :
tess = turtle.Turtle()
X.append(tess)

a)
b)
c)
d)

X contains 15 references to the same turtle called tess, an instance of the Turtle class.
X contains 15 references to 15 different instances of the Turtle class
X contains 50 references to the same turtle called tess, an instance of the Turtle class.
X contains 50 references to 50 different instances of the Turtle class

4. When programming a solution, you cycle through editing and running/testing the code until
your program is completed. When running your Python code, you are verifying that your code
a) Has no syntactic errors.
b) Has no run-time errors.
c) Has no logic errors.
d) All of the above.
5. Which of the following tasks does the Python interpreter perform?
a) Checks the code to ensure it is syntactically correct.
b) Translates python code to machine code.
c) Runs the code provided.
d) All of the above.
e) None of the above.

Page 3 of 12
6. What will be printed as the result of the following program?
matcher = [[1, 'abcde'],[2, 'fghij'],[4, 'klmno'],[8, 'pqrst']]
print(len(matcher))
a) 4
b) 8
c) 20
d) 24
7. What is the value of total when the following code completes?
nums = [[1,6], [2, 7], [3,8], [4, 9], [5, 10]]
total = 0
for x in nums :
total = total + x[1]
a) 12345
b) 15
c) 678910
d) 40
e) 55
8. Which of the following types can be stored in a list?
a) list
b) str
c) Turtle
d) int
e) All of the above
f) None of the above
9. What does it mean for a data type to be immutable?
a) Once an instance of the type is created, the instance cannot be modified.
b) It can only be used for global constants.
c) It is an instance variable in a class.
d) It can only be used as a local variable.
10. What language or representation can be understood and directly executed by a computer?
a) Binary
b) Html
c) Java
d) Python
e) All of the above

Page 4 of 12
11. What is the result of the following function if the parameter X is passed an uppercase letter?
def convert(X):
return ord(X) - ord('A')
a) The ASCII character corresponding to the number X
b) The numerical value corresponding to the location of X in the ASCII table
c) 0
d) The numerical value corresponding to the location of X in the alphabet
12. Which of the following commands will ensure that the function click is called when a turtle
screen is clicked. Let s be initialized to turtle.Screen().
a) s.onscreenclick(click)
b) s.onscreenclick('click')
c) s.onscreenclick(click(x,y))
d) s.onscreenclick('click(x,y)')
13. What is the value of x after the following two lines are executed?
myList = [1,2,3,4,5,6,7]
x = myList[1]

a)
b)
c)
d)

1
2
[1,2,3,4,5,6,7]
[2,3,4,5,6,7]

14. What is the value of x after the following two lines are executed?
myStr = '1234567'
x = myStr[1:]

a)
b)
c)
d)

'1'
'2'
'1234567'
'234567'

15. What will be printed to the console due to the following code?
myList = [1,2,3,4,5,6,7]
mineToo = myList
mineToo[2] = 8
print(myList)

a)
b)
c)
d)

[1,2,3,4,5,6,7]
[1,8,3,4,5,6,7]
[1,2,8,4,5,6,7]
8

Page 5 of 12
16. What will be printed to the console due to the following code?
myList = [1,2,3,4,5,6,7]
mineToo = myList[:]
mineToo[2] = 8
print(myList)

a)
b)
c)
d)

[1,2,3,4,5,6,7]
[1,8,3,4,5,6,7]
[1,2,8,4,5,6,7]
8

17. What will be printed to the console due to the following code?
myList = [[1],[2],[3],[4],[5],[6],[7]]
mineToo = myList[:]
mineToo[2][0] = 8
print(myList)

a)
b)
c)
d)
e)

[[1],[2],[3],[4],[5],[6],[7]]
[[1],[8],[3],[4],[5],[6],[7]]
[[1],8,[3],[4],[5],[6],[7]]
[[1],[2],[8],[4],[5],[6],[7]]
[[1],[2],8,[4],[5],[6],[7]]

18. What will happen when we run the following code if the file heythere.txt does NOT exist in the
current directory (same directory as the code that is run)?
fileHandle = open('heythere.txt','r')

a)
b)
c)
d)

The file heythere.txt is created.


Nothing happens since the file does not exist.
The program will raise an error
We cant predict what will happen.

19. Consider the following class:


class Point:
def __init__(self,xval,yval):
self.x = xval
self.y = yval

What would be printed to the console if we executed the following statements?


p1 = Point(1,2)
p2 = Point(3,4)
print(p1.x,p1.y)

a)
b)
c)
d)

0 0
1 2
3 4
1 2 3 4

Page 6 of 12
20. What will be printed to the console due to the following code?
def fun(aList):
if (len(aList) >= 5) :
aList[4] = 'item'
myList = [1,2,3,4,5,6,7]
fun(myList)
print(myList)

a) [1,2,3,4,5,6,7,item]
b) [1,2,3,4,5,6,7]
c) [1,2,3,4,item,6,7]
d) [1,2,3,item,5,6,7]
21. What will be printed to the console due to the following code?
def fun(aString):
if (len(aString) >= 5) :
aString = 'item'
myString = '1,2,3,4,5,6,7'
fun(myString)
print(myString)

a)
b)
c)
d)

1,2,3,4,5,6,7
item
1,2,3,4,5,6,7,item
(empty string)

The code below is used by the remaining three questions.


1) def fun(x,y):
2)
a = x + 10
3)
b = y * 2
4)
print(z)
5)
return(a-b)
6) x = 2
7) z = 3
8) print(fun(z,x) * 3)
9) print(a)

22. When we run the above code, what will be printed due to line 4?
a) 2
b) 3
c) 9
d) 27
e) Runtime error since z is not defined in function fun.

Page 7 of 12
23. When we run the above code, what will be printed due to line 8?
a) 2
b) 3
c) 9
d) 27
e) Runtime error, since x is not defined at line 8 in the code.
24. When we run the above code, what will be printed due to line 9?
a) 2
b) 3
c) 9
d) 27
e) Runtime error, since a is not defined at line 9 in the code.

Page 8 of 12

Written Answer Questions


1.

[4 marks] Give a trace for the following code. Make sure you clearly show all recursive function
calls, values returned for each call and where the value is returned to.
def recursion_fun(x) :
if (x <= 1) :
return 1
if (x < 5) :
y = recursion_fun(x-3)
else :
y = recursion_fun(x-2)
return x + y
z = recursion_fun(8)
print(z)

2. [5 marks] Write a function that takes one parameter: a filename. The file contains numbers
where each number is on a line of its own. The function should place all the numbers in a list
and return this list. If the function encounters problems opening or reading from the file, the
function should return the empty list.

Page 9 of 12
3.

[5 marks] Write a function that takes a 2-dimensional list of numbers as an argument. In other
words, the argument is a list that contains one or more lists of numbers. You may assume that
each inner list has the same length. For example, [[1,2,3],[4,5,6],[7,8,9],[1,5,7]]. Such a 2dimensional list can be seen as a matrix or table and written as follows.
1

Your function must sum the values in the columns and return the result
as a list. On the example provided, your function would return
[13,20,25].

4. [5 marks] Give Python code that opens a window and when the user clicks inside that window, a
circle or dot is placed at the location the user clicked.

Page 10 of 12
5. Consider the definition of class Fraction for this question.
class Fraction :
def __init__(self, numer, denom) :
self.numerator = numer
self.denominator = denom
def as_string(self) :
return str(self.numerator) + '/' + str(self.denominator)
def add(self, other_fraction) :
common_denom = self.denominator * other_fraction.denominator
added_numer = self.numerator * other_fraction.denominator + \
other_fraction.numerator * self.denominator
added_fraction = Fraction(added_numer, common_denom)
return added_fraction

a) [1 mark] Which part of the above code is the constructor for the class Fraction?

b)

[1 mark] Give a statement that would cause the code in the constructor to be executed.

c) [1 mark] What method or methods can be applied to a variable of type Fraction?


(Recall that if inst is a variable of type Class1 and m1() is a method of Class1 then
the statement inst.m1() applies the method m1 to instance inst.)

Page 11 of 12
d) [2 marks] Write a function that creates three variables called: half, quarter and added.
1. half should be of type Fraction and represent the fraction . 2. quarter should
also be of type Fraction and represent the fraction . 3. added should be of type
Fraction and contain the result of adding half and quarter together. The fraction
stored in added should be the result of calling the method add in the class Fraction.
Finally, the function should print the value stored in added by using the as_string
method in class Fraction.

e) [2 marks] Add one additional method, called mult, to the class. This method should
take as an argument an integer and return a Fraction object that is the result of
multiplying itself with the integer argument. For example, if f is a variable of type
Fraction representing , then calling f.mult(3) will return a Fraction object that
represents the fraction 9/4. You are not required to simplify the fraction.

Page 12 of 12
6. [ 4 marks] Consider the following function that prompts the user for a valid day and month:
def is_valid_date(day,month) :
date_valid = False
message =
if (month < 1 or month > 12) :
message = message + Month is invalid\n
elif (day < 1) :
message = message + Day is invalid\n
elif (month in [1,3,5,7,8,10,12] and day > 31) :
message = message + Day > 31 is invalid for month\n
elif (month in [4,6,9,11] and day > 30) :
message = message + Day > 30 is invalid for month\n
elif (month == 2 and day > 29) :
message = message + "Month 2 has at most 29 days\n")
else :
date_valid = True
return date_valid, message

Provide 4 distinct test cases that would be part of a thorough test plan for this function. No two
should test the same code. For each test case, provide the input, output and a brief description
on what is being tested.
test1:
Input:________________________________________________________________________
Expected Output: ______________________________________________________________
Description: ___________________________________________________________________
test2:
Input:________________________________________________________________________
Expected Output: ______________________________________________________________
Description: ___________________________________________________________________
test3:
Input:________________________________________________________________________
Expected Output: ______________________________________________________________
Description: ___________________________________________________________________
test4:
Input:________________________________________________________________________
Expected Output: ______________________________________________________________
Description: ___________________________________________________________________

You might also like