0% found this document useful (0 votes)
6 views

A

The document is an exam paper for a Computer Sciences diploma course at Cairo University for the academic year 2021-2022. It contains multiple-choice questions and coding problems related to Python programming. The exam covers topics such as variable assignment, loops, comments, data structures, and error identification in code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

A

The document is an exam paper for a Computer Sciences diploma course at Cairo University for the academic year 2021-2022. It contains multiple-choice questions and coding problems related to Python programming. The exam covers topics such as variable assignment, loops, comments, data structures, and error identification in code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Cairo University

Faculty of Graduate Studies for Statistical


Researches
Department: Computer Sciences

Academic Year: 2021-2022 Semester: Second

Date: 12/ 6/2022 Level: Diploma

Course Title: Course code:


Exam Time: # Exam. Sheets:
CS522
marks: 2 Hours 13 Pages
100
Exam. Instructions : ANSWER THE FOLLOWING
QUESTIONS in Python

Q uestion1 :

C h oose th e correctanswer for each ofth e fol


lowing:

1. W h ich ofth e fol


lowingassignaninteger val
ue to avariabl
e
correctl
y?
( a) x= 2 5 ( b) x= ’ h el
lo’ ( c) 2 x= 5 ( d) 2 x= ’ h el
lo’

2. W h ich ofth e fol


lowingisal
oopwith outbody?
( a) x= 3 ( b) x= 3
wh ile( x> 3 ) wh ile( x> 3 ) ;

( c) x= 3 ( d) x= 3
wh ile( x> 3 ) wh ile( x> 3 ) :
{} pass

3. W h ich ofth e fol


lowingisacorrectcom m entinpyth on?
( a) # comment ( b) ‘’ ’ comment
( c) ? comment ( d) * / comment/ *

Page 1 of 13
4. W h atisth e indexnumberofth e l
astel
em entofatupl
e with 5 0 el
em ents?

( a) 5 0 ( b) 4 9 ( c) -1 ( d) band c

5. T h e l
astprinted expressionisassigned to th e . . . . . . . . . . . . . . . variabl
e
( a) l
ast ( b) _ ( c) var ( d) exp

6. W h atisth e outputofth e fol


lowing:

A= 2 5
print( a)

( a) error ( b) 2 5 ( c) a ( d) A

7. W h ich ofth e fol


lowingisused to initial
iz e m ul
tipl
e variabl
eswith acom m on
val
ue?
( a) x= y: y = 3 3 ( b) x= y = z = 3 3
( c) x= z ; y = z ; x= 3 3 ; ( d) x& y& z= 3 3

8. a= input( “ enteryourinput: ”)
print( type( a) )

( a) int ( b) string ( b) fl
oat ( d) bool

9. x= '2 4 '+ '1 6 '


print( x)

( a) 4 0 ( b) x ( b) 2 1 ( d) 2 4 1 6

10. . . . . . . . . canstore differenttypesofval


ues
( a) variabl
e ( b) l
ist ( c) function ( d) aand c

Page 2 of 13
Q uestion2 :
C h oose th e equival
entcode for each ofth e fol
lowing:
x= 3
ifx> 3 :
print( ‘x> 3 ’ )
11. elifx< 3 :
print( ‘x< 3 ’ )
else:
print( ‘x= 3 ’ )
( a) x= 3
ifx> 3 print( ‘x> 3 ’ ) el
ifx< 3 print( ‘x< 3 ’ ) el
se print( ‘x= 3 ’ )

x= 3
( b)
ifx> 3 print( ‘x> 3 ’ ) el
se ifx< 3 print( ‘x< 3 ’ ) print( ‘x= 3 ’ ) el
se

( c) x= 3
print( ‘x> 3 ’ ) ifx> 3 el
se print( ‘x< 3 ’ ) ifx< 3 el
se print( ‘x= 3 ’ )

x= 3
( d) print( ‘x> 3 ’ ) ifx> 3 ifx< 3 print( ‘x< 3 ’ ) print( ‘x= 3 ’ ) el
se

d= { 'k1 ': 2 5 , 'k2 ': 4 6 }


12.
for k, vind. items( ) :
print( k, v)

d= { 'k1 ': 2 5 , 'k2 ': 4 6 } d= { 'k1 ': 2 5 , 'k2 ': 4 6 }


( a) ( b)
for k ind: for k ind. items( ) :
print( k, d[k]) print( d)

d= { 'k1 ': 2 5 , 'k2 ': 4 6 } d= { 'k1 ': 2 5 , 'k2 ': 4 6 }


( c) for k ind: ( d) for k ind:
print( k) print( d[k])

Page 3 of 13
13.
num= 3 5
ifnum% 2 = = 0 :
print( 'even')
else:
print( 'odd')

num = 3 5
( a)
print( [num % 2 = = 0 ]( 'odd', 'even') )

num = 3 5
( b) print( [num % 2 = = 0 ]( 'even', 'odd') )

num = 3 5
( c) print( ( 'even', 'odd') [num % 2 = = 0 ])

num = 3 5
( d)
print( ( 'odd', 'even') [num % 2 = = 0 ])

alph a= ( 'a', 'b', 'c', 'd')


14. for i inrange( l en( al ph a) ) :
print( al ph a[i])

alph a= ( 'a', 'b', 'c', 'd') alph a= ( 'a', 'b', 'c', 'd')
( a) ( b) for i inrange( l en( al ph a) ) :
for i inrange( l en( al ph a) ) :
print( i) print( al ph a)

alph a= ( 'a', 'b', 'c', 'd') alph a= ( 'a', 'b', 'c', 'd')
( c) for i inal ph a: ( d) for i inrange( l en( al ph a) ) :
print( i) pass

Page 4 of 13
15. x= 3
z = []
for i inrange( 3 ) :
z [i: ]= [i]

x= 3
( a) z = [i for i inrange( 3 ) ]

x= 3
( b)
z = [i ]

x= 3
( c)
z = for i inrange( 3 ) [i ]

( d) x= 3
z = [for i inrange( 3 ) i]

z = []
16.
z [0 : ]= 3

z = [] z = []
( a) ( b)
z [0 ]= 3 z+ = ( 3 )

z = [] z = []
( c) z . append( 3 ) ( d) z [- 1 ]= 3

Page 5 of 13
Q uestion3 :
C h oose th e error l
ine num ber ineach ofth e fol
lowing:

1. x= 1 2
17. 2. y= 4
3. fun( x, y)

4. deffun( x, y) :
5. print( x, y)

( a) l
ine 1 ( b) l
ine 2 ( c) l
ine 5 ( d) l
ine 3

18.
1. defcalcul
ate( a, b, c= [], d) :
2. returna+ b- d

3. w= x= 3
4. y, z = 5 , [1 , 2 ]
5. print( cal cul ate( x, y, z , w)

( a) l
ine 1 ( b) l
ine 2 ( c) l
ine 3 ( d) l
ine 4

1 . defsquare( * * nums, x) :
19. 2 . sqnums= { }
3 . for num, valinnums. items( ) :
4 . sqnums[num]= val * * 2
5 . return sqnums

6 . x= square( q= 2 , f= 4 )
7 . print( x)

( a) l
ine 1 ( b) l
ine 2 ( c) l
ine 3 ( d) l
ine 4

Page 6 of 13
1. deffoo( a, b) :
20. 2. c= a+ b
3. returnc
4. x= 3
5. print( foo( x) )

( a) l
ine 2 ( b) l
ine 3 ( c) l
ine 4 ( d) l
ine 5

1. deffun( * arg) :
21. 2. x= l
en( arg)
3. fori inrange( x) :
4. print( arg[i])
5. fun( 2 , 3 , 4 , a= 5 )

( a) l
ine 2 ( b) l
ine 3 ( c) l
ine 4 ( d) l
ine 5

1. s= { 1 , 2 , 3 }
22.
2. s. add( 3 )
3. print( s[0 ])
4. print( 9 ins)

( a) l
ine 1 ( b) l
ine 2 ( c) l
ine 3 ( d) l
ine 4

23. 1. d= { 'a': 1 2 3 , 'b': 9 8 0 }


2. d[3 ]= 2 3
3. print( 5 ind)
4. print( d[0 ])

( a) l
ine 1 ( b) l
ine 2 ( c) l
ine 3 ( d) l
ine 4

Page 7 of 13
1. defexp( * nums, x) :
24. 2. fori innums:
3. print( i)

4. exp( 1 , 4 , 5 , 7 )

( a) l
ine 1 ( b) l
ine 2 ( c) l
ine 3 ( d) l
ine 4

25. 1 . x= 5
2 . ifx> 0 :
3 . print( ‘positive’ )
4 . el se:
5 . print( ‘negative’ )

( a) l
ine 1 ( b) l
ine 2 ( c) l
ine 3 ( d) l
ine 4

1. x= { }
26. 2. x. add( 2 )
3. y= x
4. print( x)

( a) l
ine 1 ( b) l
ine 2 ( c) l
ine 3 ( d) l
ine 4

Page 8 of 13
Q uestion4 :
C h oose th e th e correctoutputeach ofth e fol
lowing:

defmyfun( a, b, c= [1 , 2 , 3 ]) :
c. append( a)
c. append( b)
27.
returnc
x, y= 4 , 8
print( myfun( b= x, a= y) )

( a) [1 , 2 , 3 , 8 , 4 ] ( b) no output ( c) [1 , 2 , 3 ] ( d) error

if- 5 0 inrange( - 1 , - 1 0 0 ) :
28. print( 'Y es')
else:
print( 'N o')

( a) Y es ( b) N o ( c) error ( d) output

deffoo( * grades) :
29. print( len( grades) )
print( type( grades) )
foo( 9 6 , 8 2 , 7 0 )

( a) 3 tupl
e ( b) 6 ( c) 3 Dictionary ( d) 6 tupl
e

Page 9 of 13
x= l
am bdaa: a* * 2
30. print( x( 3 ) )

( a) 6 ( b) 3 ( c) 9 ( d) 5

31.
t1 = ( 9 , 5 , 0 )
t2 = 'w', 'n', 'f'
print( t1 + t2 )

( a) 9 5 0 w n f ( b) t1 + t2 ( c) 9 5 0 ( d) w n f

32. x= 5
y= 8
txt= “ th e val
ue of{ 2 } + { 1 } is{ 0 }”. format( x+ y, y, x)
print( txt)

( a) th e val
ue of{ 2 } + { 1 } is{ 0 } ( b) th e val
ue ofx+ y isx+ y
( c) th e val
ue of5 + 8 is1 3 ( d) th e val
ue of1 3 + 8 is5

33. x= [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ]

print( x[1 : 6 : 2 ])

( a) 1 3 5 ( b) 0 1 2 3 4 ( c) 1 2 3 4 ( d) 0 2 4

34. z= { 6 , 9 , 3 }
z . remove ( 'b')
print( z )

( a) error ( b) no output ( c) { 9 , 3 , 6 } ( d) { 9 , 3 , 6 , ‘b’ }

Page 10 of 13
35. defsquare( sqr, nums) :
forn, valinnums. items( ) :
sqr[n]= val * * 2

h= {}
x= { 'a': 2 , 'b': 1 }
square( h , x)
print( h )

( a) { } ( b) {'a' 4, 'b' 1} ( c) no output ( d) h

36. h = [3 , 2 , 1 ]
m = h [: ]
ifid( h ) = = id( m) : print( " yes" )
else: print( " N o" )

( a) yes ( b) N o ( c) Error ( d) no output

deffoo( x) :
fori inrange ( 2 ) :
37. x. append( i)
x= []
foo( x)
print( x)

( a) [] ( b) [0 , 1 ] ( c) x ( d) no output

defsum ( n) :
38. ifn= = 1 : return1
returnn+ sum( n- 1 )
print( sum( 3 ) )

( a) 4 ( b) 1 0 ( c) 6 ( d) [5 3 9 ]

Page 11 of 13
39. x= 5
wh il e( x< 9 ) :
x+ = 1
ifx> 5 : break
else: print( x)

( a) error ( b) no output ( c) 6 ( d) 8

deffun( s) :
s. append( 'worl
d')

40. e= ['h el l
o']
fun( e)
print( e)

( a) e ( b) ['h el
lo'] ( c) ['h el
lo', 'worl
d'] ( d) ['worl
d']

Page 12 of 13
rem ove( ) remove anitem from aset. Ifth e item to remove
doesnotexist, remove( ) wil
lraise anerror.
set( ) createsaset.

add( ) addsanitem to aset.

item s( ) returnsal
istofdictionary’ s( key, val
ue) tupl
e pairs.

l
en( x) returnsth e numberofitemsinth e col
lectionx.

range( n) generatesasequence ofnumbersfrom z ero to n- 1 .

append(n) add a single element to the end of the list

format() takes the passed arguments, formats them, and places


them in the string where the placeholders {} are

Page 13 of 13

You might also like