python quiz 47-71

Q47:

names1 = ['Amir', 'Barry', 'Chales', 'Dao']
names2 = names1
names3 = names1[:]
names2[0] = 'Alice'
names3[1] = 'Bob'
sum = 0
for ls in (names1, names2, names3):
    if ls[0] == 'Alice':
        sum += 1
    if ls[1] == 'Bob':
        sum += 10
print sum

  1. 11 
  2. 12 
  3. 21 
  4. 22 
  5. 33 

Q48:

names1 = ['Amir', 'Barry', 'Chales', 'Dao']
loc = names1.index("Edward")
print loc

  1. -1 
  2. Edward 
  3. An exception is thrown 

Q49:

names1 = ['Amir', 'Barry', 'Chales', 'Dao']
if 'amir' in names1:
    print 1
else:
    print 2


  1. An exception is thrown

Q50:

names1 = ['Amir', 'Barry', 'Chales', 'Dao']
names2 = [name.lower() for name in names1]
print names2[2][0]

  1. An exception is thrown 

Q51:


numbers = [1, 2, 3, 4]
numbers.append([5,6,7,8])
print len(numbers)

  1. 12 
  2. An exception is thrown 


Q52:


list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print len(list1 + list2)

 

  1. An exception is thrown 

Q53:
 

def addItem(listParam):
    listParam += [1]
mylist = [1, 2, 3, 4]
addItem(mylist)
print len(mylist)

  1. An exception is thrown 

Q54:


my_tuple = (1, 2, 3, 4)
my_tuple.append( (5, 6, 7) )
print len(my_tuple)

  1. 7
  2. An exception is thrown 

Q55:
 

a = 1
b = 2
a,b = b,a
print "%d %d" % (a,b)

  1. 1 2 
  2. 2 1 
  3. An exception is thrown 
  4. This program has undefined behavior 

Q56:


def print_header(str):
    print "+++%s+++" % str
print_header.category = 1
print_header.text = "some info"
print_header("%d %s" % 
(print_header.category, print_header.text))

  1. +++1 some info+++ 
  2. +++%s+++ 
  3. some info 

Q57:


def dostuff(param1, *param2):
   print type(param2)
dostuff('apples', 'bananas', 'cherry', 'dates')

  1. str
  2. int
  3. tuple
  4. list
  5. dict

Q58:


def myfunc(x, y, z, a):
    print x + y
nums = [1, 2, 3, 4]
myfunc(*nums)

  1. 1
  2. 3
  3. 6
  4. 10

Q59:


class A:
    def __init__(self, a, b, c):
        self.x = a + b + c
a = A(1,2,3)
b = getattr(a, 'x')
setattr(a, 'x', b+1)
print a.x

  1. 1
  2. 2
  3. 3
  4. 6
  5. 7

Q60:


class NumFactory:
    def __init__(self, n):
        self.val = n
    def timesTwo(self):
        self.val *= 2
    def plusTwo(self):
        self.val += 2
f = NumFactory(2)
for m in dir(f):
    mthd = getattr(f,m)
    if callable(mthd):
        mthd()
print f.val

  1. 2
  2. 4
  3. 6
  4. 8
  5. an exception is thrown

Q61:


one = chr(104)
two = chr(105)
print "%s%s" % (one, two)

  1. hi 
  2. Inside the mytools dir create a __init__.py and myparser.py
  3. 104105 
  4. 104 

Q62:


x = 0
y = 1

a = cmp(x,y)
if a < x:
    print "a"
elif a == x:
    print "b"
else:
    print "c"

  1. a
  2. b
  3. c

Q63:


x = 1
y = "2"
z = 3
sum = 0
for i in (x,y,z):
    if isinstance(i, int):
        sum += i
print sum

  1. 2
  2. 3
  3. 4
  4. 6

Q64:


x = sum(range(5))
print x

  1. 4
  2. 5
  3. 10
  4. 15

Q65:


def getinput():
    print "0: start"
    print "1: stop"
    print "2: reset"
    x = raw_input("selection: ")
    try:
        num = int(x)
        if num > 2 or num < 0:
            return None
        return num
    except:
        return None

num = getinput()
if not num:
    print "invalid"
else:
    print "valid"

  1. valid
  2. invalid
  3. An exception is thrown

Q66:


kvps = { '1' : 1, '2' : 2 }
theCopy = kvps
kvps['1'] = 5
sum = kvps['1'] + theCopy['1']
print sum

  1. 1
  2. 2
  3. 7
  4. 10

Q67:


kvps = { '1' : 1, '2' : 2 }
theCopy = kvps.copy()
kvps['1'] = 5
sum = kvps['1'] + theCopy['1']
print sum

  1. 1
  2. 2
  3. 6
  4. 10

Q68:


aList = [1,2]
bList = [3,4]
kvps = { '1' : aList, '2' : bList }
theCopy = kvps.copy()
kvps['1'][0] = 5
sum = kvps['1'][0] + theCopy['1'][0]
print sum

  1. 1
  2. 2
  3. 6
  4. 10

Q69:


import copy
aList = [1,2]
bList = [3,4]
kvps = { '1' : aList, '2' : bList }
theCopy = copy.deepcopy(kvps)
kvps['1'][0] = 5
sum = kvps['1'][0] + theCopy['1'][0]
print sum

  1. 1
  2. 2
  3. 6
  4. 10

Q70:


kvps = { '1' : 1, '2' : 2 }
theCopy = dict(kvps)
kvps['1'] = 5
sum = kvps['1'] + theCopy['1']
print sum

  1. 1
  2. 2
  3. 6
  4. 10

Q71:
 

kvps = { '1' : 1, '2' : 2 , '3' : 3, '4' : 4, '5' : 5}
newData = { '1' : 10, '3' : 30 }
kvps.update(newData)
x = sum(kvps.values())
print x

  1. 15
  2. 51
  3. 150
  4. 139