Q25:
name = "snow storm"
print "%s" % name[6:8]
- st
- sto
- to
- tor
- Syntax Error
Q26:
name = "snow storm"
name[5] = 'X'
print name
- snow storm
- snowXstorm
- snow Xtorm
- ERROR, this code will not run
Q27:
for i in range(2):
print i
for i in range(4,6):
print i
- 2, 4, 6
- 0, 1, 2, 4, 5, 6
- 0, 1, 4, 5
- 0, 1, 4, 5, 6, 7, 8, 9
- 1, 2, 4, 5, 6
Q28:
values = [1, 2, 1, 3]
nums = set(values)
def checkit(num):
if num in nums:
return True
else:
return False
for i in filter(checkit, values):
print i
- 1 2 3
- 1 2 1 3
- 1 2 1 3 1 2 1 3
- 1 1 1 1 2 2 3 3
- Syntax Error
Q29:
values = [2, 3, 2, 4]
def my_transformation(num):
return num ** 2
for i in map(my_transformation, values):
print i
- 2 3 2 4
- 4 6 4 8
- 1 1.5 1 2
- 1 1 1 2
- 4 9 4 16
Q30:
import pickle
class account:
def __init__(self, id, balance):
self.id = id
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(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)
print myac.balance
fd = open( "archive", "r" )
myac = pickle.load( fd )
fd.close()
print myac.balance
- 500 300
- 500 500
- 600 400
- 600 600
- 300 500
Q31:
import math
print math.floor(5.5)
- 5
- 5.0
- 5.5
- 6
- 6.0
Q32:
class Person:
def __init__(self, id):
self.id = id
obama = Person(100)
obama.__dict__['age'] = 49
print obama.age + len(obama.__dict__)
- 1
- 2
- 49
- 50
- 51
Q33:
x = "foo "
y = 2
print x + y
- foo
- foo foo
- foo 2
- 2
- An exception is thrown
Q34:
def simpleFunction():
"This is a cool simple function that returns 1"
return 1
print simpleFunction.__doc__[10:14]
- simpleFunction
- simple
- func
- funtion
- cool
Q35:
sys.path.append('/root/mods')
- 改变python的启动路径
- 改变python目前的工作路径
- 添加一个新的python模块的搜索路径
- 从mods中移除所有的文件夹
Q36:
import re
sum = 0
pattern = 'back'
if re.match(pattern, 'backup.txt'):
sum += 1
if re.match(pattern, 'text.back'):
sum += 2
if re.search(pattern, 'backup.txt'):
sum += 4
if re.search(pattern, 'text.back'):
sum += 8
print sum
- 3
- 7
- 13
- 14
- 15
Q37:
names = ['Ramesh', 'Rajesh', 'Roger', 'Ivan', 'Nico']
下面哪个语句能够让列表中的名字按行输出?
- print "n".join(names)
- print names.join("n")
- print names.concatenate("n")
- print names.append("n")
- print names.join("%sn", names)
Q38:
foo = {}
print type(foo)
- set
- dict
- list
- tuple
- object
Q39:
country_counter = {}
def addone(country):
if country in country_counter:
country_counter[country] += 1
else:
country_counter[country] = 1
addone('China')
addone('Japan')
addone('china')
print len(country_counter)
- 0
- 1
- 2
- 3
- 4
Q40:
confusion = {}
confusion[1] = 1
confusion['1'] = 2
confusion[1] += 1
sum = 0
for k in confusion:
sum += confusion[k]
print sum
- 1
- 2
- 3
- 4
- 5
Q41:
confusion = {}
confusion[1] = 1
confusion['1'] = 2
confusion[1.0] = 4
sum = 0
for k in confusion:
sum += confusion[k]
print sum
- 2
- 4
- 6
- 7
- An exception is thrown
Q42:
boxes = {}
jars = {}
crates = {}
boxes['cereal'] = 1
boxes['candy'] = 2
jars['honey'] = 4
crates['boxes'] = boxes
crates['jars'] = jars
print len(crates[boxes])
- 1
- 2
- 4
- 7
- An exception is thrown
Q43:
numberGames = {}
numberGames[(1,2,4)] = 8
numberGames[(4,2,1)] = 10
numberGames[(1,2)] = 12
sum = 0
for k in numberGames:
sum += numberGames[k]
print len(numberGames) + sum
- 8
- 12
- 24
- 30
- 33
Q44:
foo = {1:'1', 2:'2', 3:'3'}
foo = {}
print len(foo)
- 0
- 1
- 2
- 3
- An exception is thrown
Q45:
foo = {1:'1', 2:'2', 3:'3'}
del foo[1]
foo[1] = '10'
del foo[2]
print len(foo)
- 1
- 2
- 3
- 4
- An exception is thrown
Q46:
names = ['Amir', 'Barry', 'Chales', 'Dao']
print names[-1][-1]
- A
- r
- Amir
- Dao
- o