PYTHON测试题.docx
《PYTHON测试题.docx》由会员分享,可在线阅读,更多相关《PYTHON测试题.docx(36页珍藏版)》请在冰豆网上搜索。
PYTHON测试题
1.whatdoesthefollowingcodedo?
(B)
defa(b,c,d):
pass
A.definesalistandinitializesit
B.definesafunction,whichdoesnothing
C.definesafunction,whichpassesitsparametersthrough
D.definesanemptyclass
2.whatgetsprinted?
Assumingpythonversion2.x(A)
printtype(1/2)
A.
B.
C.
D.
E.
3.whatistheoutputofthefollowingcode?
(E)
printtype([1,2])
A.
B.
C.
D.
E.
4.whatgetsprinted?
(C)
deff():
pass
printtype(f())
A.
B.
C.
D.
E.
5.whatshouldthebelowcodeprint?
(A)
printtype(1J)
A.
B.
C.
D.
E.
6.whatistheoutputofthefollowingcode?
(D)
printtype(lambda:
None)
A.
B.
C.
D.
E.
7.whatistheoutputofthebelowprogram?
(D)
a=[1,2,3,None,(),[],]
printlen(a)
A.syntaxerror
B.4
C.5
D.6
E.7
8.whatgetsprinted?
Assumingpythonversion3.x(C)
print(type(1/2))
A.
B.
C.
D.
E.
9.Whatgetsprinted?
(C)
d=lambdap:
p*2
t=lambdap:
p*3
x=2
x=d(x)
x=t(x)
x=d(x)
printx
A.7
B.12
C.24
D.36
E.48
10.Whatgetsprinted?
(A)
x=4.5
y=2
printx//y
A.2.0
B.2.25
C.9.0
D.20.25
E.21
11.Whatgetsprinted?
(C)
nums=set([1,1,2,3,3,3,4])
printlen(nums)
A.1
B.2
C.4
D.5
E.7
12.Whatgetsprinted?
(A)
x=True
y=False
z=False
ifxoryandz:
print"yes"
else:
print"no"
A.yes
B.no
C.failstocompile
13.Whatgetsprinted?
(C)
x=True
y=False
z=False
ifnotxory:
print1
elifnotxornotyandz:
print2
elifnotxoryornotyandx:
print3
else:
print4
A.1
B.2
C.3
D.4
14.IfPYTHONPATHissetintheenvironment,whichdirectoriesaresearchedformodules?
(D)
A)PYTHONPATHdirectory
B)currentdirectory
C)homedirectory
D)installationdependentdefaultpath
A.Aonly
B.AandD
C.A,B,andC
D.A,B,andD
E.A,B,C,andD
15.Inpython2.6orearlier,thecodewillprinterrortype1ifaccessSecureSystemraisesanexceptionofeitherAccessErrortypeorSecurityErrortype(B)
try:
accessSecureSystem()
exceptAccessError,SecurityError:
print"errortype1"
continueWork()
A.true
B.false
16.Thefollowingcodewillsuccessfullyprintthedaysandthenthemonths(B)
daysOfWeek=['Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday']
months=['Jan',\
'Feb',\
'Mar',\
'Apr',\
'May',\
'Jun',\
'Jul',\
'Aug',\
'Sep',\
'Oct',\
'Nov',\
'Dec']
print"DAYS:
%s,MONTHS%s"%
(daysOfWeek,months)
A.true
B.false
17.Assumingpython2.6whatgetsprinted?
(A)
f=None
foriinrange(5):
withopen("data.txt","w")asf:
ifi>2:
break
printf.closed
A.True
B.False
C.None
18.Whatgetsprinted?
(C)
counter=1
defdoLotsOfStuff():
globalcounter
foriin(1,2,3):
counter+=1
doLotsOfStuff()
printcounter
A.1
B.3
C.4
D.7
E.noneoftheabove
19.Whatgetsprinted?
(C)
printr"\nwoow"
A.newlinethenthestring:
woow
B.thetextexactlylikethis:
r"\nwoow"
C.thetextlikeexactlylikethis:
\nwoow
D.theletterrandthennewlinethenthetext:
woow
E.theletterrthenthetextlikethis:
nwoow
20.Whatgetsprinted?
(B)
print"hello"'world'
A.ononelinethetext:
helloworld
B.ononelinethetext:
helloworld
C.helloononelineandworldonthenextline
D.syntaxerror,thispythonprogramwillnotrun
21.Whatgetsprinted?
(E)
print"\x48\x49!
"
A.\x48\x49!
B.4849
C.4849!
D.4849!
E.HI!
22.Whatgetsprinted?
(D)
print0xA+0xa
A.0xA+0xa
B.0xA0xa
C.14
D.20
E.0x20
23.Whatgetsprinted?
(E)
classparent:
def__init__(self,param):
self.v1=param
classchild(parent):
def__init__(self,param):
self.v2=param
obj=child(11)
print"%d%d"%(obj.v1,obj.v2)
A.NoneNone
B.None11
C.11None
D.1111
E.Errorisgeneratedbyprogram
24.Whatgetsprinted?
(E)
kvps={"user","bill","password","hillary"}
printkvps['password']
A.user
B.bill
C.password
D.hillary
E.Nothing.Pythonsyntaxerror
25.Whatgetsprinted?
(B)
66%on1871timesasked
classAccount:
def__init__(self,id):
self.id=id
id=666
acc=Account(123)
printacc.id
A.None
B.123
C.666
D.SyntaxError,thisprogramwillnotrun
26.Whatgetsprinted?
(C)
name="snowstorm"
print"%s"%name[6:
8]
A.st
B.sto
C.to
D.tor
E.SyntaxError
27.Whatgetsprinted?
(D)
name="snowstorm"
name[5]='X'
printname
A.snowstorm
B.snowXstorm
C.snowXtorm
D.ERROR,thiscodewillnotrun
28.Whichnumbersareprinted?
(C)
foriinrange
(2):
printi
foriinrange(4,6):
printi
A.2,4,6
B.0,1,2,4,5,6
C.0,1,4,5
D.0,1,4,5,6,7,8,9
E.1,2,4,5,6
29.Whatsequenceofnumbersisprinted?
(B)
values=[1,2,1,3]
nums=set(values)
defcheckit(num):
ifnuminnums:
returnTrue
else:
returnFalse
foriinfilter(checkit,values):
printi
A.123
B.1213
C.12131213
D.11112233
E.SyntaxError
30.Whatsequenceofnumbersisprinted?
(E)
values=[2,3,2,4]
defmy_transformation(num):
returnnum**2
foriinmap(my_transformation,values):
printi
A.2324
B.4648
C.11.512
D.1112
E.49416
31.Whatnumbersgetprinted(C)
importpickle
classaccount:
def__init__(self,id,balance):
self.id=id
self.balance=balance
defdeposit(self,amount):
self.balance+=amount
defwithdraw(self,amount):
self.balance-=amount
myac=account('123',100)
myac.deposit(800)
myac.withdraw(500)
fd=open("archive","w")
pickle.dump(myac,fd)
fd.close()
myac.deposit(200)
printmyac.balance
fd=open("archive","r")
myac=pickle.load(fd)
fd.close()
printmyac.balance
A.500300
B.500500
C.600400
D.600600
E.300500
32.Whatgetsprintedbythecodesnippetbelow?
(B)
importmath
printmath.floor(5.5)
A.5
B.5.0
C.5.5
D.6
E.6.0
33.Whatgetsprintedbythecodebelow?
(E)
classPerson:
def__init__(self,id):
self.id=id
obama=Person(100)
obama.__dict__['age']=49
printobama.age+len(obama.__dict__)
A.1
B.2
C.49
D.50
E.51
34.Whatgetsprinted?
(E)
x="foo"
y=2
printx+y
A.foo
B.foofoo
C.foo2
D.2
E.Anexceptionisthrown
35.Whatgetsprinted?
(E)
defsimpleFunction():
"Thisisacoolsimplefunctionthatreturns1"
return1
printsimpleFunction.__doc__[10:
14]
A.simpleFunction
B.simple
C.func
D.funtion
E.cool
36.Whatdoesthecodebelowdo?
(C)
sys.path.append('/root/mods')
A.Changesthelocationthatthepythonexecutableisrunfrom
B.Changesthecurrentworkingdirectory
C.Addsanewdirectorytoseachforpythonmodulesthatareimported
D.Removesalldirectoriesformods
E.Changesthelocationwheresub-processesaresearchedforaftertheyarelaunched
37.Whatgetsprinted?
(C)
importre
sum=0
pattern='back'
ifre.match(pattern,'backup.txt'):
sum+=1
ifre.match(pattern,'text.back'):
sum+=2
ifre.search(pattern,'backup.txt'):
sum+=4
ifre.search(pattern,'text.back'):
sum+=8
printsum
A.3
B.7
C.13
D.14
E.15
38.Whichofthefollowingprintstatementswillprintallthenamesinthelistonaseperateline(A)
names=['Ramesh','Rajesh','Roger','Ivan','Nico']
A.print"\n".join(names)
B.printnames.join("\n")
C.printnames.concatenate("\n")
D.printnames.append("\n")
E.printnames.join("%s\n",names)
39.Trueorfalse?
Codeindentationmustbe4spaceswhencreatingacodeblock?
(B)
iferror:
#fourspacesofindentareusedtocreatetheblock
print"%s"%msg
A.True
B.False
40.Assumingthefilenameforthecodebelowis/usr/lib/python/person.py
andtheprogramisrunas:
python/usr/lib/python/person.py
Whatgetsprinted?
(D)
classPerson:
def__init__(self):
pass
defgetAge(self):
print__name__
p=Person()
p.getAge()
A.Person
B.getAge
C.usr.lib.python.person
D.__main__
E.Anexceptionisthrown
41.Whatgetsprinted(B)
foo={}
printtype(foo)
A.set
B.dict
C.list
D.tuple
E.object
42.Whatgetsprinted?
(C)
foo=(3,4,5)
printtype(foo)
A.int
B.list
C.tuple
D.dict
E.set
43.Whatgetsprinted?
(D)
country_counter={}
defaddone(country):
ifcountryincountry_counter:
country_counter[country]+=1
else:
country_counter[country]=1
addone('China')
addone('Japan')
addone('china')
printlen(country_counter)
A.0
B.1
C.2
D.3
E.4
44.Whatgetsprinted?
(D)
confusion={}
confusion[1]=1
confusion['1']=2
confusion[1]+=1
sum=0
forkinconfusion:
sum+=confusion[k]
printsum
A.1
B.2
C.3
D.4
E.5
45.Whatgetsprinted?
(C)
confusion={}
confusion[1]=1
confusion['1']=2
confusion[1.0]=4
sum=0
forkinconfusion:
sum+=confusion[k]
printsum
A.2
B.4
C.6
D.7
E.Anexceptionisthrown
46.Whatgetsprinted?
(E)
boxes={}
jars={}
crates={}
boxes['cereal']=1
boxes['candy']=2
jars['honey']=4
crates['boxes']=boxes
crates['jars']=jars
printlen(crates[boxes])
A.1
B.2
C.4
D.7
E.Anexceptionisthrown
47.Whatgetsprinted?
(E)
numberGames={}
numberGames[(1,2,4)]=8
numberGames[(4,2,1)]=10
numberGames[(1,2)]=12
sum=0
forkinnumberGames:
sum+=numberGames[k]
printlen(numberGames)+sum
A.8
B.12
C.24
D.30
E.33
48.Whatgetsprinted?
(A)
foo={1:
'1',2:
'2',3:
'3'}
foo={}
printlen(foo)
A.0
B.1
C.2
D.3
E.Anexceptionisthrown
49.Whatgetsprinted?
(B)
foo={1:
'1',2:
'2',3:
'3'}
delfoo[1]
foo[1]