UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm 1 CSC148H1F L0201 (Liu)

Similar documents
UNIVERSITY OF TORONTO Faculty of Arts and Science

UNIVERSITY OF TORONTO Faculty of Arts and Science

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Name: utorid: U of T

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Student Number: Lab day:

EXAMINATION INSTRUCTIONS

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

a declaration of class name, and a class docstring

CS 1110 Prelim 2 November 6th, 2012

PREPARING FOR PRELIM 1

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Structure and Interpretation of Computer Programs

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

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Structure and Interpretation of Computer Programs

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

CSC148 Recipe for Designing Classes

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

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

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

Last name:... First name:... Department (if not D-INFK):...

PREPARING FOR THE FINAL EXAM

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

CSC148 Intro. to Computer Science

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

PREPARING FOR PRELIM 2

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

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSC148: Week 1

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

Csci 102: Sample Exam

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Duration: 90 minutes. Page 1 of 14 Q2: /13 Q3: /13 TOTAL: /38. Bonus /1

CS 111X - Fall Test 1

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

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

Exam 2, Form A CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Exam 2, Form B CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS150 Sample Final. Name: Section: A / B

CSC 108H1 F 2009 Test 1 Duration 35 minutes Aids allowed: none. Student Number:

Midterm Examination COMP 304B 2004: Object-oriented Design

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

CS 1110 Prelim 1, March 2018

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

CS 331 Summer 2017 Final Exam

Tutorial Letter 103/1/2017 Introduction to Programming I

CS 1110 Prelim 1 October 17th, 2013

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

Structure and Interpretation of Computer Programs

Last name:... First name:... Department (if not D-INFK):...

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

You may not share any information or materials with classmates during the exam and you may not use any electronic devices.

CS171 Midterm Exam. October 29, Name:

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

Structure and Interpretation of Computer Programs

Data Structures I: Linked Lists

CSC A20H3 S 2011 Test 1 Duration 90 minutes Aids allowed: none. Student Number:

CS61BL Summer 2013 Midterm 2

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

PREPARING FOR THE FINAL EXAM

CS 1110 Final Exam, May 2018

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

First Name: Last Name: Q1: /10

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

CS Prelim 1 Review Fall 2013

Spring 2017 CS 1110/1111 Exam 1

CS 1110 Final, December 16th, 2013

You may not share any information or materials with classmates during the exam and you may not use any electronic devices.

EXAMINATION INSTRUCTIONS

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

CS 1110 Prelim 1 October 4th, 2012

Practice midterm exam

ICS 31 UC IRVINE. ############## YOUR UCINET #### K E Y #### ############## Second Midterm YOUR NAME YOUR STUDENT ID (8 DIGITS)

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

******************** YOUR UCINET ID TIME MWF AT: 8A P ****** K E Y ****** TA S NAME ******************** Second Midterm

Student Number: Instructor: Brian Harrington

Structure and Interpretation of Computer Programs

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

CMSC201 Computer Science I for Majors

Spring Semester 09 Exam #1 Dr. Dillon. (02/19)

CS Prelim 2 Review Fall 2015

Final Exam Version A

CS Prelim 2 Review Fall 2014

Question 1. Part (a) Simple Syntax [1 mark] Circle add_ints(), because it is missing arguments to the function call. Part (b) Simple Syntax [1 mark]

CS 1110 Final, December 16th, 2013

Student Number: UTORid: Question 0. [1 mark] Read and follow all instructions on this page, and fill in all fields.

Structure and Interpretation of Computer Programs

CS150 - Sample Final

Good Luck! CSC207, Fall 2012: Quiz 3 Duration 25 minutes Aids allowed: none. Student Number: Lecture Section: L0101. Instructor: Horton

Question 1. CSC 120H1 F Midterm Test Solutions Fall 2017

Transcription:

UNIVERSITY OF TORONTO Faculty of Arts and Science Midterm 1 CSC148H1F L0201 (Liu) October 21, 2016 (50 min.) Examination Aids: Provided aid sheet (back page, detachable!) Name: Student Number: Please read the following guidelines carefully! ˆ Please write your name on the front and back of the exam. ˆ This examination has 4 questions. There are a total of 10 pages, DOUBLE-SIDED. ˆ You may always write helper functions/methods unless explicitly asked not to. Take a deep breath. This is your chance to show us How much you ve learned. We WANT to give you the credit That you ve earned. A number does not define you. Good luck!

Midterm 1, CSC148H1F L0201 (Liu) Page 2 of 10 1. The following questions test your understanding of the terminology and concepts from the course. You may answer in either point form or full sentences; you do not need to write much to get full marks! (a) [2 marks] Suppose we are reading the code of a particular class. We see a method whose name begins with an underscore. What term do we use to describe this kind of method, and why do we write such methods? (b) [2 marks] A student asks: why do we bother using unittest to test our code, when we could just write doctests? Explain why we use unittest in addition to writing doctests. (c) [1 mark] When writing an abstract class, we often write methods whose body is a single line of code: raise NotImplementedError. What is the purpose of writing such a method? (d) [1 mark] Suppose we write the following function: def remove_middle(lst): """Remove and return the middle item in <lst>. Precondition: len(lst) is odd. @type lst: list @rtype: object """ mid = len(lst) // 2 lst.pop(mid) Unfortunately, this function does not behaves exactly as expected. Explain why not.

Midterm 1, CSC148H1F L0201 (Liu) Page 3 of 10 (e) [2 marks] Do you expect the running time of the remove middle function (from the previous part) to depend on the length of its input list? Explain. (f) [2 marks] Consider the following function, which operates on a Python stack. def size(stack): copy = stack count = 0 while not copy.is_empty(): copy.pop() count = count + 1 return count Fill in the output of the last statement below. Then underneath, explain why this happens. >>> s = Stack() >>> s.push('a') >>> s.push('b') >>> size(s) 2 >>> s.is_empty() # FILL IN THE OUTPUT IN THE NEXT LINE Explanation:

Midterm 1, CSC148H1F L0201 (Liu) Page 4 of 10 2. [4 marks] We want to create a subclass of Stack called GuardedStack, which has the following differences: ˆ It has a new attribute is good, which is a function that takes an object and returns a boolean value. This attribute is initialized through a parameter to the GuardedStack constructor. ˆ When pushing a new item onto a GuardedStack, the stack s is good attribute is first called on the item. If this returns True then the item is pushed onto the stack, otherwise a ValueError is raised. Here is a sample usage of GuardedStack. >>> def is_even(num):... return num % 2 == 0... >>> g = GuardedStack(is_even) >>> g.push(2) >>> g.push(4) >>> g.push(5) ValueError # error raised here >>> g.push(16) >>> g.pop() 16 >>> g.pop() 4 In the space below, show how to override the relevant Stack methods in the GuardedStack class. You must properly call superclass methods when appropriate. (Note: neither method is very long.) def GuardedStack(Stack): def init (self, is_good): def push(self, item):

Midterm 1, CSC148H1F L0201 (Liu) Page 5 of 10 3. [9 marks] In this question, you are responsible for designing a class to represent an item being sold in a store. Each item has a name and original price. An item can be marked as on sale, at which point there is a percentage discount on its price. Once an item is on sale, it is on sale forever. Even after an item is on sale, it must be possible to calculate (using the attributes you define) both the original price of an item, and its current price. An item s current price is equal to the item s original price, with its discount applied if it is on sale. An item always starts as not on sale; in this case, its current price is equal to its original price. Below and on the next page, we have a very incomplete class design for this scenario. You have four tasks: ˆ Fill in all the attributes of the Item class. These can all be public. Be sure to specify a type and description for each. It must be possible to compute both the original and the current price from the attributes that you store, though you do not need to show how to do this. ˆ Write one plausible representation invariant for your class. You have some freedom here; just make sure your representation invariant makes sense in the context of this situation. ˆ Write the constructor of the Item class. A docstring is not necessary. ˆ Complete the docstring and implementation of a method put on sale that allows an item to be marked as on sale. No doctests are necessary. You must decide what happens when this method is called on an item that is already on sale; clearly document the expected behaviour, and make sure you implement it correctly. class Item: """An item for sale in a store. === Attributes === TODO: document all attributes. === Representation Invariant === TODO: document *one* representation invariant. """

Midterm 1, CSC148H1F L0201 (Liu) Page 6 of 10 # TODO: Design and implement a constructor here. # Remember that a docstring is not necessary. def put_on_sale(self, discount): """Mark this item as on sale with a given discount. TODO: document what happens if this method is called on an item that is already marked as on sale. Precondition: 0 <= discount <= 1 @type self: Item @type discount: float @rtype: None """ # TODO: implement this method.

Midterm 1, CSC148H1F L0201 (Liu) Page 7 of 10 4. For this question, you should refer to the documentation of the LinkedList class found on the aid sheet. You may use all attributes of the LinkedList and Node classes, as well as their constructors. You may also access the str method for LinkedList, but no other methods. (a) [1 mark] Suppose we have created the following two linked lists: >>> linky = LinkedList([10, 3, 100, 0]) >>> lando = LinkedList([44, 13, 4]) Write an expression that accesses the item at index 2 in linky, and stores it in a variable val. (b) [2 marks] Write a code snippet that replaces the contents of lando so that it is a LinkedList that only stores a single item, which is equal to val * 2. >>> # After your code runs, we should be able to see the following. >>> print(lando) [200] (c) [2 marks] A student comes to you with the following implementation of a LinkedList method, saying that when I ran it there was a big block of red text. Explain the problem with the code below, and how to fix it to satisfy its docstring. def count(self, item): """Return the number of occurrences of <item> in this linked list. @type self: LinkedList @type item: object @rtype: int >>> linky = LinkedList([5, 8, 5, 4]) >>> linky.count(5) 2 """ curr = self._first num = 0 while curr is not None: if curr == item: num = num + 1 curr = curr.next return num

Midterm 1, CSC148H1F L0201 (Liu) Page 8 of 10 Use this page for rough work. If you want work on this page to be marked, please indicate this clearly at the location of the original question.

Midterm 1, CSC148H1F L0201 (Liu) Page 9 of 10 Use this page for rough work. If you want work on this page to be marked, please indicate this clearly at the location of the original question.

Midterm 1, CSC148H1F L0201 (Liu) Page 10 of 10 Name: Q1 Q2 Q3 Q4 Total Grade Out Of 10 4 9 5 28