CS Prelim 2 Review Fall 2012

Similar documents
CS Prelim 2 Review Fall 2015

CS Prelim 2 Review Fall 2014

CS Prelim 2 Review Fall 2017

CS Prelim 2 Review Fall 2018

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

CS Prelim 1 Review Fall 2012

PREPARING FOR PRELIM 2

CS Prelim 1 Review Fall 2013

CS 1110 Prelim 2 November 6th, 2012

PREPARING FOR PRELIM 1

CS Prelim 1 Review Fall 2017

PREPARING FOR THE FINAL EXAM

CS 1110 Prelim 2 November 13th, 2014

CS 1110 Prelim 2 November 10th, 2016

CS 1110 Final, December 8th, Question Points Score Total: 100

CS Prelim 1 Review Fall 2016

CS Prelim 1 Review Fall 2013

CS 1110 Prelim 2 November 14th, 2013

CS Prelim 1 Review Fall 2018

CS 1110 Prelim 2 November 12th, 2015

PREPARING FOR THE FINAL EXAM

CS 1110 Final, December 8th, Question Points Score Total: 100

CS 1110 Final, December 16th, 2013

Review 3. Exceptions and Try-Except Blocks

CS 1110 Prelim 2 November 12th, 2015

Lecture 11. Asserts and Error Handling

Lecture 18. Classes and Types

Lecture 19. Operators and Abstraction

Lecture 11. Asserts and Error Handling

CS1110. Lecture 1: Final review session. Review materials See website for a version of last year s final with conventions redone to match this year.

CS 1110 Final, December 9th, Question Points Score Total: 100

CS 1110 Final, December 16th, 2013

CS Lecture 19: Loop invariants

Lecture 21. Programming with Subclasses

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

Lecture 20. Subclasses & Inheritance

isinstance and While Loops

CS 1110 Final, December 17th, Question Points Score Total: 100

CS 1110 Prelim 1 October 17th, 2013

Lecture 22. While Loops

Lecture 19. Using Classes Effectively

CS 1110: Introduction to Computing Using Python Loop Invariants

Review 2. Classes and Subclasses

CS 1110 Prelim 1 October 15th, 2015

CS 1110 Prelim 1 October 4th, 2012

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

CS 1110 Prelim 2 Solutions April 2018

Lecture 21. Programming with Subclasses

Structure and Flow. CS 3270 Chapter 5

Lecture 21. Programming with Subclasses

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Iteration and For Loops

CS Lecture 26: Grab Bag. Announcements

Announcements for This Lecture

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Lecture 9. Memory and Call Stacks

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Lecture 11: Iteration and For-Loops

Lecture 18. Using Classes Effectively

CS 1110 Final, December 7th, 2017

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Lecture 17: Classes (Chapter 15)

Sorting and Searching

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

CSC148 Recipe for Designing Classes

Lecture 24: Loop Invariants

Structure and Interpretation of Computer Programs

Lecture 12. Lists (& Sequences)

Structure and Interpretation of Computer Programs

Lecture 17: Classes (Chapter 15)

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Exam #3, Form 3 A CSE 231 Fall 2015 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Structure and Interpretation of Computer Programs

Announcements for This Lecture

Abstract Data Types Chapter 1

Lecture 13. For-Loops

CS 1110, Spring 2018: Prelim 1 study guide Prepared Tuesday March 6, 2018

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Prelim 2. CS 2110, 5:30 PM, November 17, 2016

CS1110 Lab 6 (Mar 17-18, 2015)

Lecture 7. Memory in Python

CS 1110 Final Exam, May 2018

CS 1110 Final Exam Solutions May 2018

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

CS Lecture 25: Models, Views, Controllers, and Games. Announcements

CS302 Data Structures using C++

TUPLES AND RECURSIVE LISTS 5

Lecture 15. More Recursion

CSE 160 Winter 2016: Final Exam

Lecture 10: Lists and Sequences

Programming Languages ML Programming Project Due 9/28/2001, 5:00 p.m.

Lecture 19: Subclasses & Inheritance (Chapter 18)

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Lecture 13. For-Loops

CSE 160 Winter 2016: Final Exam

CMSC 433 Spring 2013 Exam 1

CS 455 Midterm Exam 2 Spring 2017 [Bono] Tuesday, April 4, 2017

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Transcription:

CS 1110 Prelim 2 Review Fall 2012

Exam Info Prelim 1: 7:30 9:00PM, Tuesday, November 6th Last name A P in Kennedy 1116 Last name R T in Warren 131 Last name U Z in Warren 231 To help you study: Study guides, review slides are online Review solution to prelim 1 (esp. call stack!) Arrive early! Helps reducing stress We will not get locked out! Grades released the same evening (if possible) 11/4/12 Prelim 2 Review 2

What is on the Exam? Five Questions (+2pts for name, netid): Recursion (Lab 6, Lab 9, A4) Iteration (Lab 7, A4) Defining Classes (Lab 8, A5) Drawing class folders (Study Guide) Short Answer (Terminology) Roughly equal weight (#4 might be less) 11/4/12 Prelim 2 Review 3

What is on the Exam? Recursion (Lab 6, Lab 9, A4) Will be given a function specification Implement it using recursion May have an associated call stack question Iteration (Lab 7, A4) Defining Classes (Lab 8, A5) Drawing class folders (Study Guide) Short Answer (Terminology) 11/4/12 Prelim 2 Review 4

Recursive Function def merge(s1,s2): """Returns: characters of s1 and s2, in alphabetical order. Examples: merge('ab', '') = 'ab'" merge('abbce', 'cdg') = 'abbccdeg' Precondition: s1 a string with characters in alphabetical order" s2 a string with characters in alphabetical order""" 11/4/12 Prelim 2 Review 5

Recursive Function def merge(s1,s2): """Returns: characters of s1 and s2, in alphabetical order. Examples: merge('ab', '') = 'ab'" merge('abbce', 'cdg') = 'abbccdeg' Precondition: s1 a string with characters in alphabetical order" s2 a string with characters in alphabetical order""" Hint: Make input smaller by pulling off first letter Only make one of two strings smaller each call Which one should you make smaller each call? 11/4/12 Prelim 2 Review 6

Recursive Function def merge(s1,s2): """Returns: characters of s1 and s2, in alphabetical order. """ if s1 == '': return s2 if s2 == '': return s1 if s1[0] < s2[0]: # Pick first from s1 and merge the rest return s1[0]+merge(s1[1:],s2) else: # Pick first from s1 and merge the rest return s2[0]+merge(s1,s2[1:]) 11/4/12 Prelim 2 Review 7

Call Stack Question def skip(s): """Returns: copy of s Odd letters dropped""" 1 result = '' 2 if (len(s) % 2 = 1): 3 result = skip(s[1:]) 4 elif len(s) > 0: 5 result = s[0]+skip(s[1:]) 6 return result Call: skip('abc') Recursive call results in four frames (why?) Consider when 4th frame reaches line 6 Draw the entire call stack at that time Do not draw more than four frames! 11/4/12 Prelim 2 Review 8

Call Stack Question def skip(s): """Returns: copy of s Odd letters dropped""" 1 result = '' 2 if (len(s) % 2 = 1): 3 result = skip(s[1:]) 4 elif len(s) > 0: 5 result = s[0]+skip(s[1:]) 6 return result Call: skip('abc ) skip 3 s 'abc' result '' skip 5 s 'bc' result '' skip 3 s 'c' result '' skip s '' result '' 11/4/12 9

Call Stack Question def skip(s): """Returns: copy of s Odd letters dropped""" s = 'abc' 1 result = '' s = 'c' 2 if (len(s) % 2 = 1): 3 result = skip(s[1:]) 4 elif len(s) > 0: 5 result = s[0]+skip(s[1:]) 6 return result s = '' s = 'bc' Call: skip('abc ) skip 3 s 'abc' result '' skip 5 s 'bc' result '' skip 3 s 'c' result '' skip s '' result '' Done Line 6 11/4/12 10

What is on the Exam? Recursion (Lab 6, Lab 9, A4) Iteration (Lab 7, A4) Again, given a function specification Implement it using a for-loop Challenge is how to use accumulator variables Defining Classes (Lab 8, A5) Drawing class folders (Study Guide) Short Answer (Terminology) 11/4/12 Prelim 2 Review 11

Implement Using Iteration def evaluate(p, x): """Returns: The evaluated polynomial p(x) We represent polynomials as a list of floats. In other words [1.5, 2.2, 3.1, 0, 1.0] is 1.5 2.2x + 3.1x**2 + 0x**3 x**4 We evaluate by substituting in for the value x. For example evaluate([1.5, 2.2,3.1,0, 1.0], 2) = 1.5 2.2(2)+3.1(4) 1(16) = 6.5 evaluate([2, 4) = 2 Precondition: p is a list (len > 0) of floats, x is a float""" 11/4/12 Prelim 2 Review 12

Implement Using Iteration def evaluate(p, x): """Returns: The evaluated polynomial p(x) Precondition: p is a list (len > 0) of floats, x is a float""" sum = 0 xval = 1 for c in p: sum = sum + c*xval xval = xval * x return sum # coefficient * (x**n) 11/4/12 Prelim 2 Review 13

What is on the Exam? Recursion (Lab 6, Lab 9, A4) Iteration (Lab 7, A4) Defining Classes (Lab 8, A5) Given a specification for a class Also given a specification for a subclass Will fill in blanks for both Drawing class folders (Study Guide) Short Answer (Terminology) 11/4/12 Prelim 2 Review 14

class Customer(object): """Instance is a customer for our company""" # Mutable attributes _name = None # last name (string or None if unknown) _email = None # e-mail address (string or None if unknown) # Immutable attributes _born = -1 # birth year (int > 1900; -1 if unknown) # DEFINE PROPERTIES HERE # Enforce all invariants and enforce immutable/mutable restrictions # DEFINE CONSTRUCTOR HERE # Constructor: Create a new Customer with last name n, birth year y, e-mail address e. # E-mail is None by default # Precondition: parameters n, b, e satisfy the appropriate invariants # OVERLOAD STR() OPERATOR HERE # Return: String representation of customer # If e-mail is a string, format is 'name (email)' # If e-mail is not a string, just returns name 11/4/12 Prelim 2 Review 15

class Customer(object): """Instance is a customer for our company""" # Mutable attributes _name = None # last name (string or None if unknown) _email = None # e-mail address (string or None if unknown) # Immutable attributes _born = -1 # DEFINE PROPERTIES HERE @property def name(self): return self._name # birth year (int > 1900; -1 if unknown) Getter Setter @name.setter def name(self,value): assert value is None or type(value) == str self._name = value Actual Exam Question will not be this long. Just for this practice. 11/4/12 Prelim 2 Review 16

class Customer(object): """Instance is a customer for our company""" # Mutable attributes _name = None # last name (string or None if unknown) _email = None # e-mail address (string or None if unknown) # Immutable attributes _born = -1 # DEFINE PROPERTIES HERE @property def email(self): return self._email # birth year (int > 1900; -1 if unknown) Getter Setter @email.setter def email(self,value): assert value is None or type(value) == str self. _email= value Actual Exam Question will not be this long. Just for this practice. 11/4/12 Prelim 2 Review 17

class Customer(object): """Instance is a customer for our company""" # Mutable attributes _name = None # last name (string or None if unknown) _email = None # e-mail address (string or None if unknown) # Immutable attributes _born = -1 # DEFINE PROPERTIES HERE @property def born(self): return self._born # birth year (int > 1900; -1 if unknown) Getter No Setter! Actual Exam Question will not be this long. Just for this practice. 11/4/12 Prelim 2 Review 18

class Customer(object): """Instance is a customer for our company""" # Mutable attributes _name = None # last name (string or None if unknown) _email = None # e-mail address (string or None if unknown) # Immutable attributes _born = -1 # birth year (int > 1900; -1 if unknown) # DEFINE PROPERTIES HERE # DEFINE CONSTRUCTOR HERE def init (self, n, y, e=none): assert type(y) == int and (y > 1900 or y == -1) self.name = n # Property, handles asserts self.email = e # Property, handles asserts self._born = y # Field, because no setter Actual Exam Question will not be this long. Just for this practice. 11/4/12 Prelim 2 Review 19

class Customer(object): """Instance is a customer for our company""" # Mutable attributes _name = None # last name (string or None if unknown) _email = None # e-mail address (string or None if unknown) # Immutable attributes _born = -1 # birth year (int > 1900; -1 if unknown) # DEFINE PROPERTIES HERE # DEFINE CONSTRUCTOR HERE # OVERLOAD STR() OPERATOR HERE def str (self): if self.email is None: return = '' if self.name is None else self.name else: s = '' if self.name is None else self.name return s+'('+self.email+')' If not None, always a str Actual Exam Question will not be this long. Just for this practice. None or str 11/4/12 Prelim 2 Review 20

class PrefCustomer(Customer): """An instance is a 'preferred' customer""" # Mutable attributes _level = 'bronze' # level of preference. One of 'bronze', 'silver', 'gold' # DEFINE PROPERTIES HERE # Enforce all invariants and enforce immutable/mutable restrictions # DEFINE CONSTRUCTOR HERE # Constructor: Create a new Customer with last name n, # birth year y, e-mail address e, and level l # E-mail is None by default # Level is 'bronze' by default # Precondition: parameters n, b, e, l satisfy the appropriate invariants # OVERLOAD STR() OPERATOR HERE # Return: String representation of customer # Format is customer string (from parent class) +', level' # Use super in your definition 11/4/12 Prelim 2 Review 21

class PrefCustomer(Customer): """An instance is a 'preferred' customer""" # Mutable attributes _level = 'bronze' # level of preference. One of 'bronze', 'silver', 'gold' # DEFINE PROPERTIES HERE @property def level(self): return self._level Getter Actual Exam Question will not be this long. Just for this practice. @level.setter def level(self,value): assert type(value) == str and (value == 'bronze' or value == 'silver' or value == 'gold') self._level = value Setter 11/4/12 Prelim 2 Review 22

class PrefCustomer(Customer): """An instance is a 'preferred' customer""" # Mutable attributes _level = 'bronze' # level of preference. One of 'bronze', 'silver', 'gold' # DEFINE PROPERTIES HERE # DEFINE CONSTRUCTOR HERE def init (self, n, y, e=none, l='bronze'): super(prefcustomer,self). init (n,y,e) self.level = l # Property, handles asserts # OVERLOAD STR() OPERATOR HERE def str (self): return super(prefcustomer,self). str ()+', '+self.level super calls method in parent class as helper Actual Exam Question will not be this long. Just for this practice. 11/4/12 Prelim 2 Review 23

What is on the Exam? Recursion (Lab 6, Lab 9, A4) Iteration (Lab 7, A4) Defining Classes (Lab 8, A5) Drawing class folders (Study Guide) Given a skeleton for a class Also given several assignment statements Draw all folders and variables created Short Answer (Terminology) 11/4/12 Prelim 2 Review 24

Two Classes class CongressMember(object): _name = '' # Member's name @property def name(self): return self._name @name.setter def name(self,value): assert type(value) == str self._name = value def init (self,n): self.name = n # Use the setter def str (self): return 'Honorable '+self.name class Senator(CongressMember): _state = '' # Senator's state @property def state(self): return self._state @property def name(self): return self._name @name.setter def name(self,value): assert type(value) == str self._name = 'Senator '+value def init (self,n,s): assert type(s) == str and len(s) == 2 super(senator,self). init (n) self._state = s def str (self): return (super(senator,self). str ()+ ' of '+self.state) 11/4/12 Prelim 2 Review 25

Execute the Following Code >>> b = CongressMember('Jack') >>> c = Senator('John', 'NY') >>> d = c" >>> d.name = 'Clint' Remember: Commands outside of a function definition happen in global space Draw two columns: Global space Heap space Draw both the Variables created Objects (folders) created Put each in right space If a variable changes Mark out the old value Write in the new value 11/4/12 Prelim 2 Review 26

Global Space Heap Space b 586790 586790 CongressMember c 4356712 name 'Jack' init (n) str () d 4356712 If property overridden, same in both partitions Note setter always puts string 'Senator' in front 4356712 CongressMember name 'Senator John' 'Senator Clint' init (n) str () Senator name 'Senator John' 'Senator Clint' state 'NY' init (n,s) str () 11/4/12 Prelim 2 Review 27

class Senator(CongressMember): _state = '' # Senator's state Global Space @property def state(self): return self._state b 586790 @property def name(self): return c 4356712 self._name @name.setter def name(self,value): assert d 4356712 type(value) == str self._name = 'Senator '+value def init (self,n,s): assert type(s) == str and len(s) == 2 super(senator,self). init (n) self._state = s def str (self): return (super(senator,self). str ()+ ' of '+self.state) Heap Space 586790 CongressMember name 'Jack' init (n) str () 4356712 CongressMember name 'Senator John' 'Senator Clint' init (n) str () Senator name 'Senator John' 'Senator Clint' state 'NY' init (n,s) str () 11/4/12 Prelim 2 Review 28

What is on the Exam? Recursion (Lab 6, Lab 9, A4) Iteration (Lab 7, A4) Defining Classes (Lab 8, A5) Drawing class folders (Study Guide) Short Answer (Terminology, Potpourri) See the study guide Look at the lecture slides In that order Read relevant book chapters 11/4/12 Prelim 2 Review 29

Next is not on Prelim, but on Final 11/4/12 Prelim 2 Review 30

Bonus Question: Dispatch-On-Type def first(x): print 'Starting first.' try: second(x) except IOError: print 'Caught at first print 'Ending first def second(x): print 'Starting second.' try: third(x) except AssertionError: print 'Caught at second print 'Ending second def third(x): print 'Starting third.' if x < 0: raise IOError() elif x > 0: raise AssertionError() print Ending third.' What is the output of first(-1)? 11/4/12 Prelim 2 Review 31

Bonus Question: Dispatch-On-Type def first(x): print 'Starting first.' try: second(x) except IOError: print 'Caught at first print 'Ending first def second(x): print 'Starting second.' try: third(x) except AssertionError: print 'Caught at second print 'Ending second def third(x): print 'Starting third.' if x < 0: raise IOError() elif x > 0: raise AssertionError() print Ending third.' What is the output of first(-1)? Starting first. Starting second. Starting third. Caught at first. Ending first. 11/4/12 Prelim 2 Review 32

Bonus Question: Dispatch-On-Type def first(x): print 'Starting first.' try: second(x) except IOError: print 'Caught at first print 'Ending first def second(x): print 'Starting second.' try: third(x) except AssertionError: print 'Caught at second print 'Ending second def third(x): print 'Starting third.' if x < 0: raise IOError() elif x > 0: raise AssertionError() print Ending third.' What is the output of first(1)? 11/4/12 Prelim 2 Review 33

Bonus Question: Dispatch-On-Type def first(x): print 'Starting first.' try: second(x) except IOError: print 'Caught at first print 'Ending first def second(x): print 'Starting second.' try: third(x) except AssertionError: print 'Caught at second print 'Ending second def third(x): print 'Starting third.' if x < 0: raise IOError() elif x > 0: raise AssertionError() print Ending third.' What is the output of first(1)? Starting first. Starting second. Starting third. Caught at second. Ending second. Ending first. 11/4/12 Prelim 2 Review 34