python quiz 25-46

Q25: 

 name = "snow storm"

 print "%s" % name[6:8]

  1.  
  2. st 
  3. sto 
  4. to 
  5. tor 
  6. Syntax Error 

Q26:

name = "snow storm"

name[5] = 'X'

print name

  1. snow storm 
  2. snowXstorm 
  3. snow Xtorm 
  4. ERROR, this code will not run 

Q27:

for i in  range(2):
    print i
for i in range(4,6):
    print i

  1. 2, 4, 6 
  2. 0, 1, 2, 4, 5, 6 
  3. 0, 1, 4, 5 
  4. 0, 1, 4, 5, 6, 7, 8, 9 
  5. 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. 1 2 3 
  2. 1 2 1 3 
  3. 1 2 1 3 1 2 1 3 
  4. 1 1 1 1 2 2 3 3 
  5. Syntax Error 
     

Q29:

values = [2, 3, 2, 4]
def my_transformation(num):
    return num ** 2
for i in  map(my_transformation, values):
    print i

 

  1. 2 3 2 4 
  2. 4 6 4 8 
  3. 1 1.5 1 2 
  4. 1 1 1 2 
  5. 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

  1. 500 300 
  2. 500 500 
  3. 600 400 
  4. 600 600 
  5. 300 500 

Q31:

import math
print math.floor(5.5)

  1. 5.0 
  2. 5.5 
  3. 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. 49 
  2. 50 
  3. 51 

Q33:

x = "foo "
y = 2
print x + y

  1. foo 
  2. foo foo 
  3. foo 2 
  4. An exception is thrown 

Q34:

def simpleFunction():
    "This is a cool simple function that returns 1"
    return 1

print simpleFunction.__doc__[10:14]

  1. simpleFunction 
  2. simple 
  3. func 
  4. funtion 
  5. cool 

Q35:

sys.path.append('/root/mods')

  1. 改变python的启动路径
  2. 改变python目前的工作路径
  3. 添加一个新的python模块的搜索路径
  4. 从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

  1. 13 
  2. 14 
  3. 15

Q37:

names = ['Ramesh', 'Rajesh', 'Roger', 'Ivan', 'Nico']
下面哪个语句能够让列表中的名字按行输出?

  1. print "n".join(names)
  2. print names.join("n") 
  3. print names.concatenate("n") 
  4. print names.append("n") 
  5. print names.join("%sn", names) 

Q38:

foo = {}
print type(foo)

 

  1. set 
  2. dict 
  3. list 
  4. tuple 
  5. 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)

Q40:

confusion = {}
confusion[1] = 1
confusion['1'] = 2
confusion[1] += 1
sum = 0
for k in confusion:
    sum += confusion[k]
print sum

Q41:

confusion = {}
confusion[1] = 1
confusion['1'] = 2
confusion[1.0] = 4
sum = 0
for k in confusion:
    sum += confusion[k]
print sum

 

  1. 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. 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

 

  1.  8 
  2. 12 
  3. 24 
  4. 30 
  5. 33 

Q44:

foo = {1:'1', 2:'2', 3:'3'}
foo = {}
print len(foo)

  1. 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. 4
  2. An exception is thrown 

Q46:

names = ['Amir', 'Barry', 'Chales', 'Dao']
print names[-1][-1]

  1. Amir 
  2. Dao