P h i l

advertisement
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 First Exam.
Date: 17-11-2013
First Semester of 2013-2014
Time: 50 minutes
Information for Candidates
1. This examination paper contains four questions, totaling 25 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 (3 marks)
1. How do you call __str__method?
simply by using . Access operator, if obj is an object than obj.__str__()
2. What’s wrong with the following?
L = [2, 5, 7]
L = L.append(3)
1.5 mark
1.5 mark
It returns none, does change the list.
Question2 (7 mark)
Act like the python interpreter and run the following program. What will the following python statements
display?
1.
def Multiply(a, b):
return a * b
list = [(6, 3), (1, 7), (5, 5) , (2, 8)]
(2.5 marks)
print [Multiply(y, x) for (x, y) in list]
[18, 7, 25, 16]
2.
print [(x, y) for x in [1,2] for y in [3,1,4] if x != y]
Ans: [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4)]
(2.5 marks)
3.
def f(a, L=[]):
L.append(a)
return L
2.5 marks
print f(1)
print f(2)
print f(3)
[1]
[1, 2]
[1, 2, 3]
2 marks
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 (3 marks)
Translate the following while loop into a for loop (which does the same thing as the while loop):
i = 20
while (i > 0):
print "i = ", i
i =- 1
for i in range
1
(20, 0, -1): 1
print "i = ", I
mark
mark
1
mark
Question4 (8 marks)
A vector of dimension n is an ordered collection of n elements, which are called components. Write a
Python class named Vector that has the following methods:
__init__ method that allow us to insert data into a Vector object when we construct it.
__str__ method that prints a Vector object we actually want to print the data contained in the Vector.
getn(n) method that returns nth component from the vector.
__add__ method that finds the sum of two vectors
i.e. if A = (A1, A2, ..., An) and B = (B1, B2, ..., Bn) are two vectors thanA + B = (A1 + B1, A2 + B2, ...,
An + Bn)
Answer
class Vector:
def __init__(self, data):
self.data = data
def __str__(self):
s=”[“
for j in range(len(self.data-1)):
s= s+str(data[j]) +”,”
s= s+str(data[(len(self.data)]) +”]”
return s
def getn(self, n):
return data[n]
def __add__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] + other.data[j])
return Vector(data)
each method 2 mark
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.
Question5 (4 marks)
Write a python function that reverses the order of the words in a sentence and the order of the letters in
each word
def reverse(s):
M=s.split()
M.reverse()
L=[]
for w in M:
L.append(w[::-1])
return ' '.join(L)
Download