Python 3 Application Programming MCQs Solution | TCS Fresco Play
Disclaimer: The primary purpose of providing this solution is to assist and support anyone who are unable to complete these courses due to a technical issue or a lack of expertise. This website's information or data are solely for the purpose of knowledge and education.
Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).
All Question of the MCQs Present Below for Ease Use Ctrl + F with the question name to find the Question. All the Best!
Course Path: Data Science/DATA SCIENCE BASICS/Python 3 Application
1.What does the match method of re module do?
- It matches the pattern at end of the string
- It matches the pattern at any position of the string
- It matches a pattern at the start of the string
- No such method exists
Answer: 3)It matches a pattern at the start of the string
2.What is the output of the expression re.split(r'[aeiou]', 'abcdefghij')?
- ['abcd', 'efgh', 'ij']
- ['a', 'bcd', 'e', 'fgj', 'i', 'j']
- Results in error
- ['', 'bcd', 'fgh', 'j']
Answer: 4)['', 'bcd', 'fgh', 'j']
3.Which of the following syntax is used to name a grouped portion of a match?
- ?P
- ?
- ?G
Answer: 2)?P
4.What does seek method of a file object do?
- Moves the current file position to a different location at a defined offset.
- Tells the current position within the file and indicates that the next read or write occurs from that position in a file.
- Returns various values associated with file object as a tuple
- Determines if the file position can be moved or not.
Answer: 1)Moves the current file position to a different location at a defined offset.
5.Which of the following expression is used to compile the pattern p?
- re.assemble(p)
- re.compile(p)
- re.regex(p)
- re.create(p)
Answer: 2)re.compile(p)
6.Which of the following modules support regular expressions in Python?
- re
- regex
- regexp
- reg
Answer: 1)re
7.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.group(0)
- mo.group()
- mo.groups()
- mo.group(1)
Answer: 3)mo.groups()
8.In a match found by a defined pattern, how to group various portions of a match?
- Using braces, {}
- Using angular brackets, <>
- Using square brackets, []
- Using paranthesis, ()
Answer: 4)Using paranthesis, ()
9.What does the search method of re module do?
- No such method exists
- It matches a pattern at the start of the string.
- It matches the pattern at end of the string.
- It matches the pattern at any position of the string.
Answer: 4)It matches the pattern at any position of the string.
1.Which of the following method is used to fetch all the rows of a query result?
- fetchone
- fetch
- select
- fetchall
Answer: 4)fetchall
2.Which of the following method is used to insert multiple rows at a time in sqlite3 datatbase?
- execute
- insert
- insertmany
- executemany
Answer: 4)executemany
3.Which of the following package provides the utilities to work with Oracle database?
- oracle
- cx_Oracle
- cx_Pyoracle
- pyoracle
Answer: 2)cx_Oracle
4.pyodbc is an open source Python module that makes accessing ODBC databases simple.
- False
- True
Answer: 2)True
5.Which of the following package provides the utilities to work with MongoDB database?
- mongodb
- pymongo
- pymongodb
- mongopy
Answer: 2)pymongo
6.While using Object relation mappers for database connectivity, a table is treated as ____________.
- Function
- Method
- Object
- Class
Answer: 4)Class
7.Django web framework uses SQLAlchemy as Object-relational mapper.
- True
- False
Answer: 2)False
8.Which of the following package provides the utilities to work with postgreSQL database?
- psycopg2
- pypostgres2
- postgres2
- postgresql
Answer: 1)psycopg2
9.Which of the following package provides the utilities to work with MySQLDB database?
- MySQLdb
- mysqldb
- mysqlpkg
- mysql
Answer: 1)MySQLdb
10.Which of the following method is used to fetch next one row of a query result?
- fetchone
- fetch
- select
- fetchall
Answer: 1)fetchone
1.What is the output of the following code?
def outer(x, y):
def inner1():
return x+y
def inner2():
return x*y
return (inner1, inner2)
(f1, f2) = outer(10, 25)
print(f1())
print(f2())
- 35250
- 35 250
- 25035
- 250 35
2.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()])
- [0,1,2,3]
- [0,2,4,6]
- [0,0,0,0]
- [6,6,6,6]
Answer: 4)[6,6,6,6]
3.What is the output of the following code?
def outer(x, y):
def inner1():
return x+y
def inner2(z):
return inner1() + z
return inner2
f = outer(10, 25)
print(f(15))
- 50
- Error
- 60
- 45
Answer: 1)50
4.A closure does not hold any data with it.
- False
- True
Answer: 1)False
5.Which of the following is false about the functions in Python?
- A(x: 12, y: 3)
- A function can be returned by some other function
- A function can be passed as an argument to another function
- A function can be treated as a variable
Answer: 1)A(x: 12, y: 3)
6.What is the output of the following code?
def f(x):
return 3*x
def g(x):
return 4*x
print(f(g(2)))
- 8
- 6
- 24
- Error
Answer: 3)24
7.What is the output of the following code?
v = 'Hello'
def f():
v = 'World'
return v
print(f())
print(v)
- HelloHello
- WorldWorld
- HelloWorld
- WorldHello
8.A closure is always a function.
- False
- True
Answer: 2)True
1.Which of the following is true about decorators?
- Decorators always return None
- dec keyword is used for decorating a function
- Decorators can be chained
- A Decorator function is used only to format the output of another function
Answer: 3)Decorators can be chained
2.What is the output of the following code?
def bind(func):
func.data = 9
return func
@bind
def add(x, y):
return x + y
print(add(3, 10))
print(add.data)
Error
- 13
- 9
- 9
- 13
Answer: 1)13
3.What is the output of the following code?
from functools import wraps
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__)
- Error
- wrapper
- decorator_func
- square
Answer: 4)square
4.What is the output of the following code?
def star(func):
def inner(*args, **kwargs):
print("*" * 3)
func(*args, **kwargs)
print("*" * 3)
return inner
def percent(func):
def inner(*args, **kwargs):
print("%" * 3)
func(*args, **kwargs)
print("%" * 3)
return inner
@star
@percent
def printer(msg):
print(msg)
printer("Hello")
- ***%%%Hello%%%***
- %%%***Hello***%%%
- %%%***Hello%%%***
- ******Hello%%%%%%
5.What is the output of the following code?
def smart_divide(func):
def wrapper(*args):
a, b = args
if b == 0:
print('oops! cannot divide')
return
return func(*args)
return wrapper
@smart_divide
def divide(a, b):
return a / b
print(divide.__name__)
print(divide(4, 16))
print(divide(8,0))
- smart_divide0.25oops! cannot divide
- wrapper0.25oops! cannot divide
- smart_divide0.25oops! cannot divideNone
- wrapper0.25oops! cannot divideNone
6.Classes can also be decorated, if required, in Python.
- False
- True
Answer: 2)True
Quiz on Descriptors and Properties
1.What is the output of the following code?
class A:
def __init__(self, val):
self.x = val
@property
def x(self):
return self.__x
@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)
- TypeError
- 7
- AttributeError
- ValueError
Answer: 3)AttributeError
2.What is the output of the following code?
class A:
def __init__(self, value):
self.x = value
@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
- AttributeError
- 7
- George
Answer: 1)ValueError
3.Which of the following method definitions can a descriptor have?
- __get__ or __set__
- __get__, __set__
- any of __get__, set__, __delete__
- __get__, __set__, __delete__
Answer: 3)any of __get__, set__, __delete__
4.What is the output of the following code?
class A:
def __init__(self, x):
self.__x = x
@property
def x(self):
return self.__x
a = A(7)
a.x = 10
print(a.x)
- 10
- None
- AttributeError
- 7
Answer: 3)AttributeError
5.If a property named temp is defined in a class, which of the following decorator statement is required for deleting the temp attribute ?
- @temp.delete
- @property.deleter.temp
- @property.delete.temp
- @temp.deleter
Answer: 4)@temp.deleter
6.What is the output of the following code?
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)
Answer: )
b = A('Hello', '!!!')
print(a.z)
print(b.z)
- 25
- 25Hello!!!
- 2525
- Error
7.If a property named temp is defined in a class, which of the following decorator statement is required for setting the temp attribute?
- @temp.set
- @temp.setter
- @property.set.temp
- @property.setter.temp
Answer: 2)@temp.setter
8.Which of the following is true about property decorator?
- property decorator is used either for getting, setting or deleting an attribute
- property decorator is used either for setting or getting an attribute.
- property decorator is used only for setting an attribute
- property decorator is used only for getting an attribute
Answer: 1)property decorator is used either for getting, setting or deleting an attribute
Python
1.What is the output of the following code?
def s1(x, y):
return x*y
class A:
@staticmethod
def s1(x, y):
return x + y
def s2(self, x, y):
return s1(x, y)
a = A()
print(a.s2(3, 7))
- 10
- 4
- TypeError
- 21
Answer: 4)21
2.What is the output of the following code?
class A:
@staticmethod
def m1(self):
print('Static Method')
@classmethod
def m1(self):
print('Class Method')
A.m1()
- Class Method
- Static Method
- TypeError
- Static MethodClass Method
Answer: 1)Class Method
3.What is the output of the following code?
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()
- TypeError
- In Class A, Method m1.
- In Class A, Class Method m1.
- In Class A, Class Method m1.In Class A, Method m1.
Answer: 2)In Class A, Method m1.
4.Which of the following decorator function is used to create a class method?
- classmethod
- classfunc
- classdef
- class
Answer: 1)classmethod
5.What is the output of the following code?
class A:
@classmethod
def getC(self):
print('In Class A, method getC.')
class B(A):
pass
b = B()
B.getC()
b.getC()
- AttributeError
- In Class A, method getC.In Class A, method getC.
- In Class A, method getC.
- TypeError
6.The static Method is bound to Objects and Class.
- True
- False
class A:
@staticmethod
@classmethod
def m1(self):
print('Hello')
A.m1(5)
- AttributeError
- TypeError
- ValueError
- Hello
Answer: 2)TypeError
8.Which of the following decorator function is used to create a static method?
- staticdef
- static
- staticmethod
- staticfunc
Answer: 3)staticmethod
Quiz on Abstract Classes
1.What is the output of the following code?
from abc import ABC, abstractmethod
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()
- In class B, Method m1.In class B, Method m1.In class A, Method m1.
- In class B, Method m1.
- TypeError
- In class B, Method m1.In class B, Method m1.
2.Which of the following module helps in creating abstract classes in Python?
- ABC
- abc
- abstractclass
- abstract
Answer: 2)abc
3.What is the output of following code?
from abc import ABC, abstractmethod
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()
- In class A, Method m2.
- In class A, Method m1.
- In class B, Method m2.
- TypeError
Answer: 1)In class A, Method m2.
4.What is the output of following code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def m1():
print('In class A.')
a = A()
a.m1()
- AbstratError
- TypeError
- ClassError
- In class A.
Answer: 2)TypeError
5.What is the output of the following code?
from abc import ABC, abstractmethod
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()
- In class B, Method m1.In class C, Method m2.
- In class A, Method m1.In class C, Method m2.
- In class A, Method m1.In class B, Method m1.
- TypeError
6.What is the output of the following code?
from abc import ABC, abstractmethod
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)
- In class A, Method m1.
- AttributeError
- TypeError
- In class B, Method m1.
Answer: 4)In class B, Method m1.
7.What is the output of the following code?
from abc import ABC, abstractmethod
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()
- In class B, Method m1.In class B, Method m1.In class A, Method m1.
- AttributeError
- TypeError
- In class B, Method m1.In class B, Method m1.
Answer: 2)AttributeError
8.Which of the following decorator function is used to create an abstract method?
- abstract
- abstractmethod
- abstractdef
- abstractfunc
Answer: 2)abstractmethod
Quiz on Context Managers
1.What is the output of the following code?
from contextlib import contextmanager
@contextmanager
def tag(name):
print("<%s>" % name)
yield
print("</%s>" % name)
with tag('h1') :
print('Hello')
- <%h1>Hello</%h1>
- Hello
- <h1></h1>Hello
- <h1>Hello</h1>
2.What does the context manager do when you are opening a file using with?
- It writes into the opened file
- It closes the opened file automatically
- It does nothing
- It opens the file automatically
Answer: 2)It closes the opened file automatically
3.Popen of subprocess module is a context manager.
- True
- False
Answer: 1)True
4.Which of the following module helps in creating a context manager using decorator contextmanager?
- contextlib
- contextmanager
- manage
- managelib
Answer: 1)contextlib
Quiz on Coroutines
1.Select the correct statement that differentiates a Generator from a Coroutine.
- Generators and Coroutines output values
- Only Generators output values
- Generators and Coroutines take input values
- Only Coroutines take input values
Answer: 4)Only Coroutines take input values
2.What is the output of the following code?
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 'Jack' : 4Length of 'Black' : 5
- Results in Error
- Length of 'Jack Black' : 10
3.What is the output of the following code?
def stringDisplay():
while True:
s = yield
print(s*3)
c = stringDisplay()
next(c)
c.send('Hi!!')
- TypeError
- Hi!!Hi!!Hi!!
- Hi!!Hi!!Hi!!
- Hi!!
Answer: 2)Hi!!Hi!!Hi!!
4.What is the output of the following code?
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')
- First Name: GeorgeLast Name: Williams
- First Name: GeorgeLast Name: WilliamsFirst Name: John
- First Name: George
- Results in Error
5.Which of the following methods is used to pass input value to a coroutine?
- input
- pass
- bind
- send
Answer: 4)send
6.A Coroutine is a generator object.
- False
- True
Answer: 2)True
7.What is the output of the following code?
def stringDisplay():
while True:
s = yield
print(s*3)
c = stringDisplay()
c.send('Hi!!')
- Hi!!Hi!!Hi!!
- TypeError
- Hi!!
- Hi!!Hi!!Hi!!
Answer: 2)TypeError
Python 3 Application Programming - Final Assessment
1.What does tell method of a file object do?
- Tells the current position within the file and indicate that the next read or write occurs from that position in a file.
- Determines if the file position can be moved or not.
- Changes the file position only if allowed to do so else returns an error.
- Moves the current file position to a different location at a defined offset.
Answer: 1)Tells the current position within the file and indicate that the next read or write occurs from that position in a file.
2.What is the output of the expression re.sub(r'[aeiou]', 'X', 'abcdefghij')?
- Xbcdefghij
- Results in Error
- abcdefghij
- XbcdXfghXj
Answer: 4)XbcdXfghXj
3.Which of the following methods have to be defined in a class to make it act like a context manager?
- enter, quit
- enter, exit
- __enter__, __exit__
- __enter__, __quit__
Answer: 3)__enter__, __exit__
4.Which of the following statement is used to open the file C:\Sample.txt in append mode?
- open('C:\Sample.txt', 'w+')
- open('C:/Sample.txt', 'a')
- open('C:\Sample.txt', 'w')
- open('C:\Sample.txt', 'r+')
Answer: 2)open('C:/Sample.txt', 'a')
5.ZipFile utility of zipfile module is a context manager.
- True
- False
Answer: 1)True
6.Which of the following keywords is used to enable a context manager in Python?
- context
- enable
- contextmanager
- with
Answer: 4)with
7.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()
- mo.dict()
- mo.gdict()
- mo.group()
Answer: 1)mo.groupdict()
If you have any queries, please feel free to ask on the comment section.If you want MCQs and Hands-On solutions for any courses, Please feel free to ask on the comment section too.Please share and support our page!
Post a Comment