Python 3 Application Programming
Python 3 Application Programming
['','bcd','fgh','j']
Which of the following methods of a match object, mo, is used to view the
grouped portions of match in the form of a tuple
mo.groups()
Which of the following methods of a match object, mo, is used to view the named
group portions of match in the form of a dictionary
mo.groupdict()
pyodbc is an open source Python module that makes accessing ODBC databases
simple. State True or False
True
Which of the following method is used to fetch all the rows of a query result ?
fetchall
Which of the following method is used to fetch next one row of a query result ?
fetchone
Which of the following package provides the utilities to work with postgreSQL
database?
psycopg2
Which of the following package provides the utilities to work with MySQLDB
database?
MySQLdb
Which of the following package provides the utilities to work with mongodb
database?
pymongo
Which of the following package provides the utilities to work with Oracle
database?
cx_Oracle
def inner1():
return x+y
def inner2(z):
return inner1() + z
return inner2
f = outer(10, 25)
print(f(15))
50
def inner1():
return x+y
def inner2():
return x*y
print(f1())
print(f2())
35
250
v = 'Hello'
def f():
v = 'World'
return v
print(f())
print(v)
World
Hello
What is the output of the following code ?
def multipliers():
return [lambda x : i * x for i in range(4)]
[6,6,6,6]
A Closure does not hold any data with it. State True or False
False
def f(x):
return 3*x
def g(x):
return 4*x
print(f(g(2))
24
def decorator_func(func):
def wrapper(*args, **kwdargs):
return func(*args, **kwdargs)
wrapper.__name__ = func.__name__
return wrapper
@decorator_func
def square(x):
return x**2
print(square.__name__)
wrapper
def bind(func):
func.data = 9
return func
@bind
def add(x, y):
return x + y
print(add(3, 10))
print(add.data)
13
9
What is the output of the following code ?
***
%%%
Hello
%%%
***
def decorator_func(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@decorator_func
def square(x):
return x**2
print(square.__name__)
square
return func(*args)
return wrapper
@smart_divide def divide(a, b): return a / b
print(divide(8,0))
wrapper
0.25
oops! cannot divide
None
class A:
@x.setter
def x(self, val):
self.__x = val
@x.deleter
def x(self):
del self.__x
a = A(7)
del a.x
print(a.x)
Attribute Error
class A:
def __init__(self, x , y):
self.x = x
self.y = y
@property
def z(self):
return self.x + self.y
a = A(10, 15)
b = A('Hello', '!!!')
print(a.z)
print(b.z)
25
Hello!!!
class A:
@property
def x(self):
return self.__x
@x.setter
def x(self, value):
if not isinstance(value, (int, float)):
raise ValueError('Only Int or float is allowed')
self.__x = value
a = A(7)
a.x = 'George'
print(a.x)
ValueError
class A:
@property
def x(self):
return self.__x
a = A(7)
a.x = 10
print(a.x)
AttributeError
class A:
@staticmethod
def m1(self):
print('Static Method')
@classmethod
def m1(self):
print('Class Method')
A.m1()
Class Method
Static Method is bound to Objects and also the Class. State True or Flase
False
class A:
@classmethod
def getC(self):
print('In Class A, method getC.')
class B(A):
pass
b = B()
B.getC()
b.getC()
class A:
@staticmethod
def s1(x, y):
return x + y
a = A()
print(a.s2(3, 7))
21
class A:
@classmethod
def m1(self):
print('In Class A, Class Method m1.')
def m1(self):
print('In Class A, Method m1.')
a = A()
a.m1()
In Class A, Method m1
class A:
@staticmethod
@classmethod
def m1(self):
print('Hello')
A.m1(5)
TypeError
================================================================================
==================================================
What is the output of the following code ?
class A(ABC):
@classmethod
@abstractmethod
def m1(self):
print('In class A, Method m1.')
class B(A):
@classmethod
def m1(self):
print('In class B, Method m1.')
b = B()
b.m1()
B.m1()
A.m1()
class A(ABC):
@abstractmethod
def m1(self):
print('In class A, Method m1.')
class B(A):
def m1(self):
print('In class B, Method m1.')
class C(B):
def m2(self):
print('In class C, Method m2.')
c = C()
c.m1()
c.m2()
class A(ABC):
@abstractmethod
def m1():
print('In class A, Method m1.')
def m2():
print('In class A, Method m2.')
class B(A):
def m2():
print('In class B, Method m2.')
b = B()
b.m2()
TypeError
class A(ABC):
@abstractmethod
@classmethod
def m1(self):
print('In class A, Method m1.')
class B(A):
@classmethod
def m1(self):
print('In class B, Method m1.')
b = B()
b.m1()
B.m1()
A.m1()
AttributeError
class A(ABC):
@abstractmethod
def m1():
print('In class A.')
a = A()
a.m1()
TypeError
class A(ABC):
@abstractmethod
def m1(self):
print('In class A, Method m1.')
class B(A):
@staticmethod
def m1(self):
print('In class B, Method m1.')
b = B()
B.m1(b)
@contextmanager
def context():
print('Entering Context')
yield
print("Exiting Context")
with context():
print('In Context')
Entering Context
In Context
Exiting Context
What does the contex manger do when you are opening a file using with.
It closes the opened file automatically
@contextmanager
def tag(name):
print("<%s>" % name)
yield
print("</%s>" % name)
with tag('h1') :
print('Hello')
<h1>
Hello
</h1>
================================================================================
========================================================================
What is the output of the following code ?
def stringDisplay():
while True:
s = yield
print(s*3)
c = stringDisplay()
c.send('Hi!!')
TypeError
def stringDisplay():
while True:
s = yield
print(s*3)
c = stringDisplay()
next(c)
c.send('Hi!!')
Hi!!Hi!!Hi!!
def nameFeeder():
while True:
fname = yield
print('First Name:', fname)
lname = yield
print('Last Name:', lname)
n = nameFeeder()
next(n)
n.send('George')
n.send('Williams')
n.send('John')
def stringParser():
while True:
name = yield
(fname, lname) = name.split()
f.send(fname)
f.send(lname)
def stringLength():
while True:
string = yield
print("Length of '{}' : {}".format(string, len(string)))
f = stringLength(); next(f)
s = stringParser()
next(s)
s.send('Jack Black')
Length of 'Jack' : 4
Length of 'Black' : 5
Which of the following command is used to read the next line from a file using
the file object fo?
fo.readline()
Which of the following command is used to read n number of bytes from a file
using the file object fo?
fo.read(n)