Docsity

Review Sheet - Introduction to Computing | CS 1301, Study notes of Computer Science

Toggle sidebar
bg1
1. tracing
for i in range(2):
for j in range(3):
print i, j
What is printed?
2. (10 points) Object-Oriented Python. Write a class called Dog that has
(a) a constructor
(b) a str method.
The constructor is to take a breed, gender, and name (in that order) as string parameters and store
them as a fields called breed, gender, and name. The str method is to return a string structured like
this one: ”German Shepherd, male, named Spot” given the breed is German Shepherd, gender is male,
and name is Spot. Be sure to name both the constructor and str methods correctly.
3. (5 points) OO error correction. Given correct implementations of the Dog class, find and correct the
errors in the following code segment that is in your main program. (No def or class header is required.)
Each line has one error.
myDog = new Dog("male", "Wolfie")
print string(myDog)
4. (10 points) Matching. Place the number of the term that best fits the question subparts. Choices can
be used once, more than once, or not at all.
1. n 11. log n
2. overriding 12. const
3. overloading 13. object
4. refinement 14. priority queue
5. str 15. inheritance
6. str 16. self
7. map 17. class
8. init 18. abstraction
9. init 19. depth-first search
10. method 20. breadth-first search
(a) In OO programming if you see the line myCard= Card(”Ace”, ”Hearts”), Card is
actually a call to what?
(b) This is like a blueprint for objects in object-oriented programming. One writes this as the
model for a given concept like Card, Deck, Student, Friend.
(c) When you store data for an actual instance using a class, you create one of these. (For
example, you are storing a particular Card, a particular Friend, etc.)
(d) If you want to get a meaningful string representation of an object, then you should provide
this method in your class.
(e) Functions in object-oriented programming go by this name instead.
pf3

Partial preview of the text

Download Review Sheet - Introduction to Computing | CS 1301 and more Study notes Computer Science in PDF only on Docsity!

  1. tracing

for i in range(2): for j in range(3): print i, j

What is printed?

  1. (10 points) Object-Oriented Python. Write a class called Dog that has

(a) a constructor (b) a str method. The constructor is to take a breed, gender, and name (in that order) as string parameters and store them as a fields called breed, gender, and name. The str method is to return a string structured like this one: ”German Shepherd, male, named Spot” given the breed is German Shepherd, gender is male, and name is Spot. Be sure to name both the constructor and str methods correctly.

  1. (5 points) OO error correction. Given correct implementations of the Dog class, find and correct the errors in the following code segment that is in your main program. (No def or class header is required.) Each line has one error.

myDog = new Dog("male", "Wolfie") print string(myDog)

  1. (10 points) Matching. Place the number of the term that best fits the question subparts. Choices can be used once, more than once, or not at all. 1. n 11. log n 2. overriding 12. const 3. overloading 13. object 4. refinement 14. priority queue 5. str 15. inheritance 6. str 16. self 7. map 17. class 8. init 18. abstraction 9. init 19. depth-first search 10. method 20. breadth-first search

(a) In OO programming if you see the line myCard= Card(”Ace”, ”Hearts”), Card is actually a call to what?

(b) This is like a blueprint for objects in object-oriented programming. One writes this as the model for a given concept like Card, Deck, Student, Friend.

(c) When you store data for an actual instance using a class, you create one of these. (For example, you are storing a particular Card, a particular Friend, etc.)

(d) If you want to get a meaningful string representation of an object, then you should provide this method in your class.

(e) Functions in object-oriented programming go by this name instead.

  1. (5 points) Big O Matching. Place the number of the term that best fits the question subparts. Choices can be used once, more than once, or not at all. 1. O(1) 2. O(log n) 3. O(n) 4. O(n log n) 5. O(n^2 ) 6. O(0)

(a) What is the Big O of sequential search?

(b) What is the Big O of binary search?

(c) What is the Big O of bubblesort?

(d) What is the Big O of selection sort?

(e) What is the Big O of quicksort?

  1. (5 points) map, reduce, filter, list comprehensions.

[x for x in nums if x >= 0] What does that list comprehension do?

  1. (5 points) map, reduce, filter.

answers = map(math.sqrt, [100, 9, 49]) What does that call to map create?

  1. (5 points) Exception Handling.

The following code will result in a IOError in Python when the file is not found. Write a try/except block around the lines as given. Your try/except should be written so that if the IOError occurs, the program does not crash, but prints ”That file was not found” instead.

import os file = open("/User/blah.txt", "r")

  1. def goofy(x): try: ans = 1.0/x print "The answer is", ans except ZeroDivisionError: print "Sorry, cannot divide by zero" except TypeError: print "Sorry there was a type issue" finally: print "The End" What is printed when for each of the following? goofy(0) goofy(1) goofy(”Mickey”)

Page 2