Exampleexam Answers
Exampleexam Answers
Answer Book
Question 1.
[5 marks]
b)
def deposit(self, amt):
self.balance += amt
return
def withdraw(self, amt):
if self.balance – amt > 0:
self.balance -= amt
return
def add_interest(self):
i = BankAccount.interest_rate *
self.balance
self.balance += i
[10 marks]
c)
class StudentAccount(BankAccount):
overdraft_lmt = 1000
def __init__(self, name,
number, balance):
BankAccount.__init__(self, name,
number, balance)
return
def withdraw(self, amt):
if self.balance – amt > overdraft_lmt:
self.balance -= amt
return
[10 marks]
Question 2.
class Palindrome(object): # [2 marks]
def __init__(self):
pass
def reverse(self, s): # [8 marks]
rev = ""
for i in range(len(s)):
rev += s[-i-1]
return rev
def isPalindrome(self, s): [5 marks]
if s == self.reverse(s):
return True
else:
return False
Award more marks for good coding practices, e.g. testing boundary conditions, storing test
cases in a linear data structure (ease of extension), and so on.
Question 3.
a)
def sum(n): # iterative
total = 0
for i in range(n):
total += i
return total
[4 marks]
b)
def sum(nums): # iterative
total = 0
for i in nums:
total += 1
return total
[5 marks]
c)
i=1
while(i<20):
print "i = ", i
i += 1
[8 marks]
d)
for i in range(20, 0, -1):
print "i = ", i
[8 marks]
Question 4.
[5 marks]
[5 marks]
c) Consideration should be given to how the turtle can tell if it has moved off the track. One
strategy is to move the turtle forward a pixel or so, then rotate left and right by increasing
amounts until one of the sensors detects the track.
[15 marks]
Question 5.
[2 marks]
b)
def sum(arg):
if arg == []:
return 0
else: return arg[0] + sum(arg[1:])
Award two marks for the base case and three for the recursive case. Do not penalise
students for not using slicing.
[5 marks]
Award half marks for stating what the method does, two for a suitable argument and two
for a correct return value.
[8 marks]
d)
• iso(3) -> True
• iso(2) -> False
• ise(3) -> False
• ise(2) -> True
• iso returns True if the argument is odd and False otherwise. ise returns True if the
argument is even and False otherwise.
[10 marks]