Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit



CSC 161: Final Exam

Fall 2015




Instructions:

● Write your name and NetID clearly at the top of the page.

●  Sign the declaration of academic honesty on this page.

● You have 75 minutes for this exam, once it starts.

●  If you run out of room for an answer, continue on the back of the page.

●  Keep your eyes on your own paper.

Do not start this exam until instructed to do so!






Declaration of Academic Honesty: I affirm that I will not give or receive any unauthorized help on this exam, and that all work will be my own.




Signed: Date:


CSC 161: The Art of Programming

Part I: True/False


Final Exam (Continued)



Circle True or False for each statement.

1.  (3 points)  True       False        The first parameter of a Python class method definition is called this.

2.  (3 points)  True       False        a  and   (b  or  c)  ==   (a  and  b)  or   (a  and  c)

3.  (3 points)  True       False        The condition x  <= y <= z is allowed in Python.

4.  (3 points)  True       False        New objects are created invoking a constructor.

5.  (3 points)  True       False        Using a “bare except” to catch all exceptions is good form.


Part II: Multiple Choice (Concept Review)


Select the correct answer to each question below.

6.  (3 points)  A class method definition with four formal parameters is generally called with how many actual parameters (a.k.a. “arguments”)?

Three

Four

口 Five

口 A method is always private and cannot be called


7.  (3 points) Within a method definition, the instance variable x could be accessed via which expres- sion?

口 x

self.x

口  self[x]

口  self.getX()


8.  (3 points) Which of the following is not a property of a Python dict? 口 A Python list cannot be used as a dictionary key

口 The contents of a dictionary are not returned in the order in which they were added 口 Dictionary values must only be a string, int or float because they are immutable

口 To lookup a value in a dictionary, use a key:  a_dict["a  key"]


CSC 161: The Art of Programming

9.  (3 points) What does the continue keyword do? 口 Returns from a function

口 Continues with the next iteration of the loop

口 Allows a loop to continue in case of an exception 口 Is used in if statements

10.  (3 points) Which Boolean expression below is False? Let x=5. 口  True  and  x  >=  4

not   (x  < 0) and (x >  0)

口  (x  >  0)  or   (x  < 0)

(x   !=  0)  and   (x  ==  0)

(-10  < x < 10)


Final Exam (Continued)




Part III: Short Answer


Answer the questions, below.  Don’t over-think the answers, keep it succinct! Partial credit is possible.

11.  (4 points)  Compare and contrast the following pairs of terms: (a)  For loop vs. While loop




(b) Interactive loop vs. Sentinel loop


CSC 161: The Art of Programming                                                               Final Exam (Continued)

12.  (4 points)  Answer the following questions on classes:

(a)  How is a class and an instance of a class related?







(b)  A class named Food is defined, and the class requires a food name, and the integer number of calories in that item. Make a single instance of this class.





13.  (4 points)  A question about loops, booleans and decision control:

(a) Write a for and while loop that are equivalent to each other:










(b) What is an example of a post-test loop?




















Page 4 of 13


CSC 161: The Art of Programming                                                               Final Exam (Continued)

14.  (4 points)  More on Python loop control:

(a) What Python keyword can be executed in the body of a loop to cause it to terminate?


(b)  Construct a while loop that runs until a certain score condition is met for players player_1 and player_2 (both integer values) in some sort of sports game. If one player has n points, and the other player is less than half of the other player’s score the loop stops running.  Note: your boolean expression must be in the while loop’s condition.











15.  (4 points) You have to write a Python exception handler in your code.   List the four Python language keywords you could use to handle an exception, and define what do they do.

1.



2.



3.



4.


CSC 161: The Art of Programming                                                               Final Exam (Continued)

Part IV: Multiple Choice (Programming)


Select the correct output to the code provided.

16.  (5 points)

def indexMax():

myList  =   [1,  6,  5,  9,  3,  1]

max =  myList[0]

indexOfMax  =  0

for i in range (1, len (myList)):

if myList[i]  > max :

max =  myList[i]

indexOfMax  =  i

print (indexOfMax)

indexMax()

11

6

9

3

口 0


CSC 161: The Art of Programming

17.  (5 points)


Final Exam (Continued)


class Name:

def __init__(self,  firstName,  mi,  lastName):

self.firstName  =  firstName

self.mi  =  mi

self.lastName  =  lastName


firstName  =  "John"

name  =  Name(firstName,   'F',  "Smith")

firstName  =  "Peter"

name.lastName  =  "Pan"

print (name.firstName,  name.lastName)

The program displays Peter  Pan.

口 The program displays John  Pan.

口 The program displays Pete  Smith.

口 The program displays John  Smith.

口 The program displays Smith  Pan.


18.  (5 points)

class A:

def __init__(self):

self.x  = 1

self.y  = 1


getY(self):

return self.y

a  =  A()

a.x  =  51

print (a.x)

口 The program runs fine and prints 0.

The program has an error because y is private

口 The program has an error because x is private

The program runs fine and prints 51.

口 None of the above.


CSC 161: The Art of Programming

19.  (5 points)


Final Exam (Continued)


class Count:

def __init__(self):

self.count  =  0


def increment(c,  times):

c.count  += 1

times  += 1

def main():

m_count  =  Count()

times  =  0

for i in range (0,  100):

increment(m_count,  times)

print ("m_count.count  =",  m_count.count,  "times  =",  times)

main()

m_count.count  =  100  times  =  0

口 m_count.count  =  100  times  =  100

口 m_count.count  =  101  times  =  0

口 m_count.count  =  101  times  =  101

口 None of the above


CSC 161: The Art of Programming

20.  (5 points)

class Student:

def __init__(self,  name,  age,  gpa):

self.gpa  =  gpa

self.name  =  name

self.age  =  age

def is_honor_student(self):

return self.gpa  >  3.0

def is_old(self):

return self.age  >  40

student  =  Student('G.  H.  Hardy',  70,  4.0) print (student.is_old ())

print (student.is_honor_student ())

口  True

True

口  True

False

口 False

True

口 False

False

口 The program returns an error


21.  (5 points (bonus))    An extra credit question on recursion:

def f2(n,  result):

if n  ==  1:

return 0

else :

print (n,  end  =  "  ")

return f2(n  -  1,  n  +  result)

print (f2(4,  0))

3 2 1 0

口 4 3 2 1

口  1 2 3 4

4 3 2 0


Final Exam (Continued)


CSC 161: The Art of Programming                                                               Final Exam (Continued)

22.  (5 points (bonus))    An extra credit question on searching. . .

def search(x,  nums):

low  = 0

high  = len (nums)  - 1

steps  = 0


while low  <= high:

mid  =   (low  +  high)//2 item  =  nums[mid] if x  ==  item:

return mid,  steps

elif x  < item: high = mid - 1

else :                           low  =  mid  + 1

steps  +=  1

return False,  steps


#  There  is  still  a  range  to  search #  Position  of  middle  item

#  Found  it!  Return  the  index

#  x  is  in  lower  half  of  range #  move  top  marker  down                #  x  is  in  upper  half  of  range #  move  bottom  marker  up


nums_lst  = range (1,  100,  5)

found,  steps  =  search(5,  nums_lst)

if found:

print ("Number  found  in  index   {0},  and  "

"the  search  took   {1}  steps".format (found,  steps))

else :

print ("Number  was  not  found,  and  "

"the  search  took   {0}  steps".format (steps))

口 Number  was  not  found,  and  the  search  took  2  steps

口 Number  found  in  index  1,  and  the  search  took  2  steps 口 Number  found  in  index  1,  and  the  search  took  4  steps

Number  was  not  found,  and  the  search  took  4  steps

口 None of the above.


CSC 161: The Art of Programming                                                               Final Exam (Continued)

Part V: Programming Assignment

23.  (25 points) Write a class Account that stores information about a bank account.

(a) Your class should have at least three methods to manipulate any instance of the Account class:

withdraw(), deposit(), and __init__().

(b) Your Account class should provide three class variables:  interest_rate set to 0.05 (5%),

overdraft_limit set to 500 (dollars), and overdraft_charge set to 50 (dollars). These are values common to all accounts at this bank (i.e. all instances of this class).

(c)  Additionally, the Account class should create two instance variables: account_number of the customer and balance which is set to 0 dollars (it’s a new, empty account). These are values unique to each Account instance.

(d)  (5 points (bonus))  Allow your user to “overdraft” on their account.  An overdraft occurs when money is withdrawn from a bank account and the available balance goes below zero. This allows a user to withdraw money on credit from the bank.  The user should only be allowed to withdraw money up to some overdraft limit. If an account overdraws past the overdraft limit, there is fee charged.

(e)  (5 points (bonus)) Write a useful and properly written __repr__ method.

Any class method should check its actual parameters for sensible values (such as deposit and withdrawal amounts that make sense).

Do not write a main() function, I just wanted the class definition. Also, the use of input() is neither required, nor correct! All information for your class will be passed into the constructor, when someone uses your class.


CSC 161: The Art of Programming                                                               Final Exam (Continued)