CSCI 150 Exam 2 Solutions

Similar documents
Spring 2017 CS 1110/1111 Exam 1

Spring 2017 CS 1110/1111 Exam 2

CS 1301 Exam 1 Fall 2010

Spring 2017 CS 1110/1111 Exam 3

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

CS 115 Exam 3, Spring 2014

CS 115 Exam 3, Fall 2016, Sections 5-8

Lecture 21. Programming with Subclasses

Python. Tutorial Lecture for EE562 Artificial Intelligence for Engineers

CS 1301 Exam 1 Fall 2010

1 Fall 2017 CS 1110/1111 Exam 3

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

Text Input and Conditionals

CPSC 217 Assignment 3

Read and fill in this page now

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Intro. Scheme Basics. scm> 5 5. scm>

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

The distill program uses dictionaries and the anagrams program uses sets.

(the bubble footer is automatically inserted into this space)

CS Exam 2 Name: Your Grading TA: This exam has 7 pages including the title page. Please check to make sure all pages are included.

CS 1301 Exam 1 Spring 2011

Lecture 21. Programming with Subclasses

CS 1301 Exam 1 Answers Fall 2009

Lecture 18. Classes and Types

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

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

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

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

ENGR 101 Engineering Design Workshop

CS 1803 Fall 2010 Exam 1

APT Session 2: Python

Structure and Interpretation of Computer Programs

CS 111X - Fall Test 1

CS 1301 Post Exam 3 Practice Spring 2016

Midterm Exam 2B Answer key

CS 111X - Spring Final Exam - KEY

MUTABLE LISTS AND DICTIONARIES 4

CS1 Lecture 5 Jan. 26, 2018

CS 115 Exam 3, Fall 2015, Sections 1-4

Structure and Interpretation of Computer Programs

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

Review 3. Exceptions and Try-Except Blocks

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

CS 1110 Prelim 2 November 14th, 2013

CPSC 217-T03/T08. Functions Ruting Zhou

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

CSC148 Recipe for Designing Classes

ECS 10 Concepts of Computation Example Final Problems

Lecture 21. Programming with Subclasses

Lecture 17: Classes (Chapter 15)

Project 5 Due 11:59:59pm Tue, Dec 11, 2012

Final Exam Version A

Hashing. So what we do instead is to store in each slot of the array a linked list of (key, record) - pairs, as in Fig. 1. Figure 1: Chaining

Question 1. CSC A08 F Midterm Test Solutions Fall 2011

CS1102: Macros and Recursion

THE AUSTRALIAN NATIONAL UNIVERSITY Final Examination November COMP1730 / COMP6730 Programming for Scientists

CS Prelim 2 Review Fall 2018

EXAMINATION INSTRUCTIONS

Control Flow Structures

CSC 310 Programming Languages, Spring 2014, Dr. Dale E. Parson

Clicker Question What will this print? def blah(x): return x+1 def main(): z = blah(3) print(z) main() A) 3 B) 4 C) 5 D) It causes an error

Programming Project 1

CS 1301 Exam 1 Fall 2014

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

Functionally Modular. Self-Review Questions

University of California, Berkeley College of Engineering

>>> print( X ) [0,1,2,3] def mystery(l):??

CS 1301 Exam 1 Fall 2014

Structure and Interpretation of Computer Programs

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

CSE413 Midterm. Question Max Points Total 100

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

All written answers are limited to their question boxes. Make sure all answers are easily legible.

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Slide Set 15 (Complete)

CS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:

Part III Appendices 165

CS 1110 Final Exam Solutions May 15th, 2014

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

Project 5 Due 11:59:59pm Wed, July 16, 2014

CS 1110 Final Exam Solutions May 2018

CS1 Lecture 5 Jan. 25, 2019

Project 4 Due 11:59:59pm Thu, May 10, 2012

Assignment 7: functions and closure conversion (part 1)

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS Name : Grading TA:

Final Exam(sample), Fall, 2014

EECS 492 Midterm #1. Example Questions. Note: Not a complete exam!

Question Possible Points Earned Points Graded By GUI 22 SQL 24 XML 20 Multiple Choice 14 Total Points 80

CS 1110 Final Exam, May 2018

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

2. All answers should go on blank paper that will be provided. Very carefully number and draw lines separating each solution!

Repetition Algorithms

Project 5 Due 11:59:59pm Tuesday, April 25, 2017

Transcription:

CSCI 150 Exam 2 Solutions 1. Here are definitions of two classes. Give a main( ) function that creates 3 persons: Hagrid, who is a Person, and Harry and Hermione, who are both Students at Hogwarts school. Make Harry and Hermione be friends. class Person: def init (self, name): self.name = name self.friends = [ ] def makefriends(self, other): self.friends.append(other) other.friends.append(self) def str (self): s = self.name+" is friends with " for x in self.friends: s = s + x.name + " " return s class Student(Person): def init (self, name, school): Person. init (self, name) self.school = school def main( ): p1 = Person( Hagrid ) p2 = Student( Harry, Hogwarts ) p3 = Student( Hermione, Hogwarts ) p2.makefriends(p3)

2. What will this program print? class Top: def init (self, value): # value should be a string self.value = value def str (self): return self.value class A(Top): def init (self, value): Top. init (self, value) def str (self): return self.value+self.value class B(Top): def init (self,value): Top. init (self, value) def Print(self): print( "My value is " + self.value ) def main(): t = Top("bob") a = A("bob") b = B("bob") print(t, a, b) main() bob bobbob bob

3. Here is a program with a recursive function. What will this program print? def blah(x): if x == 0: return 0 elif x % 2 == 0: return 1 + blah(x//2) else: return blah(x-1) def main( ): for x in range(0, 9): print(x, blah(x)) main( ) 0 0 1 0 2 1 3 1 4 2 5 2 6 2 7 2 8 3

4. When I run the following program I get an error on the line print(f) It says that f isn t defined. Explain in English what is wrong with this program. class Foo: def init (self, f): self.value = f def Print(self): print(f) def main(): F = Foo(23) F.Print() main() The variable f in the Print method is not an instance variable (its name doesn t start with self.) and it hasn t been given a value in the Print method, so it is undefined. The Print method can t see the variable f in the init method.

5. Here is another bad class definition: class Bar: def init (self, x): self.x = x def addone( ): self.x = self.x + 1 def Print( ): print(self.x) def main(): b = Bar(13) b.addone( ) b.print() main() When I try to run this program I get an error message on the second line in main( ): b.addone( ) The error message says TypeError: addone() takes 0 positional arguments but 1 was given Explain in English what is wrong. What is the one argment in b.addone( )?? The addone method (and the Print method also) should have parameter (or argument) self. Since it doesn t, it takes no arguments. In the statement b.addone( ), b is the argument that is passed to the function, and it should be given to the parameter self

6. Write a recursive function digits(x) that returns the number of digits integer x has. For example, digits(9635) returns 4. You can assume that x is never negative, so if x is less than 10 digits(x) should return 1; if x is between 10 and 99 it should return 2; if x is between 100 and 999 it should return 3, and so forth. Note that len(str(x)) gives the right answer but that isn t what I am looking for. This should be a recursive function that works on the value of x. def digits(x): if x < 10: return 1 else: return 1 + digits(x//10)

7. A game has 8 players; we ll call them A, B, C, D, E, F, G, and H. During the game they get points; we will model this with input statements. Write a program with a loop that repeatedly asks for a player s name, then, if the name is not blank, ask for the number of points that player won. These points are added onto the player s total. When the game is over an empty string is entered for the player s name, and at that point the program prints the name of the player with the most points. You don t need to worry about bad input; every player s name will be a letter in the range from A to H, points will be non-negative integers. Here is a sample run: player: B points: 10 player: E points: 12 player: H points: 9 player: B points: 3 player: <Return> player B wins! def main(): D = { } done = False while not done: player = input( "player: " ) if player == "": done = True else: points = eval(input( "points: " )) if player in D.keys(): D[player] = D[player] + points else: D[player] = points best = "A" for x in D.keys(): if D[x] > D[best]: best = x print( "%s wins with %d points."%(best, D[best])) main()

You can use this page as extra space for any question. If you want me to grade it be clear about what question you are answering here. When you are finished write and sign the Honor Pledge.