You are on page 1of 5

CSE1003ComputerProgramming

ClassTestOct/Nov2009
Total:40marks

Duration:1hour

SECTIONA(10marks)

MultipleChoiceQuestions
1. Whatwillbetheoutputforthefollowingprogram?
num1=3/4
num2=3.0/4
num3=3/4.0
printnum1=,num1,num2=,num2,num3=,num3

A. num1=0.75num2=0.75num3=0.75
B. num1=0num2=0.75num3=0.75
C. num1=0num2=0num3=0.75
D. num1=1num2=0.75num3=0.75
E. num1=0.75num2=0num3=0

2. Whatwillbetheoutputofthefollowingprogramiftheuserentersthefollowingvalues:8,10?
x,y=input(Pleaseinserttwonumber)
a,b=x+y,xy
a,b=b,a
printa=,a,,b=,b

A. a=18,b=2
B. a=2,b=18
C. a=2,b=18
D. Theprogramgeneratesanderror
E. a=18,b=2

3. ThebestwaytoimplementamultiwaydecisioninPythonisbyusing
A. if
B. ifelse
C. ifelifelse
D. try
E. except

4. Theexpressionaor(bandc)isequivalentto:
A. aand(borc)

B. (aandb)orc
C. not(aand(borc))
D. (aorb)andc
E. Noneoftheabove

5. Whichofthefollowingconditionsisinvalid?
A. (a>b),giventhataandbareintegervariables
B. (c!=5andd!=2),giventhatcanddcontainscharactersxandyrespectively
C. ((A[0]>3)or!(A[3]!=A[0])),giventhatlistAcontainsintegerelements
D. (decision[0]==yorY),giventhatdecisionisanlistofcharacters
E. ain[1,2,3,4],giventhatainanintegervariable

6. Whatwouldbetheoutputofthefollowingstatements?
num1=3
num2=2
num3=3
ifnum1>num2:
ifnum2>num3:
printThisisthefun!
elifnum3>num1:
printPythonisinteresting
else:
printNothing
elifnum2>num3:
printHardworkpaysoff
else:
printSorry

A.
B.
C.
D.
E.

ThisisFun!
Pythonisinteresting
Nothing
Hardworkpaysoff
Sorry

7. APythonfunctiondefinitionbeginswith:
A.
B.
C.
D.
E.

def
define
function
defun
func

8. Afunctionwithnoreturnstatementreturns
A. nothing

B.
C.
D.
E.

itsparameters
itsvariables
None
Errormessage

9. Intheprogramcodesbelow
count=1
y=0
while(count<n):
x=input(Giveavalueforx)
if(x<0):
break
y=y+x*x
count=count+1

theloopcontrolvariable(s)is/are:
A.
B.
C.
D.
E.

countonly
xonly
bothcountandx
y
Noneofthevariablesinthecode

10. Considerthefollowingprogramcodes
sum=0.0
count=0
xStr=raw_input(Enteravalidvalue<EnterToQuit>)
whilexStr!=:
try:
x=eval(xStr)
sum=sum+x
count=count+1
xStr=raw_input(Enteravalidvalue<EnterToQuit>)
exceptNameError:
printGivenvaluenotvalid
xStr=raw_input(Enteravalidvalue<EnterToQuit>)
printTheaverageis,sum/count

ForwhichinputistheprogramlikelytodisplaythemessageGivenvalueisnotvalid
A.

B.

1.0

C.

D.

E.

A1

SECTIONB(30marks)

Question1
a. Thefollowingprogramiswrittentocalculatethesum,averageandproductof3numbers.However
theprogrammerleftsomemistakes.Identifyandcorrectthemistakes.
1stNum,2ndNum,3rdNum=input(Pleaseinputthreenumbers:)
sum=1stNum+2ndNum+3rdNum
average=sum/3
product=1stNum*2ndNum*3rdNum
printThesumissumtheaverageisaveragetheproductisproduct
[3marks]

b. WritetheappropriatePythoncodesforthefollowingflowchart:
Inputx1,x2,x3

No
No

x2>=x3

max=x3

Yes

x1>=x2

Yes

No

max=x2

max=x3

x1>=x3

Yes

max=x1

sqrtMax=Square
rootofmax
OutputsqrtMax

[5marks]

c. RewritethealternativeformofthefollowingexpressionusingtheDeMorganlaws:
ifnot(temperature>=30andpassenger<5):
printAirconisswitchedoff!
[2marks]

[Total:10marks]

Question2
a. UsingtheprecedenceruleofPython,givetheoutputofthefollowingprogramforeachofthethree
setsofinputgiven:

a,x,y,z=input(Pleaseentervaluesfora,x,y,andz:)
ifaandnotxoryandz:
printaandnotxoryandz
else:
printFalse

i. 1,0,0,0
ii. 1,1,1,0
iii. 0,1,1,1
[3marks]

b.

Write a function that takes as


parameter an 8digit number and returns the sum of the 8 digits. E.g. if the 8digit number is
23456789,thefunctionreturnsthevalue44(i.e.2+3+4+5+6+7+8+9).
[5marks]

c. Writeaprogramthatallowstheusertoenteran8digitnumberanddisplaysthesumofthe8digits.
Yourprogrammustmakeuseofthefunctiondefinedinpart(b)above.
[2marks]

[Total:10marks]

Question3
a. PredicttheoutputofthefollowingPythonprogram:
n=5
x=1
y=0
sum=25
foriinrange(n):
x=x+i
y=y+x
printi,x,y
sum=sum+y+x
printSum=,sum
[5marks]

b. Write a programthat continuously asks the user tokey in numerical values, finds out whetherthe
valueisdivisibleby3,anddisplaysanappropriatemessagetellingtheuserifthenumberisdivisible
by3ornot.Theprogramshouldstopwhentheusertypesinzero.
[5marks]

[Total:10marks]

You might also like