P h i l

advertisement
Philadelphia University
Lecturer
: Dr. Ali Fouad
Coordinator
: Dr. Ali Fouad
Internal Examiner : Dr. Samer Hanna
Faculty of Information Technology
Department of Software Engineering
Examination Paper
Advanced Object Oriented Programming (721324) Sec. 1 First Exam.
Date: 9-5-2015
Second Semester of 2014-2015
Time: 50 minutes
Information for Candidates
1. This examination paper contains five 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 (10 marks)
1) Write the output produced by this program below. (3 marks)
x=3
if 2 > x :
print 'First'
else :
print 'Second'
if 1 > x :
print 'Third'
print 'Fourth'
print `Fifth'
2) Find the error in the following program. (2 marks)
1. line = raw_input("Type a word")
2. print "You typed", line
3. line = line + "h"
4. num = int(line)
5. print "You typed the number ", num
3) What is the difference between? (1 mark)
a, b = b - a, a - b
and
a=b-a
b=a–b
3) The program prints “There are 1 even integers in the list”, Why? (1 mark)
def even( data ) :
count = 0
for n in data :
if n % 2 == 0 :
count += 1
return count
### main ###
d = [8, 10, 7, 9, 4, 2]
print "there are %d even integers in the list" % even( d )
4) Write a Python fragment that displays the longest string in a list of words named names. For example,
if names refers to the list [“monitor”,”system”,”hardware”], then your program should display
”hardware”. (1 mark)
5) Write what the following would be printed out: (2 marks)
L = [(‘a’, 1), (‘d’, 10)]
print [ (x, i * 2) for (x, i) in L]
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 (4 mark)
Consider the following function that finds the sum of squares of consecutive integers.
def squares(num):
total = 0
L=[]
for i in range(num):
L.append(i*i)
total += i*i
return total, L
Write identical simplified function in form provides performance reflected by Python capabilities.
def squares(num):
L=[i*I for I in range(num)]
return sum(L),L
Question3 (5 mark)
Write your own version of Python’s range() function. The function, myrange(), will be called using the
form myrange(a, b) where a < b.
Myrange(s, e):
L=[];
While s< e:
L.append(s)
s = s+ 1
return L
Question4 (2 marks)
Show summary of object binding's trace to get the result for one of the following python code:
def dynamic(test, first, items):
no=0
for second in items:
if test(first, second):
no= no +1
return no
def equal(x, y): return x == y
print dynamic (equal, 2, [ 2, 1, 5, 2 ])
Solution:
test
item
args
equal,
2,
[4, 2, 1, 5, 2,6, 3]
no
0
1
1
1
2
arg
2
1
5
2
equal
T
F
F
F
0.5 mark
0.5 mark
0.5 mark
0.5 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 short Python code segment that finds the frequencies of English alphabetic in a string and then
prints the percentage of occurrences for each character.
For example if you have the string "akabca" then the output will be as follows:
[('a', 0.5), ('b', 0.16), ('c', 0.16), ('k', 0.16)]
print ([(x,s1.count(x)/len(s1)) for x in "abcdefghijklmnopqrstyxz" if x in s1 ])
Download