CS 1110 Prelim 2 November 14th, 2013

Similar documents
CS 1110 Prelim 2 November 13th, 2014

CS 1110 Prelim 2 November 12th, 2015

CS 1110 Prelim 2 November 12th, 2015

CS 1110 Prelim 2 November 10th, 2016

CS 1110 Prelim 1 October 17th, 2013

CS 1110 Prelim 2 November 6th, 2012

CS 1110 Prelim 1 October 4th, 2012

CS 1110 Prelim 1 October 15th, 2015

Review 2. Classes and Subclasses

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

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

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

CS 1110 Final, December 16th, 2013

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

CS Prelim 2 Review Fall 2018

CS Prelim 2 Review Fall 2015

CS Prelim 2 Review Fall 2014

CS 1110 Final, December 16th, 2013

CS 1110 Final Exam, May 2018

CS Prelim 2 Review Fall 2017

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

PREPARING FOR PRELIM 1

CS 1110 Prelim 1 October 12th, 2017

CS 1110 Final Exam May 9th, 2013 SOLUTIONS

PREPARING FOR THE FINAL EXAM

CS 1110 Prelim 1 March 7th, 2013

CS 1110 Prelim 2 April 26th, 2016

PREPARING FOR PRELIM 2

Lecture 19: Subclasses & Inheritance (Chapter 18)

CS 1110 Final, May 2017

CS Prelim 1 Review Fall 2017

CS 1110 Prelim 2 April 22, 2014

Review 1. Call Frames; Diagramming Objects

Lecture 20. Subclasses & Inheritance

CS Prelim 2 Review Fall 2012

PREPARING FOR THE FINAL EXAM

CS Prelim 1 Review Fall 2013

CS Prelim 1 Review Fall 2018

CS 1110 Regular Prelim 1, March 14th, 2017

CS Prelim 1 Review Fall 2016

CS 1110 Prelim 1 March 15th, 2016

CS 1110 Prelim 1, March 2018

CS Prelim 1 Review Fall 2012

CS Prelim 1 Review Fall 2013

UTORid: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell

CS 1110 Prelim 2 Solutions April 2018

CS 1110 Prelim 2 April 21, 2015

CS 1110 Final, December 7th, 2017

Lecture 21. Programming with Subclasses

CS 1110 Final Exam Solutions May 2018

Lecture 17: Classes (Chapter 15)

Lecture 21. Programming with Subclasses

Overview of List Syntax

Lecture 21. Programming with Subclasses

CS100J Prelim 1 22 February 2007

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

Do not turn this page until you have received the signal to start.

Announcements for This Lecture

CS 1110: Introduction to Computing Using Python Loop Invariants

Last Name: First Name: Cornell NetID, all caps: CS 1110 Regular Prelim 1 Solutions March 2018

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

Lecture 18. Classes and Types

Lecture 18. Using Classes Effectively

CS1110 Lab 6 (Mar 17-18, 2015)

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

Lecture 17: Classes (Chapter 15)

CS Lecture 19: Loop invariants

Last Name: UTION First Name: SOL Cornell NetID: CS1110. Solution: CS 1110 Prelim 2 April 26, 2016

Announcements for This Lecture

Lecture 12. Lists (& Sequences)

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CS 1110 Final Solutions May 2017

Lecture 4. Defining Functions

CS 1110 Prelim 1 March 10, 2015

Do not turn this page until you have received the signal to start.

CSC148 Recipe for Designing Classes

Do not turn this page until you have received the signal to start.

Lecture 11. Asserts and Error Handling

Announcements for this Lecture

Lecture 19. Using Classes Effectively

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

Lecture 1. Course Overview, Python Basics

Total Score /1 /20 /41 /15 /23 Grader

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CS150 - Sample Final

Lecture 19. Operators and Abstraction

CS 1110 Prelim 2 April 21, 2015 GRADING GUIDE

UNIVERSITY OF TORONTO SCARBOROUGH. December 2017 EXAMINATIONS. CSCA20H3 Duration 3 hours. Examination Aids: Instructor: Bretscher

CS 1110: Introduction to Computing Using Python Lists and Sequences

Lecture 10: Lists and Sequences

Do not turn this page until you have received the signal to start.

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Lecture 1. Course Overview, Python Basics

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

x[2] x[2] = "d" x.append("d") x.insert(0, "d") x[3:5] x.s ort()

XC Total Max Score Grader

Lecture 18. Methods and Operations

Announcements For This Lecture

UNIVERSITY OF TORONTO SCARBOROUGH. Wnter 2016 EXAMINATIONS. CSC A20H Duration 2 hours 45 mins. No Aids Allowed

Transcription:

CS 1110 Prelim 2 November 14th, 2013 This 90-minute exam has 6 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need more space. You may tear the pages apart; we have a stapler at the front of the room. It is a violation of the Academic Integrity Code to look at any exam other than your own, to look at any other reference material, or to otherwise give or receive unauthorized help. You will be expected to write Python code on this exam. We recommend that you draw vertical lines to make your indentation clear, as follows: def foo(): if something: do something do more things do something last You should not use while-loops on this exam. Beyond that, you may use any Python feature that you have learned about in class (if-statements, try-except, lists, for-loops, recursion and so on). Question Points Score 1 2 2 24 3 20 4 20 5 20 6 14 Total: 100 The Important First Question: 1. [2 points] Write your last name, first name, netid, and lab section at the top of each page.

Throughout this exam you will need to make use of both strings and lists. You are expected to understand how slicing works. In addition, the following functions and methods may be useful: String Functions and Methods Function or Method Description len(s) Returns: number of characters in s; it can be 0. s.isupper() Returns: True if all letters in s are upper case, False otherwise. s.upper() Returns: a copy of s with all letters upper case. Non-letters are unaffected. s.islower() Returns: True if all letters in s are lower case, False otherwise. s.lower() Returns: a copy of s with all letters lower case. Non-letters are unaffected. s.find(s1) Returns: index of the first character of the FIRST occurrence of s1 in s (-1 if s1 does not occur in s). s.rfind(s1) Returns: index of the first character of the LAST occurrence of s1 in s (-1 if s1 does not occur in s). List Functions and Methods Function or Method Description len(x) Returns: number of elements in list x; it can be 0. y in x Returns: True if y is in list x; False otherwise. x.index(y) Returns: index of the FIRST occurrence of y in x (an error occurs if y does not occur in x). x.count(y) Returns: the number of times y appears in list x. x.pop() Returns: The first element of x, removing it from the list. x.append() Adds y to the end of list x. x.insert(i,y) Inserts y at position i in list x. Elements after position i are shifted to the right. 2. [24 points] Classes and Subclasses The next two pages have skeletons of two classes: Cornellian and Student. Student is a subclass of Cornellian, while Cornellian is only a subclass of object. You are to 1. Fill in the missing information in each class header. 2. Fill in the parameters of each method. 3. Implement each method according to its specification. 4. Enforce any preconditions in these methods using asserts. There is one method completed for you: _assigncuid. You are to use it in the initializer of Cornellian to assign the initial value of attribute _cuid. Pay close attention to the specification. There are no parameters beyond the ones listed. If a parameter has a default argument, it is clearly listed in the specification. Some method specifications are very explicit about the helper methods that you must use. Page 2

class Cornellian( object ): # Fill in missing part """Instance represents someone at Cornell IMMUTABLE ATTRIBUTES: _cuid: Cornell ID (not netid) [int > 0] MUTABLE ATTRIBUTES _name: full name [str, not empty]""" # CLASS ATTRIBUTE TO ASSIGN IDS IN ORDER NEXT_CUID = 1 # GETTERS/SETTERS HERE def getcuid( self ): # Fill in parameters """Return: Cornell ID""" return self._cuid def getname( self ): # Fill in parameters """Return: full name""" return self._name def setname( self, n ): # Fill in parameters """Set full name to n Precondition: n a nonempty string.""" assert type(n) == str and n!= '' self._name = n def _assigncuid(self): """Assigns _cuid to next available Cornell id""" self._cuid = Cornellian.NEXT_CUID Cornellian.NEXT_CUID = Cornellian.NEXT_CUID+1 # INITIALIZER def init ( self, n ): # Fill in parameters """Initializer: Make a Cornellian with name n. Initializer calls _assigncuid() to assign attribute _cuid Precondition: n a nonempty string.""" self.setname(n) # Handles precondition for us. self._assigncuid() # Creates CUID. def str ( self ): # Fill in parameters """Returns: Description of this Cornellian Description has form 'name [cuid]' Example: 'Walker White [1160491]' """ return self._name+' ['+str(self._cuid)+']' Page 3

class Student( Cornellian ): # Fill in missing part """Instance represents someone at Cornell Instance attributes are inherited from Cornellian. Also, MUTABLE ATTRIBUTES _gpa: grade point average [float between 0 and 4.3]""" # GETTERS/SETTERS HERE def getgpa( self ): # Fill in parameters """Return: student GPA""" return self._gpa def setgpa( self, g ): # Fill in parameters """Set student GPA to g Precondition: g a float between 0 and 4.3.""" assert type(g) == float and 0 <= g and g <= 4.3 self._gpa = g # INITIALIZER def init ( self, n, g = 0 ): # Fill in parameters """Initializer: Make a Student with name n, gpa g. Precondition: n a nonempty string, g a float between 0 and 4.3. g is 0 by default.""" # Call initializer for superclass Cornellian. init (self,n) self.setgpa(g) # Handles precondition def ondeanslist( self ): # Fill in parameters """Return: True if GPA >= 3.5; False otherwise""" return self._gpa >= 3.5 def str ( self ): # Fill in parameters """Returns: Description of this Student Description is same as Cornellian, plus '. Dean\'s List' if the student is on the Dean's List. Implementation must use str in Cornellian as helper. Example: 'Bob Roberts [234781]' 'Emma Towns [492886]. Dean\'s List' """ prefix = Cornellian. str (self) if self.ondeanslist(): return prefix+'. Dean\'s List' return prefix # Not on Dean's List Page 4

3. [20 points total] Folders and Name Resolution (a) [12 points] Suppose you were to execute the following commands in the interactive shell. > > > a = Student('Bill Gates', 2.54) > > > b = Cornellian('Walker White') > > > c = a > > > a = b In the space below, create two columns: one for global space and another for heap space. Clearly show what is created in each, drawing folders for objects and classes, and boxes for variables. If the value of a variable or attribute changes, cross the old one out and write the new value out beside it. You do not need to draw the folder of the object class. Variables Heap Space a b c id1 id2 id1 id2 id1 _name _cuid _gpa Student 'Bill Gates' 1 2.54 Cornellian init (n) str () setname(n) getname() getcuid() _assigncuid() NEXT_ID 3 id2 Cornellian Student _name 'Walker White' _cuid 2 init (n,g=0) str () setgpa(g) getgpa() ondeanslist() Page 5

(b) [8 points] Consider the two (undocumented) classes below. class A(object): x = 3 y = 5 def init (self,y): self.y = y def f(self): return self.g() def g(self): return self.x+self.y class B(A): y = 4 z = 10 def init (self,x,y): self.x = x self.y = y def g(self): return self.x+self.z def h(self): return 42 Execute the following in the interactive shell: > > > a = A(1) > > > b = B(7,3) The following expressions are either valid or the produce an error. Indicate which is which. If the expression is valid, tell us what it evaluates to. i. a.y 1 v. a.f() 4 ii. a.z Error vi. b.f() 17 (Inherited from A) iii. b.x 7 (Instance Attribute) vii. a.h() Error iv. B.x 3 (Class attribute) viii. A.g(b) 10 (Uses method in A) Page 6

4. [20 points] Iteration. Use for-loops to implement both the functions specified below. You may wish to refer to the list methods on the first page. You do not need to enforce the function preconditions. def min(thelist): """Return: the least element of thelist. Example: min([3,0,-1,5]) is -1 Precondition: thelist is a non-empty list of numbers""" # As list not empty, first element is place to start minimum = thelist[0] for x in thelist: if x < minimum: minimum = x return minimum def insert(thelist,x): """MODIFIES thelist, putting x into the correct, ordered position. List thelist is sorted (e.g. the elements are in order). The function puts x into thelist at the right position so it is still ordered. If x is already in thelist, this function inserts x before the first occurrence. You may NOT use the method sort(). Example: if a = [0,2,4,5], insert(a,3) makes a into [0,2,3,4,5] if a = [1,2,3,7], insert(a,-1) turns a into [-1,1,2,3,7] if a = [1,2,2,7], insert(a,2) turns a into [1,2,2,2,7] if a = [], insert(a,4) turns a into [4] Precondition: thelist is a sorted (possibly empty) list of numbers. x is a number (int or float).""" # Find position of first element greater than x pos = 0 for k in range(len(thelist)): if thelist[k] < x: pos = pos+1 # Shift greater elements to the right. thelist.insert(pos,x) Page 7

5. [20 points] Recursion. Use recursion to implement both the functions specified below; do not use for-loops or while-loops. You may wish to refer to the string methods on the first page. You do not need to enforce the function preconditions. def swapcase(s): """Return: a copy of s where letter case is swapped. Upper case letters are replaced by lower case letters. Lower case letters are replaced with upper case letters. Example: swapcase('hello World!') is 'hello world!' Precondition: s is a string (possibly empty).""" if s == '': return '' if s[0].isupper(): a = s[0].lower() else: # Lower or not a letter a = s[0].upper() return a+swapcase(s[1:]) def split(s,delimiter): """Return: list of substrings of s separated by the delimiter. This function breaks up s into several substrings according to the delimiter (a single character separator like ',' or ':'). Example: split('ab,c d', ',') is ['ab', 'c ', '', ' d'] split('ab::cd :: ef', '::') is ['ab', 'cd ', ' ef'] split('ab::cd :: ef', ',') is ['ab::cd :: ef'] split('', ',') is [''] Precondition: s is a string (possibly empty). delimiter is a nonempty string""" # Need to break up at delimiters, not first character pos = s.find(delimiter) if pos == -1: return [s] front = [ s[:pos] ] back = s[pos+len(delimiter):] # Do not include delimiter return front+split(back,delimiter) Page 8

6. [14 points total] Poutporri. (a) [6 points] Consider the following assignment statements. > > > a = [[1,2,3],[4,5,6],[7,8,9]] > > > b = a[1:] > > > b[0] = [10,11] > > > b[1][0] = 99 What are the values a and b after all these assignments? Explain your answer. a is [[1,2,3],[4,5,6],[99,8,9]] b is [[10,11],[99,8,9]] The second line (the slice) puts [[4,5,6],[99,8,9]]. The third line replaces the first element of this 2D list with another list. The last line goes inside of the list [7,8,9] and changes the first element. Because of how slicing works, the folder for this list is shared by both a and b, so changes to one affect another. (b) [4 points] Rewrite the command assert type(x) == int, str(x)+' is not an int' so that the user gets a TypeError instead of an AssertionError (You may need more than one line). if type(x)!= int: msg = str(x)+' is not an int' raise TypeError(msg) (c) [4 points] Explain the difference between an invariant and a precondition. How do they relate to one another? Okay, wow; this question was a mistake. We intended to ask a question about class invariants and function preconditions. However, we did not realize that the exam was on the same day as the lecture on loop invariants and general preconditions. In the end, we accepted either answer, as long it was correct. A function precondition is a property that must be true of a function or method parameter for the function to work properly. An class invariant is a property that must be true of an object attribute that must be true for the object to work properly. For the relationship, it is not enough to note that these are similar. A similarity is not the same thing as a relationship. The relationship is that the preconditions of the methods in a class are written so that they preserve invariants when called. Page 9