Faculty of Information Technology Department of Software Engineering Examination Paper Philadelphia University Lecturer : Dr. Ali Fouad Coordinator : Dr. Ali Fouad Internal Examiner : Dr. Samer Hanna Advanced Object Oriented Programming (721324) Sec. 1 Date: 22-12-2013 Second Exam. First Semester of 2013-2014 Time: 50 minutes Information for Candidates 1. This examination paper contains four questions, totaling 22 marks. 2. The marks for parts of questions are shown in round brackets. Advice to Candidates 1. You should attempt all questions. 2. You should write your answers clearly. I. Basic concepts Objective: The aim of the question in this part is to evaluate your knowledge and skills concerning with the basic concepts of object oriented. Question1 (4 marks) What will the following python code display? class Parent: # define parent class parentAttr = 100 def __init__(self): print "Calling parent constructor" def parentMethod(self): print 'Calling parent method' def setAttr(self, attr): Parent.parentAttr = attr def getAttr(self): print "Parent attribute :", Parent.parentAttr class Child(Parent): # define child class def __init__(self): print "Calling child constructor" def childMethod(self): print 'Calling child method' c = Child() # instance of child c.childMethod() # child calls its method c.parentMethod() # calls parent's method c.setAttr(200) # again call parent's method c.getAttr() # again call parent's method Calling child constructor Calling child method Calling parent method Parent attribute : 200 Each 1 mark Question2 (3 marks) How can you implement a property for an attribute self._x for a class in python? (3 mark) @property def x(self): return self._x @x.setter def x(self, value): self._x = value each statement 0.5 mark II. Familiar Problem Solving Objective: The aim of the question in this part is to evaluate your ability to solve problems in object oriented programming, focusing on constructors, assessors, and other methods. Question3 (10 marks) The following code defines the start of a class to represent bank accounts: class BankAccount(object): def __init__(self, name, number, balance): self.name = name self.number = number self.balance = balance 1) Add instance methods called deposit(), withdraw() which increase and decrease the balance of the account. Make sure the withdraw() method doesn't allow the account to go into less than zero, and __str__ method. def deposit(self, amt): self.balance += amt def withdraw(self, amt): if self.balance – amt > 0: self.balance -= amt def __str__(self): return “\nname is ”+ self.name+ “\nnumber is”+ str(self.number)+ “\nbalance”+ str(self.balance)+ (5 marks) each line 0.5 2) Create a subclass of BankAccount called StudentAccount. Every StudentAccount should have an overdraft limit of 1000. Write a constructor for the new class. Override the withdraw() method to make sure that students can withdraw and doesn't allow the account to go into less 1000. class StudentAccount (BankAccount): def __init__(self, name, number, balance): BankAccount.__init__(self, name, number, balance) def withdraw(self, amt): if self.balance – amt > 1000: self.balance -= amt each line 0.5 5 marks III. Unfamiliar Problem Solving Objective: The aim of the question in this part is to evaluate that student can solve familiar problems with ease and can make progress toward the solution of unfamiliar problems, and can set out reasoning and explanation in clear and coherent manner. Question4 (5 marks) Create a class named Bank that uses a list to store accounts which includes the following methods: constructor method addAccount method to add new account to the bank totalBalance that return the total momey in the bank class Bank : def __init__(self): self.d = [] def addAccount(self, e): self.d.append(e) def totalBalance (self ): sum=0 for acc in self.d: sum=sum + acc.balance return nd each line 0.5 point