Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS 1301 Post Exam 3 Practice Spring 2016, Exams of Algorithms and Programming

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

2021/2022

Uploaded on 05/11/2023

marphy
marphy 🇺🇸

4.3

(30)

284 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 1301
Post Exam 3 Practice Spring 2016
Name :
Grading TA:
Integrity: By taking this exam, you pledge that this is your work and you have
neither given nor received inappropriate help during the taking of this exam in com-
pliance with the Academic Honor Code of Georgia Tech. Do NOT sign nor take this
exam if you do not agree with the honor code.
Devices: If your cell phone, pager, PDA, beeper, iPod, or similar item goes off during
the exam, you will lose 10 points on this exam. Turn all such devices off and put them
away now. You cannot have them on your desk.
Academic Misconduct: Academic misconduct will not be tolerated. You are to
uphold the honor and integrity bestowed upon you by the Georgia Institute of Tech-
nology.
Keep your eyes on your own paper.
Do your best to prevent anyone else from seeing your work.
Do NOT communicate with anyone other than a proctor for ANY reason in ANY
language in ANY manner.
Do NOT share ANYTHING during the exam. (This includes no sharing of pencils,
paper, erasers).
Follow directions given by the proctor(s).
Stop all writing when told to stop. Failure to stop writing on this exam when
told to do so is academic misconduct.
Do not use notes, books, calculators, etc during the exam.
Time: Don’t get bogged down by any one question. If you get stuck, move on to the
next problem and come back once you have completed all of the other problems. Write
all answers in the solution boxes unless directed otherwise. You will have 50 minutes
to complete this exam.
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:
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS 1301 Post Exam 3 Practice Spring 2016 and more Exams Algorithms and Programming in PDF only on Docsity!

CS 1301

Post Exam 3 Practice Spring 2016

Name :

Grading TA:

  • Integrity: By taking this exam, you pledge that this is your work and you have neither given nor received inappropriate help during the taking of this exam in com- pliance with the Academic Honor Code of Georgia Tech. Do NOT sign nor take this exam if you do not agree with the honor code.
  • Devices: If your cell phone, pager, PDA, beeper, iPod, or similar item goes off during the exam, you will lose 10 points on this exam. Turn all such devices off and put them away now. You cannot have them on your desk.
  • Academic Misconduct: Academic misconduct will not be tolerated. You are to uphold the honor and integrity bestowed upon you by the Georgia Institute of Tech- nology. - Keep your eyes on your own paper. - Do your best to prevent anyone else from seeing your work. - Do NOT communicate with anyone other than a proctor for ANY reason in ANY language in ANY manner. - Do NOT share ANYTHING during the exam. (This includes no sharing of pencils, paper, erasers). - Follow directions given by the proctor(s). - Stop all writing when told to stop. Failure to stop writing on this exam when told to do so is academic misconduct. - Do not use notes, books, calculators, etc during the exam.
  • Time: Don’t get bogged down by any one question. If you get stuck, move on to the next problem and come back once you have completed all of the other problems. Write all answers in the solution boxes unless directed otherwise. You will have 50 minutes to complete this exam.

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

  1. Multiple Choice 16
  2. removeDuplicates 3
  3. Big O Mystery 5
  4. BigO Graph 10
  5. Searching 8
  6. Objective Cars 5
  7. Point Objects 5
  8. Weather 10
  9. Functional Practice 7
  10. Functional: Sum It Up 4
  11. Functional Temps 10
  12. Functional Blanks 7

Total: 90

  1. (16 points) For each of the following multiple choice questions, indicate the most correct answer by circling it! (a) [1 pt] Examine the following code: def foo(aList): for i in range( len(aList) // 2 - 1 ): for j in range(len(aList) - 2): aList[i-j] = 0 Let N be the length of aList. If your ”unit of work” is assigning a zero to a list element aList[i-j], consider how the number of units of work increase as N increases. What is the Big O complexity class of the function foo? A. O(N) B. O(N^2 ) C. O(2N^ ) D. O(LogN) E. O(NlogN) (b) [1 pt] Which of the following has the smallest Big O complexity? A. Binary Search B. Dictionary Lookup C. Merge Sort D. Insertion Sort

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.

  1. (5 points) You are an intern at BigProgrammingCompany Inc, and your first job is to figure out the code left for you by a previous intern. Your boss gives you the following code and wants to know what it does and how efficient it is:

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.

  1. (10 points) Draw a single graph with a line for each of the four main Big O complexity classes we have learned about. Label both the X and Y axes appropriately. For each line, label it with the Big O complexity class it represents, and also give the name of an algorithm that falls in that 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)

  • Merge Sort (or Quicksort) O( logn) - Binary search X-axis: - Number of items, elements, etc Y-axis: - Work, time, or number of com- parisons
  1. (8 points)

(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.

  1. (5 points) Examine the following class definition of a Point and the seven lines of code that use Points:

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)

  1. (10 points) Examine the following code:

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

  1. (10 points) You are working for a meteorologist and need to use Python’s functional programming to manipulate the following list of temperatures (in Fahrenheit) that represents the high temperature for each day in a particular week. You may only use map/filter/reduce and lambda functions.

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]

  1. all elements are multiplied together to produce the product of all values in originalList
  1. (7 points) >>> _______ 576
    1. all elements in originalList are doubled

list(____________(lambda x: 2*x, originalList)) [-8, -6, -4, -2, 2, 4, 6, 8]

  1. only positive elements are retained

list(_____________(lambda x: x > 0, originalList)) [1, 2, 3, 4]

  1. only multiples of 3 and multiples of 5 are returned

list(filter(_____________________________________________, originalList)) [-3, 3]

  1. all elements in the list are printed out (A list of None’s is returned)

list(map(_________________________________________, originalList))

1 2 3 4 [None, None, None, None, None, None, None, None]