









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Instructions and questions for a practice exam in CS 1301 at Georgia Tech. The exam covers topics such as Big O complexity, searching, functional programming, and object-oriented programming. The document also includes guidelines for academic integrity and device usage during the exam. The questions include multiple choice, coding exercises, and analysis of code snippets.
Typology: Exams
1 / 15
This page cannot be seen from the preview
Don't miss anything!
Post Exam 3 Practice Spring 2016
Name :
Grading TA:
I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Georgia Tech community. I have also read and understand the requirements outlined above.
Signature:
Question Points Score
Total: 90
D. A and C above E. B and C above F. A, B, and C above Examine the following code which defines a class and then creates an instance of the object, then answer the following questions about it: (i) [2 pts] class Costume: numberOfCostumes = 0
def init(self, type): self.type = type
def printType(self): print("I am wearing a {} costume".format(self.type))
ghost = Costume("Ghost") Look at the following pieces of code and the statement that describes what they do. Select the one that is true. A. ghost.numberOfCostumes = 1 updates the class variable numberOfCos- tumes B. ghost.type = 1 updates the instance variable type C. Costume.numberOfCostumes = 1 updates the instance variable num- berOfCostumes D. instance and class variables are the same thing E. All of the above F. None of the above Which line of code below correctly calls the printType method? A. ghost.printType(ghost) B. ghost.printType() C. self.printType(ghost) D. ghost.printType(self) E. Costume.printType(self.ghost) (j) [1 pt] Assume a class named FinalExam exists and the variable ”aVar” points to an instance/object of type FinalExam. Which of the following lines of code will cause Python to throw an error? A. FinalExam.color = "gold" B. aVar.color = "navy" C. aVar = FinalExam D. FinalExam.aVar.color = "white"
(k) [1 pt] Examine the following code:
def main(): counter = Counter() num = 0 for x in range(0,100): incrementor(counter,num) return(counter.count,num)
def incrementor(c,num): c.count = c.count + 1 num = num + 1
class Counter: def init(self): self.count = 0
aTuple = main() What is stored in aTuple after the above code is run? A. (0,100) B. (100,0) C. (100,100) D. (0,0) E. An error is raised (l) [3 pts] Examine the following code which defines a class and then creates an in- stance of the object, then answer the following questions about it: class Test2: def init(self,x,y): self.x=x y=self.recursionFun(y)
def recursionFun(self,x): print("recursionFun called, x is:", x) for i in x: if int(i) > 5: return self.x else: self.x=self.x+self.recursionFun(i[0]) return self.x
app=Test2(3,[’12’,8,[99],21]) What is app.y? A. None B. 0 C. 3 D. 6 E. Not Defined. What is app.x? A. None B. 0 C. 3 D. 6 E. Not Defined. How many times total was the recursionFun method called, either by itself or other methods? A. 0 B. 1 C. 2 D. 3 E. 4 F. 5
(m) [1 pt] Given the following code:
class Puppy:
Solution: Function A will be much faster because dictionary lookups are much faster than searching in a list. Function B looks at every item in the input list (N), and then (using the ”not in” operator) looks through every item in newList to see if it matches or not. Because the ”not in” operator for a list has to look at every element, that takes up to N- checks (if the newList is almost full). So this is an O(N^2 ) operation. Function A looks at every item in the input list, and then using the ”not in” operator for dictionaries checks to see if that key exists in the dictionary. Because the ”not in” operator runs in 0(1) or constant time, the total runtime for Function A is O(N). Grading: +1 point for identifying that A is faster. +1 for saying that dictionary lookups are faster than list searches. +1 for using Big O notation to explain why (NN for Function B, and N1 for Function A, or just N vs constant time for the lookups.
def function(key,aList): a = - for i in aList: a = a + 1 if i==key: return a return None
Describe using a sentence or two what this function does. Be sure to clearly specify what it returns in all cases.
If the number of items in the aList is N, what is the BigO time complexity class of this function (average case)? Use the comparison operator as your unit of ”work”.
Solution: This function will search for a key in a list. If it finds the key, it will return the index (location) of the key. If it does not find the key, it will return None
The Big O complexity class is O(N). Grading: +3 for a correct description (including returning None for a key that is not found). +2 for a good description that does not mention None and +1 for a description that indicates they know basically what it does (search for the key) + for the correct Big O complexity class.
Solution: Grading: 1 point for each line that is correctly labeled with a Big O complexity class 1 point for each algorithm that matches the big O complexity class (not necessarily the line) 1 point for the labels on the axes O(n) - Sequential or Linear search O( n^2 ) - Bubble Sort or Insertion sort O( nlogn)
(a) [2 pts] Here is a sequence of numbers: 1, 3, 5, 7, 8, 10, 15 If you were to perform a linear search on the above sequence, looking for the number ’8’, in what order does the computer search through the items in the sequence? Write them below.
Solution: 1,3,5,7,
(b) [2 pts] If you were to perform a binary search on the original sequence for the num- ber 8, in what order does the computer search through the items in the sequence? Write each item that is examined in the order that it is examined below.
Solution: 7, 10, 8
(c) [2 pts] What advantage does the binary search give us?
+1 for the Mercedes on the 2nd line. +1 for getting the rest of the string ”I’m driving a X Y, bro.” right. -1 for each extra incorrect lines. -1 for writing quotes around the printouts.
class Point: def init(self,x=0,y=0): self.x = x self.y = y
def getDistance(self): from math import sqrt dist = sqrt( self.xself.x + self.yself.y) return(dist)
def eq(self,other): if self.x == other.x: return True else: return False
p1 = Point() p2 = Point(3,4) p3 = Point(3,3)
v1 = p1.x v2 = p2.y v3 = p2 == p v4 = p2.getDistance()
What are the values in the 4 variables (v1,v2,v3,v4) after this code executes?
Solution: v1: 0 (accept 0.0 but don’t give bonus point) v2: 4 (accept 4.0 but don’t give bonus point) v3: True
v4: 5.0 (accept 5 but don’t give bonus point)
Grading: +1 for each correct answer. +1 bonus if all 4 answers are correct (including correct float/int)
class GA: population = 9800000 weather = "hot"
def init(self, loc): print("Georgia") self.city = loc if self.city == "Atlanta": self.Atlanta() elif self.city == "Athens": print("Bad!") GA.weather = "cold"
def Atlanta(self): self.weather = "humid"
place = GA("Atlanta") place2 = GA("Athens") print("The weather in {0} is {1}.".format(place.city, place.weather)) print("The weather in {0} is {1}.".format(place2.city, GA.weather))
What is printed on the screen as the code above is executed?
Solution: One point for each correct line: Georgia Georgia Bad! The weather in Atlanta is humid. The weather in Athens is cold.
num = num+i
return num
Solution:
def sumItUp(aList): value = reduce(lambda a,b: a+b , aList) return(value)
Grading: +2 for the correct lambda or helper function +1 for the correct call to reduce +1 for returning the correct value
tempF = [86, 91, 82, 88, 90, 87, 85, 72]
(a) [3 pts] Write a line of code that will filter out temperatures under 85 degrees F, keeping only temps 85 or higher, and store the resulting list as highTempsF. highTempsF =
Solution: highTempsF = filter( lambda x: x>=85, tempF) +1 pt filter +1 pt correct lambda funct +1 pt uses tempF
(b) [3 pts] Write a line of code that will convert the highTempsF into Celsius and store the resulting list as highTempsC. Celsius = 5/9*(Fahrenheit-32). highTempsC =
Solution: highTempsC = map( lambda x: 5/9* (x-32), highTempsF) +1 pt map +1 pt correct lambda funct +1 pt uses highTempsF
(c) [4 pts] Write a line of code that will sum the Celsius high temperatures, and then calculate the average Celsius high temp, storing it in avgHighC. You MAY NOT use the sum() function, but may use the len() function. avgHighC =
Solution: avgHighC = reduce( lambda x,y: x+y, highTempsC) / len(highTempsC) +1 pt reduce +1 pt correct lambda funct to sum +1 pt uses highTempsC +1 pt for divide by length
Fill in the following blanks so that the python code is correct: originalList = [-4,-3,-2,-1,1,2,3,4]
list(____________(lambda x: 2*x, originalList)) [-8, -6, -4, -2, 2, 4, 6, 8]
list(_____________(lambda x: x > 0, originalList)) [1, 2, 3, 4]
list(filter(_____________________________________________, originalList)) [-3, 3]
list(map(_________________________________________, originalList))
1 2 3 4 [None, None, None, None, None, None, None, None]