Вы находитесь на странице: 1из 5

Play with PYTHON By Gajendra Sir Mo.No.

:9810301034
Practices Worksheet
Question #1: what does the following code do? def a(b, c, d): pass
defines a list and initializes it defines a function, which does nothing (ans)
defines a function, which passes its parameters through defines an empty class
Question #2: what gets printed? Assuming python print (type(1/2))
<type 'int'> (ans) <type 'number'> <type 'float'>(ans) <type 'double'> <type 'tuple'>
Question #3: what is the output of the following code? print type([1,2])
<type 'tuple'> <type 'int'> <type 'set'> <type 'complex'> <type 'list'> - correct
Question #4: what gets printed?
def f(): pass
print (type(f()))
<type 'function'> <type 'tuple'> <type 'NoneType'> - correct <type 'str'> <type 'type'>
Question #5: what should the below code print?
print (type(1J))
<type 'complex'> (ans) <type 'unicode'> - <type 'int'> <type 'float'> <type 'dict'>
Question #6: what is the output of the following code?
print type(lambda:None)
<type 'NoneType'> <type 'tuple'> <type 'type'> <type 'function'> (ans) <type 'bool'>
Question #7: what is the output of the below program?
a = [1,2,3,None,(),[],]
print (len(a))
syntax error 4 5 6 (ans) 7
Question #8: what gets printed? Assuming python
print (type(1/2))
<type 'int'> <type 'number'> <type 'float'> - correct <type 'double'> <type 'tuple'>
Question #9: What gets printed?
d =lambda p: p * 2 x = d(x)
t = lambda p: p * 3 x = t(x)
x=2 x = d(x)
print (x)
7 12 24 – correct 36 48
Question #10: What gets printed?
x,y= 4.5,2 print (x//y)
2.0 – correct 2.25 9.0 20.25 21
Question #11: What gets printed?
nums = set([1,1,2,3,3,3,4]) print (len(nums))
1 2 4 – correct 5 7
Question #12: What gets printed?
x = True print ("yes")
y = False else:
z = False print ("no")
if x or y and z:
yes – correct no fails to compile
Question #13: What gets printed?
x = True print (2)
y = False elif not x or y or not y and x:
z = False print (3)
if not x or y: else:
print (1) print (4)
elif not x or not y and z:
1 2 3 – correct 4
Question #18: What gets printed?
counter = 1 counter += 1
def doLotsOfStuff(): doLotsOfStuff()
global counter print (counter)
for i in (1, 2, 3):
1 3 4 – correct 7 none of the above
Question #16: What gets printed? print r"\nwoow"
1. new line then the string: woow 3. the text like exactly like this: \nwoow - correct
2. the text exactly like this: r"\nwoow" 4. the letter r and then newline then the text: woow

Page 1 | g9_sharma@yahoo.com
Play with PYTHON By Gajendra Sir Mo.No.:9810301034
5. the letter r then the text like this: nwoow
Question #17: Assuming python 2.6 what gets printed?
f = None if i > 2:
for i in range (5): break
with open("data.txt", "w") as f: print (f.closed)
True - correct False None
Question #20: What gets printed?
print ("hello" 'world')
1. on one line the text: hello world 3. hello on one line and world on the next line
2. on one line the text: helloworld -correct 4. syntax error, this python program will not run -
Question #22: What gets printed?
print (0xA + 0xa)
0xA + 0xa 0xA 0xa 14 20 – correct 0x20
Question #24: What gets printed?
kvps = {"user","bill", "password","hillary"}
print (kvps['password'])
user bill password hillary Nothing. Python syntax error - correct
Question #26: What gets printed?
name = "snow storm"
print ("%s" % name[6:8])
st sto to – correct tor Syntax Error
Question #27: What gets printed?
name = "snow storm"
name[5] = 'X'
print (name)
snow storm snowXstorm snow Xtorm ERROR, this code will not run - correct
Question #28: Which numbers are printed?
for i in range(2):
print (i)
for i in range(4,6):
print (i)
1. 2, 4, 6 4. 0, 1, 4, 5, 6, 7, 8, 9
2. 0, 1, 2, 4, 5, 6 5. 1, 2, 4, 5, 6
3. 0, 1, 4, 5 - correct
Question #29: What sequence of numbers is printed?
values = [1, 2, 1, 3] else:
nums = set(values) return False
def checkit(num): for i in filter(checkit, values):
if num in nums: print (i)
return True

1. 1 2 3 4. 1 1 1 1 2 2 3 3
2. 1 2 1 3 - correct 5. Syntax Error
3. 1 2 1 3 1 2 1 3
Question #34: What gets printed?
x = "foo "
y=2
print (x + y)
1. foo 4. 2
2. foo foo 5. An exception is thrown - correct
3. foo 2
Question #38: Which of the following print statements will print all the names in the list on a separate line
names = ['Ramesh', 'Rajesh', 'Roger', 'Ivan', 'Nico']
1. print ("\n".join(names)) - correct 4. print (names.append("\n"))
2. print (names.join("\n")) 5. print (names.join("%s\n", names))
3. print (names.concatenate("\n"))
Question #41: What gets printed?
foo = {}
print (type(foo))

Page 2 | g9_sharma@yahoo.com
Play with PYTHON By Gajendra Sir Mo.No.:9810301034
1. set 3. list 5. object
2. dict - correct 4. tuple
Question #42: What gets printed?
foo = (3, 4, 5)
print (type(foo))
1. int 3. tuple - correct 5. set
2. list 4. dict
Question #43: What gets printed?
country_counter = {} country_counter[country] = 1
def addone(country): addone('China')
if country in country_counter: addone('Japan')
country_counter[country] += 1 addone('china')
else: print (len(country_counter))

0 1 2 3 – correct 4
Question #44: What gets printed?
confusion = {} sum = 0
confusion[1] = 1 for k in confusion:
confusion['1'] = 2 sum += confusion[k]
confusion[1] += 1 print (sum)
1 2 3 4 - correct 5
Question #45: What gets printed?
confusion = {} sum = 0
confusion[1] = 1 for k in confusion:
confusion['1'] = 2 sum += confusion[k]
confusion[1.0] = 4 print (sum)
2 4 6 – correct 7 An exception is thrown
Question #46: What gets printed?
boxes = {} jars['honey'] = 4
jars = {} crates['boxes'] = boxes
crates = {} crates['jars'] = jars
boxes['cereal'] = 1 print (len(crates[boxes]))
boxes['candy'] = 2
1 2 4 7 An exception is thrown - correct
Question #47: What gets printed?
numberGames = {} sum = 0
numberGames[(1,2,4)] = 8 for k in numberGames:
numberGames[(4,2,1)] = 10 sum += numberGames[k]
numberGames[(1,2)] = 12 print len(numberGames) + sum
8 12 24 30 33 - correct
Question #48: What gets printed?
foo = {1:'1', 2:'2', 3:'3'}
foo = {}
print len(foo)
0 - correct 1 2 3 An exception is thrown
Question #49: What gets printed?
foo = {1:'1', 2:'2', 3:'3'} del foo[2]
del foo[1] print len(foo)
foo[1] = '10'
1 2 - correct 3 4 An exception is thrown
Question #50: What gets printed?
names = ['Amir', 'Barry', 'Chales', 'Dao']
print names[-1][-1]
A r Amir Dao o - correct
Question #51: What gets printed?
names1 = ['Amir', 'Barry', 'Chales', 'Dao'] names2[0] = 'Alice'
names2 = names1 names3[1] = 'Bob'
names3 = names1[:] sum = 0
Page 3 | g9_sharma@yahoo.com
Play with PYTHON By Gajendra Sir Mo.No.:9810301034
for ls in (names1, names2, names3): if ls[1] == 'Bob':
if ls[0] == 'Alice': sum += 10
sum += 1 print (sum)
11 12 - correct 21 22 33
Question #52: What gets printed?
names1 = ['Amir', 'Barry', 'Chales', 'Dao']
loc = names1.index("Edward")
print (loc)
-1 0 4 Edward An exception is thrown - correct
Question #54: What gets printed? def dostuff(param1, **param2):
names1 = ['Amir', 'Barry', 'Chales', 'Dao'] print (type(param2))
names2 = [name.lower() for name in names1] dostuff('capitals', Arizona='Phoenix',
print names2[2][0] California='Sacramento', Texas='Austin')
i a c - correct C An exception is thrown In str tuple list dict - correct
Question #55: What gets printed? Question #74: What gets printed?
numbers = [1, 2, 3, 4] kvps = { '1' : 1, '2' : 2 }
numbers.append([5,6,7,8]) theCopy = kvps
print len(numbers) kvps['1'] = 5
4 5 - correct 8 12 An exception is thrown sum = kvps['1'] + theCopy['1']
Question #57: What gets printed? print (sum)
list1 = [1, 2, 3, 4] 1 2 7 10 – correct An
list2 = [5, 6, 7, 8] exception is thrown
print len(list1 + list2) Question #75: What gets printed?
2 4 5 8 - correct An exception is kvps = { '1' : 1, '2' : 2 }
thrown theCopy = kvps.copy()
Question #58: What gets printed? kvps['1'] = 5
def addItem(listParam): sum = kvps['1'] + theCopy['1']
listParam += [1] print (sum)
mylist = [1, 2, 3, 4] 1 2 6 – correct 10 An exception is thrown
addItem(mylist) Question #76: What gets printed
print( len(mylist)) aList = [1,2]
1 4 5 - correct 8 An exception is thrown bList = [3,4]
Question #59: What gets printed? kvps = { '1' : aList, '2' : bList }
my_tuple = (1, 2, 3, 4) theCopy = kvps.copy()
my_tuple.append( (5, 6, 7) ) kvps['1'][0] = 5
print len(my_tuple) sum = kvps['1'][0] + theCopy['1'][0]
1 2 5 7 An exception is thrown - correct print (sum)
Question #60: What gets printed? 1 2 6 10 – correct An exception is thrown
a=1 Question #77: What gets printed?
b=2 import copy
a,b = b,a aList = [1,2]
print "%d (%d" % (a,b)) bList = [3,4]
1 2 2 1 – correct An exception is thrown kvps = { '1' : aList, '2' : bList }
This program has undefined behavior theCopy = copy.deepcopy(kvps)
Question #64: What gets printed? kvps['1'][0] = 5
def mfunc(x, y, z, a): sum = kvps['1'][0] + theCopy['1'][0]
print (x + y) print (sum)
nums = [1, 2, 3, 4] 1 2 6 – correct 10 An exception is thrown
myfunc(*nums) Question #79: What gets printed?
1 3 – correct 6 10 An exception is thrown kvps = { '1' : 1, '2' : 2 , '3' : 3, '4' : 4, '5' : 5}
Question #62: What gets printed? newData = { '1' : 10, '3' : 30 }
def dostuff(param1, *param2): kvps.update(newData)
print (type(param2)) x = sum(kvps.values())
dostuff('apples', 'bananas', 'cherry', 'dates') print (x)
str int tuple – correct list dict 15 51 – correct 150 An exception is thrown
description: param2 aggregates remaining Question #80: What gets printed (with python version
parameters into a tuple. 3.X) assuming the user enters the following at the prompt?
Question #63: What gets printed? #: foo
Page 4 | g9_sharma@yahoo.com
Play with PYTHON By Gajendra Sir Mo.No.:9810301034
a = input("#: ") f foo – correct Not a number An exception is thrown
print (a)

Source or this worksheet is Internet

Page 5 | g9_sharma@yahoo.com

Вам также может понравиться