Philadelphia University

Lecturer : Dr. Ali Fouad

Coordinator : Dr. Ali Fouad

Internal Examiner : Dr. Samer Hanna

Advanced Object Oriented Programming (721324) Sec. 1 Final Exam. First Semester of 2014-2015

Date: 6-2-2014 Time: 120 minutes

Information for Candidates

1. This examination paper contains five questions, totaling 40marks.

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 (10 marks)

1) What is the difference between = = and is?

“is” means two variables refer to the same object

“==“ means that two variables or expressions refer to the same value: 2 marks

2) Does the following statements have an error? If so, why?

a)

t = (1, ‘a’, 9, 2)

t[0] = 6

Yes. You cannot change the value of an element in a tuple (what the second line tries to do)

because tuples are immutable. 1 mark

b)

t = [1, ‘a’, 9, 2]

t[0] = 61 mark

No. You can change the value of an element in a list (what the second line of code is doing) because lists are mutable.

3) Write the output produced by this program below.

  1. x=3
  2. if2x:
  3. print'First'
  4. else:
  5. print'Second'
  6. if2x:
  7. print'Third'
  8. print'Fourth'
  9. print`Fifth'

Answer:

Second1.5 marks

Fourth

Fifth

4) Write a short Python code segment that adds up the lengths of all the words in a text file and then prints the average (mean) length.

Lword=file(“words.txt”).read().split()

sum = 0

for i in lword: 3.5 marks

sum = sum + len(i)

print float (sum)/len(lword)

5) Write a statement that makes a call to the method “myprint”

class MyClass(object):

@staticmethod

def myprint(x):

print x

MyClass. myprint (2)1 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.

Question2 (10 marks)

Act like the python interpreter and run the following program. Write what the following would be printed out:

1)

colours1 = ["red", "green"]

colours2 = colours1

colours2 = ["rouge", "vert"]

print colours1 ['red', 'green'] 1 mark

colours2 = colours1

colours2[1] = "blue“

print colours1  ['red', 'blue']1 mark

2)

part = [1,2]
whole = [part, part]
shallow = whole[:]# not shallow = whole
deep = copy.deepcopy(whole)
del part[0]
print part  [2]0.5 mark
print whole  [[2], [2]] 1 mark
print shallow  [[2], [2]] 1 mark
print deep  [[1, 2], [1, 2]] 1 mark

del deep[0][0]

print deep  [[2], [2]] 0.5 mark

3)

def sub(a, b):
return a - b

list = [(6, 3), (1, 7), (5, 5) , (2, 8)](2 marks)

print [sub(x, y) for (x, y) in list]

[3, -6, 0, -6]each 0.5

4.

class Vector:

count = 0

def __init__(self,a , b):

self.a=a

self.b=b

count+=1

def __str__(self):

return ‘Vector (%d, %d)’% (self.a, self.b)

def __add__(self, other):

return Vector(self.a+other.a, self.b+other.b)

v1 = Vector(2, 10)

v2 = Vector(5, -2)

v3 = Vector(1, 1)

v4= v1+ v2+ v3

print Vector.count  32 marks

print v4Vector (8, 9)

Question3 (15 marks)

1) Write a class named Student has the following data members:

Data member Description

name name of the student

id identification number of the student

scores list of test scores

studentGrade student status (pass/no pass)

There will be four member functions:

constructor that sets name, id and initializes scores and student grade

addscore adds score to list of test scores

str to return string representation of the data members

get studentGrade returns the student grade

2) Write a derive class UndergraduateStudent form the Student class with method “computeCourseGrade” that sets studentGrade to “pass” if the average of test scores is equal or greater than 60 otherwise sets to “nopass”.

3) Write a derive class PostgraduateStudent form the Student class with method “computeCourseGrade” that sets studentGrade to “pass” if the average of test scores is equal or greater than 70 otherwise sets to “nopass”.

class Student(object):

def __init__(self, name, id):2 marks

self.name = name

self.id = id

self.scores = []

self.studentGrade = "“****"

def addscore(self, score):

self.scores.append(score)2 marks

def __str__(self):2 marks

return "name:"+ self.name+ "Id:"+self.id+ "studentGrade:"+self.studentGrade

def getstudentGrade(self):2 mark

returns self.studentgrade

class UndergraduateStudent(Student):

def computeCourseGrade(self):

sum=0

for s in self.scores:

sum+=s

if s/len(self.scores)>=60:3 marks

self.studentGrade="pass"

else:

self.studentGrade="nopass"

class PostgraduateStudent(Student):2 marks

def computeCourseGrade(self):

sum=0

for s in self.scores:

sum+=s

if s/len(self.scores)>=70:

self.studentGrade="pass"

else:

self.studentGrade="nopass"

4) define undergraduate student with name =“Ahmed”, id=”201020018”, scores=[79,90,40], and compute its grade. 2 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 faculty that uses a dictionary to store students with the following methods:

constructor method

addstudent method to add new student to the faculty

removestudent method to remove student from the faculty.

reterive method to reterive students informations.

totalPass that returns number of the total pass students.

class Faculty :

def __init__(self):

self.d = {}

def addStudent(self, e):

self.d[e.id] = e

def removeStudent(self, e):

del self.d[e.id]

def reterive(self, id):

print self.d[id]

def totalPass (self ):

sum=0

for s in d:

if s, computeCourseGrade ==”pass”:

sum+=1

return sum