51053 - Python course Questions and Answers
Q1) What is the output of the following code?
def multipliers():
return[lambda x : i*x for i in range (4)]
print([m(2) for m in multipliers()])
a) [0,0,0,0]
b) [6,6,6,6]
c) [0,1,2,3]
d) [0,2,4,6]
Answer : b) [6,6,6,6]
Q2) Which of the following string can be assigned to format argument of basicConfig function, in logging module, inorder to view a level followed by a message in each line or log file?
a) "%(levelname):%(message)"
b) "%(level)s:%(message)s"
c) "%(level):%(message)"
d) "%(levelname)s:%(message)s"
Answer : d) "%(levelname)s:%(message)s"
Q3) Which of the following statement set the metaclass of class A to B?
a) class A(metaclass=B): pass
b) class A:__metatype__= M
c) class A(meta=B): pass
d) class A:__metaclass__= B
Answer : d) class A:__metaclass__= B
Q4) Given the statement, d = dict(), which of the following statement is not valid for assigning a key-value pair to dictionary 'd1' ?
a) d1 = {1:4}
b) d1[4+1] = 15
c) d1 = {1, 4}
d) d1[3] = 9
Answer : b) d1[4+1] = 15
Q5) Which of the following modules warn about PEP 8 inconsistencies present in a python script ?
Select multiple Answers
a) pep8chk
b) pep8
c) pep8check
d) flake8
Answer : b & d
Q6) Which of the following is true about decorators ?
a) Decorators can be chained
b) dec keyword is used for decorating a function
c) A Decorator function is used only to format the output of another function
d) Decorators always return None
Answer: c) A Decorator function is used only to format the output of another function
Q7) What is the output of the following code?
class Person(object):
def__init__(self, name):
print("Mt name is ", name)
class Bob(Person)
def __init__(self, name = 'Bob'):
print('My name is Bob')
def ClassID (self):
print("I'm the father")
class Sue(Person):
def ___init___(self, name='Sue'):
print('My name is Sue')
def ClassID(self):
print("I'm the mother")
class Child(Bob, Sue):
def ___init___(self, name='X'):
super(Child, self).___init___(name)
def ClassID(self):
print("I'm the child")
Ann = Child('Ann')
Ann.ClassID()
Options:
a) My name is Ann
I'm the child
b) My name is Sue
I'm the child
c) My name is Ann
My name is Bob
I'm the child
d) My name is Non
I'm the child
Answer: a) My name is Ann
I'm the child
Q8) If A and B are sets, which is a valid set operation
a) A + B
b) A ^ B
c) A * B
d) A ! B
Answer : b) A ^ B
Q9) What is the type of variable 'a' defined as 'a =(5)' ?
a) tuple
b) list
c) int
d) str
Answer : c) int
Q10) Which of the following is not a standard level for logging various event using 'logging' module ?
a) INFO
b) LOG
c) CRITICAL
d) DEBUG
Answer: b) LOG
Q11) Which of the following expression does not create a tuple and assign in to variable 't1' ?
a) t1 = (1, 2, 3)
b) t1 = ('a', 'b', 'c')
c) t1 = tuple(1, 2, 3)
d) t1 = 1, 2, 3
Answer: d) t1 = 1, 2, 3
Q12) What is the output of the following code ?
def foo(n):
if (n < 3): yield 1
else: return
yield 2
n = 2
f =foo(n)
for i in range(n): print(f.___next___())
n = 5
f = foo(n)
for i in range(n): print(f.___next___())
Answer: 1 2
Q13) Which of the following module is not used for parsing command line argument automatically?
a) optparse
b) cmdparse
c) argparse
d) getopt
Answer: b) cmdparse
Q14) Which code extracts the matched data from the object returned by f1 in the given sample code?
import re
def f1(data):
p = re.compile('(?P[A-Z]{2,3}) (?P[0-9]{3})')
return p.search(data)
a) obj = f1('CS 101')dept,
num = obj[0], obj[1]
b) obj = f1('CS 101')dept,
num = obj.group('dept'), obj.group('num')
c) obj = f1('CS 101')dept,
num = obj.get('dept'), obj.get('num')
d) obj = f1('CS 101')dept,
num = obj['dept'], obj['num']
Answer :
b) obj = f1('CS 101')dept,
num = obj.group('dept'), obj.group('num')
Q15) Which of the following module warn about common sources of errors present in a python script ?
a) pyflakes
b) pyerrors
c) flake8
d) pywarn
Answer: A & C
Q16) Which of the following method is used by a user defined class to support '+' operator ?
a) __add__()
b) plus()
c) add()
d) __plus__()
Answer: a) __add__()
Q17) Which of the following statement creates a dictionary whose keys are elements of list 'keys' and assoicted values are from list 'vals' ?
a) { key:val for key in keys for val in vals }
b) dict(zip(keys,vals))
c) {keys: vals}
d) dict(keys, vals)
Answer: b) dict(zip(keys,vals))
Q 18) What is the output of the given statement? '{0:$>2d} * {1:$>2d} = {2:$>2d}'.format(5, 10, 5*10)
a) $5 * 10 = 50
b) 5 *10 = 50
c) $5 * $10 = $50
d) 5 * $10 = 50
Answer : c) $5 * $10 = $50
Q19) Which of the keyword is used to display a customised error message to the user ?
a) raise
b) yield
c) error
d) except
Answer: a) raise
Q20) Which of the following modules help in checking performance of python code?
a) timecheck
b) performcheck
c) pcheck
d) timeit
Answer: d) timeit