CIT-590 Midterm. Fall Name: Pennkey (not number): Please read the following and sign:

Similar documents
Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

CS 1301 Exam 2 A Fall 2015

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS Prelim 2 Review Fall 2018

EXAMINATION INSTRUCTIONS

Midterm #1 Fall minutes

Programming for Engineers in Python

CS 1110 Prelim 2 November 6th, 2012

: Intro Programming for Scientists and Engineers Final Exam

Discussion 4. Data Abstraction and Sequences

CS 1110 Prelim 2 November 14th, 2013

Practical Questions CSCA48 Winter 2018 Week 3

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

CS 1301 Exam 2 A Fall 2015

CS 1301 Exam 1 Fall 2010

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

University of California, Berkeley College of Engineering

Midterm Exam 2B Answer key

CSC148: Week 1

CSE 160 Winter 2017: Final Exam

CIS 110 Introduction to Computer Programming Fall 2017 Midterm. Recitation ROOM :

CSE 160 Winter 2017: Final Exam

CS 2316 Exam 1 Spring 2013

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

CS 2316 Exam 1 Spring 2014

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.

My dear students, Believe in yourselves. Believe in your abilities. You have got this! -Dr. M

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS164: Midterm I. Fall 2003

CS 1301 Exam 1 Spring 2014

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

APT Session 2: Python

CS 2316 Exam 1 Practice ANSWER KEY

Midterm #2a Fall minutes DO NOT WRITE IN THIS AREA

Total Score /15 /20 /30 /10 /5 /20 Grader

Student Number: Lab day:

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS3360 Design and Implementation of Programming Languages Final Exam May 16, pm to 12:45pm (2 hour and 40 minutes) NAME:

CS 231 Data Structures and Algorithms, Fall 2016

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

CS 1301 Exam 1 Fall 2010

Structure and Interpretation of Computer Programs

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

1 Inheritance (8 minutes, 9 points)

3. Conditional Execution

Total Score /15 /20 /30 /10 /5 /20 Grader

CS 1110 Prelim 2 November 13th, 2014

All program statements you write should be syntactically correct. Partial credit is not guaranteed with incorrect use of syntax.

CS 1301 Post Exam 3 Practice Spring 2016

Introduction to Python

Warm-up! CS 3 Final Review. Predict the output. Predict the Output. Predict the output. Predict the output. What is your favorite color?

CS 1110 Final, December 16th, 2013

EXAMINATION INSTRUCTIONS

Final Exam(sample), Fall, 2014

CS Prelim 2 Review Fall 2012

CS 2316 Exam 4 Fall 2011

Due: Tuesday 29 November by 11:00pm Worth: 8%

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

Problem Solving for Intro to Computer Science

roboturtle Documentation

Structure and Interpretation of Computer Programs

Distributions of Continuous Data

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

ext Total Score /20 /20 /15 /20 /25 /5 Grader

Midterm I Practice Problems

NOTE: All references to Python on this exam mean Python 3, so you should answer accordingly.

CS 1110 Final, December 16th, 2013

CS 234 Python Review Part 2

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

UNIVERSITY OF TORONTO SCARBOROUGH. Fall 2015 EXAMINATIONS. CSC A20H Duration 3 hours. No Aids Allowed

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

CS 367: Introduction to Data Structures Midterm Sample Questions

CS 1301 Exam 1 Fall 2014

Practice Midterm Exam Solutions

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

CS 1301 Exam 1 Fall 2014

CSE 160 Winter 2016: Final Exam

CS 415 Midterm Exam Spring 2002

Book keeping. Will post HW5 tonight. OK to work in pairs. Midterm review next Wednesday

CMPSCI 453 Instructor V. Arun Fall Midterm 11/07/2007

Midterm Exam. Sample Solutions

CS 1331 Exam 1 ANSWER KEY

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

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

XC Total Max Score Grader

EXAM PREPARATION SECTION 1

CS61A Notes Week 13: Interpreters

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

This was the second midterm from I have added a few comments in bold type, like this.

CISC 124: Introduction To Computing Science II

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

Graph Theory Questions from Past Papers

Curves & Splines. Assignment #3. Overview & Objectives. Due Dates. CPSC 453 Fall 2018 University of Calgary

In math, the rate of change is called the slope and is often described by the ratio rise

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

CS1 Lecture 13 Feb. 13, 2019

University of Virginia Department of Computer Science. CS 4501: Information Retrieval Fall 2015

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

Compsci 101 Fall 2015 Exam 1 Rubric. Problem 1 (24 points)

Transcription:

CIT-590 Midterm Fall 2017 Name: Pennkey (not number): Please read the following and sign: I agree to not discuss the contents exam with other students until cleared by the instructor to do so. I realize that other students have not taken the exam yet, and I will not provide academically dishonest assistance to them. I have received no advanced knowledge of the contents of this exam. I will not use any notes, calculators, cell phones or electronic devices on this exam. MY CELL PHONE IS TURNED OFF AND SILENCED. I acknowledge that my answer must go in the space provided, and that notes on the back of the page will not be graded. If I run out of space, I will write a note saying "See the extra back page" where I will write my answer. My answer must go on the front of this page. I agree to all of the following under penalty of Penn's policies regarding academic honesty. Sign: Date: Do not flip to the next page until given permission by the instructor.

1) Just warming up [15 points 3 points each]. Answer each of the following questions in a few sentences. a) Briefly explain how Test-Driven Development works. Identify all functions that do not depend on other functions and test those first. Write a stub for each of these functions [1 point]; then write all tests for those functions [1 point]; finally write the actual functions and add code until your tests pass [1 point]. [full points for some variant of the second sentence] b) What is the benefit of Test-Driven Development as opposed to a program that has unit tests but did not use Test-Driven Development? The benefit is that it forces you to think about the structure of your program and functions before you actually write them. Testing post-factum is still useful but your tests may be affected by your program so that you test for things that you know will work. [full points for some variant of the first sentence] c) What are the pros and cons of the garbage collector? The pros are that it makes it easier to 1) code and 2) avoid memory bugs/leaks. The cons are 1) computational and 2) memory overheads. [full points if 1 argument from each sentence is present] d) What is the difference between a local and a global variable? Local variables are only accessible within the scope they are defined (e.g., function, loop). Global variables are accessible by the whole program. [1.5 points for each sentence] e) Name one disadvantage of a linked list as opposed to a standard Python list. Indexing is really the only one we mentioned in class.

2) Think like a Python [15 points 1.5 points per box]. What will each segment of code print, and what is the value of x at the end? If this generates an error, say Error under Print and why it's an error under X. Use the first two as a guide. Code Segment What prints Value of X x = "cat" x = x + "dog" print(x) import math x = math.sqrt(-5) print("this never prints") "catdog" ERROR "catdog" Cannot take sqrt of negative number a = 6 b = 2 x = 2 * a / b ** 2 print(a/b) lst1 = [[1, 2], 5] lst2 = [2, 3] x = lst1[-2] print(lst1 + lst2) 3.0 3.0 [[1,2], 5, 2, 3] [1,2] x = this test is easy x[13:16] = hard print(x[5:]) import math a = math.sqrt(10) x = a ** 2 assert x == 10 print(a) ERROR ERROR Strings are immutable assert statement is false due to limited precision x = [] x.append({2: two }) x.append({3: three }) print(x[0][2]) two [{2: two }, {3: three }]

3) Life is a script [15 points]. Remember that Python is a scripted language, so there is no type checking until a line is actually executed. Suppose I write the following function: def modify_collection(col): for index in range(len(col) - 1): col[index] = col[index] + col[index + 1] Now suppose I call modify_collection with each of the following five collections. Write down what each collection looks like after modify_collection is called. If the function throws an error for some reason, say why [3 points each]. a) [1, 2, 3, 4] [3, 5, 7, 4] [-1 point if last element changed] b) ["a", "b", "c", "d"] [ ab, bc, cd, d ] [-1 point if last element changed] c) "abcd" Error: strings are immutable d) {2: "two", 1: "one", 0: "zero"} {2: two, 1: onetwo, 0: zeroone } [-1 point if last element changed; no point deduction if order changed] e) (1, 2, 3, 4) Error: tuples are immutable

4) I never get movie references [15 points]. Suppose I have compiled a movie database and stored it in the global dictionary below. Please help me figure out some important information about my movies by filling out the code for each of the two functions below [7.5 points each]. actor_db = {"Harrison Ford": ["Indiana Jones", "Star Wars"],\ "Leonardo DiCaprio": ["Inception", "Blood Diamond", "The Beach"],\ "Joseph Gordon-Levitt": ["Inception", "The Dark Knight Rises"]} def all_movies_in_common(actor1, actor2): """Returns a list of all movies with both actor1 and actor2 (assume inputs are strings) """ # Your code goes here. movie_list1 = actor_db[actor1] movie_list2 = actor_db[actor2] common_list = [] for movie in movie_list1: if movie in movie_list2: common_list.append(movie) return common_list def actor_with_most_movies(): """Returns the name of the actor who has played in the most movies""" # Your code goes here. max_movies = -1 cur_actor = for actor in actor_db: if len(actor_db[actor]) > max_movies: max_movies = len(actor_db[actor]) cur_actor = actor return actor

5) But I do get football references [20 points]. Now suppose I also like to keep track of some famous football players. For each player, I keep track of their age, number of goals scored, teams they have played for, and name. When a player moves to a different team, I call the transfer_player function. Also, for each team, I keep track of all of their present and past players and all of these players teams (in player_history). Please answer the questions after the code below. import copy def transfer_player(player, team): """Returns a new list for player, with team added to player s list of teams.""" new_player = copy.copy(player) new_player[2].append(team[0]) team[1].append(new_player) return new_player def player_history(team): """Returns a dictionary where the keys are all players who have ever played for the team and the values are all teams they have ever played for""" players = {} for player in team[1]: players[player[3]] = player[2] return players def main(): messi = [30, 532, ["Barcelona"], "Messi"] weird_player = [25, 249, ["Santos", "Barcelona"], "Neymar"] barca = ["Barcelona", [messi, weird_player]] psg = ["Paris Saint-Germain", []] transfer_player(weird_player, psg) print(player_history(barca)) if name == " main ": main()

a) Please draw the state of the stack and heap before return new_player is executed. You need to draw the state of player and new_player (assume strings fit in one box). You can ignore team. (Hint: I have drawn all the memory boxes you need to use.) [10 points]. b) What does print(player_history(barca)) print [3 points]? { Messi : [ Barcelona ], Neymar : [ Santos, Barcelona, Paris Saint-Germain ]} c) What would print(player_history(barca)) print if I had done a deep copy instead of a shallow copy [3 points]? { Messi : [ Barcelona ], Neymar : [ Santos, Barcelona ]} d) In general, what is the difference between a deep copy and a shallow copy [2 points]? A shallow copy only copies simple values and references, whereas a deep copy copies all the data in collections as well. e) When is the if-case if name == " main " true [2 points]? When we execute that module directly (but not when we import it).

7) You gotta have class [20 points]. Finally, suppose I also like to keep digital track of my (imaginary) dogs. Look at the class definitions below and answer the following questions. class Animal(): def init (animal, age, name): animal.age = age animal.name = name class Dog(Animal): def init (self, age, name = "Even dogs know Ronaldo > Messi"): Animal. init (self, age, name) def main(): my_dog = Dog(1) a) Why do I need to use Animal in Animal. init (self, age, name) [3 points]? Because as defined, init is a class method (it does not use self). b) How would I have to change the init method in Animal if I want to use super(). init (age, name) [3 points]? Replace animal with self. c) What is the difference between methods and functions [2 points]? Methods belong to objects/classes whereas functions are independent and can be defined outside of classes. d) Why can I just provide one parameter when I create my_dog in main() [2 points]? Because the name parameter has a default value.

e) As is clear from their class definition, dogs like to work with numbers. They also like to change their names in weird ways by playing with numbers. Please implement the method below (you can assume it is inside the Dog class) [10 points + 5-point bonus]. def change_name(self): """Changes the name of the Dog object in the following way: if the name is a single word, then it doesn t change; if the name is more than one word, the new name is a string containing the words in odd positions in the old name (for example, if the dog s name was the default name, the new name would be "dogsronaldomessi")""" # Your code goes here. # Bonus 5 points: change the name so that there is space # between the words but no space after the last word words = self.name.split() new_name = "" i = 1 if len(words) > 1: while i len(words): new_name = new_name + words[i] if i < len(words) 2: #bonus points if-case new_name = new_name + " " i += 2 self.name = new_name