Course website for the Introduction to Computer Science I by James Tam | Return to the course web page |
◦ Lecture section (01, 02 or 03),
◦ Class name and number (CPSC 231),
◦ Last name and initials
◦ ID number.
There are 21 multiple choice questions on the final exam. Make sure you bring an HB or darker pencil to answer these questions on a bubble sheet. Do not use a pen to fill in any of the bubbles (not even the contact information).
To ensure you do well in this section, make sure you are able to do the following:
1. Identify the number of times (the body of) a loop will execute.
2. Know the basic milestones in the history of Computer Science since the 1940s as identified in class.
3. Understand the challenges faced by the various fields in Computer Science, as discussed in class.
4. Understand the differences and similarities between a procedural and object-oriented problem solving approach.
5. Know the various error types that can be encountered while programming and when in the coding cycle each error type is encountered.
6. Understand the difference between the coding cycle when using an interpreted language (a language with a virtual machine) and a compiled language.
7. Know the commonly used encoding schemes for characters, integers and floating point numbers.
8. Able to evaluate arithmetic expression contain a mix of arithmetic operators and types of numbers.
9. Able to identify when changes to a list made inside a function are also visible outside the function.
10. Able to identify the scope of a variable. The levels of variable scope we have talked about during the semester are: local, global and class.
The following are
examples of the types of multiple choice questions you can expect.
1. Given that you know the ASCII code for the character ‘A’ is 65 then what will be the ASCII code for the character ‘F’
a. 65
b. 66
c. 69
d. 70
e. 71
2. In computer programs real numbers are stored in the floating point form. Which of the following is NOT one of the three components of a floating point number?
a. Sign
b. Mantissa
c. Exponent
d.
Decimal point
3. Which of the following people was a part of the team that worked on the ABC (computer)?
a. Allan Turing
b. Bill Gates
c. Clifford Berry
d. Steven Jobs
e.
None of these people were part of the team that worked on this computer.
4.
How many times will the loop in the following code be executed?
x = 2
while (x < 5) :
if (x % 2 == 0) :
x = x – 1
else :
x = x + 3
a. 0
b. 1
c. 2
d. 3
e.
4
5.
What will be printed to the console when we run the following code?
def
fun5(my_list, x) :
for i in range(0,x,2) :
my_list.append(i)
ml = [1.1,2.2,3.3]
fun5(ml, 5)
print (ml)
a. [1.1, 2.2, 3.3]
b. [1.1, 2.2, 3.3, 0]
c. [1.1, 2.2, 3.3, i]
d. [1.1, 2.2, 3.3, 0, 2, 4]
e.
[1.1, 2.2, 3.3, 0, 1, 2, 3, 4]
6. For an interpreted language such as Python, when is (binary) machine code generated?
a. As you type your code in an editor such as Idle, Emacs or Gedit.
b. While the code is run in the interpreter.
c. When you save the code with a .py extension.
d.
Machine code is never generated for interpreted language.
7. Which of the following error types is NOT detected by the Python interpreter?
a. Syntax errors
b. Runtime errors
c. Logic errors
d.
All these error types are detected by the interpreter.
8.
Consider the following code:
import tkinter
from tkinter import *
def button_clicked() :
num = int(button1['text'])
updated_text = str(num + num)
button1['text'] = updated_text
win = Frame()
win.pack()
button1 = Button(win)
button1['text'] = "5"
button1['command'] = button_clicked
button1.grid(row=0, column=0)
win.mainloop()
When we run it, the following window will appear on the screen.
What text will be on the button labelled ‘5’ after we click
it?
a. 5
b. 10
c. 55
d. 25
There are a total
of 12 written answer questions on the final exam. You may use either a pen or
pencil to answer these questions in the space provided in the exam booklet. To
ensure you do well in this section, make sure you are able to do the following:
1. Trace a nested loop. The following are two examples of nested loops.
a.
What
will be printed to the console when we run the following code?
i = 0
j = 0
while (i < 5):
while (j < 4):
print('(' + str(i) + ',' + str(j) + ')')
j = j + 1
i = i + 1
j = 0
print ('\n')
b.
Consider the following code:
def fun1() :
file = open('temp.txt', 'r')
lines = file.readlines()
file.close()
wordlist = []
for line in lines :
words = line.split()
for word in words :
if word not in wordlist :
wordlist.append(word)
return wordlist
If the file
contains the following text (Coleridge's "Rime of the Ancient Mariner"),
what will the function return?
I looked upon the rotting sea,
And drew my eyes away;
I looked upon the rotting deck,
And there the dead men lay.
2. Design a conditional statement (if statement). The following are possible examples.
a. Create an if-statement that prints the contents of two variables x and y in ascending order.
b.
Create an if-statement that prints the largest value of three variables,
x, y and z. You may assume that x, y and z contain numbers.
3.
Design a test suite given a function. For example, provide a set of
tests for the following function that thoroughly tests the function. Make sure
that all lines of code are tested. Each test should indicate the
input/arguments provide (provide exact literals), the expected value(s) that the
function returns (these should also be literals) and a description of what is
being tested.
def
is_all_caps(aString) :
for ch in aString :
if (ord(ch) < ord('A') or ord(ch) > ord('Z')) :
return False
return True
4.
Write code that uses a function from another module.
5. Write functions that parse and manipulate strings. For example:
a. Write a function that takes a single string parameter. The function should return this string except that each comma (,) in the string is replaced by a colon (:). Do not use any methods from the string library when writing this function.
b.
Write a function that takes two string parameters
aString
and subString.
The function should return the string
aString
with all
occurrences of
subString
removed. Do not use any methods from the string library when writing this
function.
6. Manipulate lists and 2d lists. For example:
a. Write a function that takes two parameters, the first is a list of strings and the second is a string called sub. You function should remove from the list all those strings that have sub as a substring. For example, fun([‘hello’,’this’,’is’,’a’,’swell’,’day’], ‘ell’) should return [‘this’,’is’,’a’,’day’].
b.
Write a function that takes as parameter a 2d list and returns the sum of
all numbers in this list. For example, fun([[1,2,3],[2,3],[6,5,3,1]]) should
return 26.
7. Tracing code employing exceptions: What is the output of the following programs if the user enters the following inputs:
Numerator |
Denominator |
1 |
3 |
1 |
0 |
a.
numerator = float(input("Enter numerator: "))
den = float(input("Enter a non-zero denominator: "))
if (den == 0):
raise ZeroDivisionError("Don't enter denom of zero")
else:
quotient = numerator / den
print(quotient)
b.calculationOK =
False
while not (calculationOK):
try:
numerator = float(input("Enter numerator: "))
den = float(input("Enter non-zero denominator: "))
quotient = numerator / den
except ZeroDivisionError:
print("Don't enter a denom of zero")
else:
calculationOK = True
print(quotient)
8.
Able to read from a file that can be of variable length and able to write
to files.
9.
Given a problem statement, design functions that would be helpful in
solving that problem. For this type of question, only the function definition
(name, parameter list, data returned and description). Do not provide any
implementation of these functions. A possible problem is as follows:
Create a program that will read marks, expressed as letter grades, from a file.
The user should be prompted for the filename. Your program will then create a
bar chart based on the marks.
10. Given code that defines a class, able to identify the different components of this class. This includes the constructor, methods and instance variables. You must also be able to create an instance of this class and manipulate this instance, including calling methods on the instance and passing the instance as a variable and returning an instance from a function.
For example,
consider the following class definition.
class Time:
def __init__(self, hours, minutes, seconds):
self.hours = hours
self.minutes = minutes
self.seconds = seconds
def after(self, time2):
if self.hour > time2.hour:
return True
if self.hour < time2.hour:
return False
if self.minute > time2.minute:
return True
if self.minute < time2.minute:
return False
if self.second > time2.second:
return True
return False
def convertToSeconds(self):
minutes = self.hours * 60 + self.minutes
seconds = minutes * 60 + self.seconds
return seconds
a. Write a function called now() which creates an instance of Time representing 11:12:13 and returns this instance.
b. Add a method addHour() to the class definition such that it updates the time represented by the instance to an hour later. For example, if variable my_time is of type Time and represents time 12:30:23, then my_time.addHour() should ensure that my_time now represents time 13:30:23.
11.
What will be printed to the console when we run the following
code? Make sure you show your trace of this code which shows all function calls
and values of local variables for each function call.
def f(n):
if (n==0):
return 1
else:
v = f(n-1)
r = n * v
return r
n
= 6
print(n, f(n))