Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
131 views

Python 3 Functions and OOPs

The document contains questions and answers related to Python functions and object-oriented programming concepts. Some key points covered include: - Generators consume less memory than lists. Multiple yield expressions can be used in a generator function. - The default return value of a Python function is None. Function headers include the function name and parameter list. - Special methods like __init__, __add__, __enter__, and __exit__ are used to support functionality like initialization, operator overloading, and the with statement in classes. - Exceptions like NameError, SyntaxError, ZeroDivisionError, and TypeError can occur in different situations. Finally blocks are always executed even if an exception occurs.

Uploaded by

TECHer YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Python 3 Functions and OOPs

The document contains questions and answers related to Python functions and object-oriented programming concepts. Some key points covered include: - Generators consume less memory than lists. Multiple yield expressions can be used in a generator function. - The default return value of a Python function is None. Function headers include the function name and parameter list. - Special methods like __init__, __add__, __enter__, and __exit__ are used to support functionality like initialization, operator overloading, and the with statement in classes. - Exceptions like NameError, SyntaxError, ZeroDivisionError, and TypeError can occur in different situations. Finally blocks are always executed even if an exception occurs.

Uploaded by

TECHer YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

The elements of an iterator can be accessed multiple times.

State if the
statement is True or False.
@
False
------------------
What is the return type of function 'id'?
@
int
----------------
Generators consume more space in memory than the lists. State if the statement
is True or false.
@
False
----------------
The output of expression, k = [print(i) for i in "maverick" if i not in "aeiou"]
is _______.
@
prints all characters that are not vowels
----------------
The output of the expression [ chr(i) for i in [65, 66, 67] ] is _______.
@
['A', 'B', 'C']

m
-----------------

er as
The output of the expression itertools.takewhile(lambda x: x<5, [1,4,6,4,1]) is

co
_______.

eH w
@
[1,4]

o.
----------------- rs e
What value does 'k' hold after executing the expression, k = [print(i) for i in
ou urc
"maverick" if i not in "aeiou"] ?
@
A list of None's
-------------------
o

The output of the expression {0 if i%2 ==0 else 1 for i in range(8)} is _______.
aC s

@
v i y re

{0, 1}
---------------
A generator function can have multiple yield expressions. State if the statement
is True or False.
@
ed d

True
ar stu

---------------
Which of the following types of arguments can be passed to a function?
Required arguments
@
All the options mentioned
sh is

----------------
What is the default return value of a Python function?
Th

@
None
---------------
Which of the following are present in a function header?
@
function name and parameter list
---------------
The output of the expression { ord(i) for i in 'apple' } is _______.
@
{97, 112, 108, 101}

================================================================================
==========

Which of the following method is used by a user defined class to support '+'
operator?

This study source was downloaded by 100000832806195 from CourseHero.com on 10-21-2021 04:05:31 GMT -05:00

https://www.coursehero.com/file/61825524/Python-3-Functions-and-OOPstxt/
@
__add__
--------------------------
What is the output of the following code?
class class1:
a = 1
def f1(self):
a = 2
class1.a += 1
print(class1.a, end=' ')
print(a, end=' ')
class1().f1()
class1().f1()

@
2 2 3 2
--------------------------------------------
What is the output of the following code?

class A:
x = 0

m
def __init__(self, a, b):

er as
self.a = a

co
self.b = b

eH w
A.x += 1

o.
def __init__(self): rs e
A.x += 1
ou urc
def displayCount(self):
print('Count : %d' % A.x)
o

def display(self):
aC s

print('a :', self.a, ' b :', self.b)


v i y re

a1 = A('George', 25000)
a2 = A('John', 30000)
a3 = A()
a1.display()
ed d

a2.display()
ar stu

print(A.x)

@
Results in Error
------------------------------------------
sh is

Whta is the output of the following code ?


Th

class A:
def __init__(self):
print('one')

def f(self):
print(float())
print(hex(-255))

class B(A):
def __init__(self):
print('two')

def f(self):
print(float())
print(hex(-42))
@

This study source was downloaded by 100000832806195 from CourseHero.com on 10-21-2021 04:05:31 GMT -05:00

https://www.coursehero.com/file/61825524/Python-3-Functions-and-OOPstxt/
two
0.0
-0x2a
-----------------------------------------
What is the output of the following code?

class A:
def __init__(self, a = 5):
self.a = a

def f1(self):
self.a += 10

class B(A):
def __init__(self, b = 0):
A.__init__(self, 4)
self.b = b

def f1(self):
self.b += 10

m
x = B()

er as
x.f1()

co
print(x.a,'-', x.b)

eH w
@

o.
"4 - 10" rs e
------------------------------------------
ou urc
Which of the following statement sets the metaclass of class A to B?
@
class A:
__metaclass__ = B
o

---------------------------------------------
aC s

What is the output of the following code?


v i y re

class A:
def __init__(self, x=5, y=4):
self.x = x
self.y = y
ed d
ar stu

def __str__(self):
return 'A(x: {}, y: {})'.format(self.x, self.y)

def __eq__(self, other):


return self.x * self.y == other.x * other.y
sh is

def f1():
Th

a = A(12, 3)
b = A(3, 12)
if (a == b):
print(b != a)
print(a)

f1()

@
False
A(x: 12, y: 3)
--------------------------------------
Which methods are invoked on entering into and exiting from the block of code
written in 'with' statement?
@
__enter__, __exit__

This study source was downloaded by 100000832806195 from CourseHero.com on 10-21-2021 04:05:31 GMT -05:00

https://www.coursehero.com/file/61825524/Python-3-Functions-and-OOPstxt/
------------------------------------
Which of the following keyword is used for creating a method inside a class ?
@
def
---------------------------------------
What is the output of the following code?

class grandpa(object):
pass

class father(grandpa):
pass

class mother(object):
pass

class child(mother, father):


pass

print(child.__mro__)

m
(<class '__main__.child'>, <class '__main__.mother'>, <class '__main__.father'>,

er as
co
<class '__main__.grandpa'>, <class 'object'>)

eH w
================================================================================

o.
========== rs e
ou urc
The output of the expression '2' == 2 is _________.
@
False
----------------------
o

Can one block of except statements handle multiple exception?


aC s

@
v i y re

Yes, like except NameError, SyntaxError, ...


--------------------------
In which of the following scenarios, finally block is executed?
@
always
ed d

----------------------
ar stu

Which of the following execption occurs, when an undefined object is accessed?


@
NameError
-------------------------
Which of the following execption occurs, when a number is divided by zero?
sh is

@
ZeroDivisionError
Th

------------------------
Which of the following exception occurs, when an integer object is added to a
string object?
@
TypeError
--------------
When will the else part of try-except-else be executed?
@
when no exception occurs
---------------
Which of the keyword is used to display a customised error message to the user?
@
raise
---------------
If a list has 5 elements, then which of the following exceptions is raised when
8th element is accessed?

This study source was downloaded by 100000832806195 from CourseHero.com on 10-21-2021 04:05:31 GMT -05:00

https://www.coursehero.com/file/61825524/Python-3-Functions-and-OOPstxt/
@
IndexError
----------------
How many except statements can a try-except block have?
@
more than zero

================================================================================
==========

Which of the following statement retreives names of all builtin module names?
@
import sys; sys.builtin_module_names
--------------
Which of the following expression can be used to check if the file
'C:\Sample.txt' exists and is also a regular file?
@
os.path.isfile(C:\Sample.txt)
--------------
Which of the following statement retreives names of all builtin objects?
@
import builtins; builtins.dict.keys()

m
--------------

er as
Which of the following modules is used to manage installtion, upgradtion,

co
deletion of other pacakages automatically?

eH w
@
pip

o.
----------------- rs e
Which of the following module is not used for parsing command line arguments
ou urc
automatically?
@
cmdparse
-----------------
o

Which of the following modules are used to deal with Data compression and
aC s

archiving?
v i y re

@
All of those mentioned
----------------
In Python, which of the following files is mandatory to treat a folder as a
package?
ed d

@
ar stu

init.py - Bold
-----------------
Which of the following is not a way to import the module 'm1' or the functions
'f1' and 'f2' defined in it?
@
sh is

import f1, f2 from m1


----------------
Th

Which of the following methods of 'random' module is used to pick a single


element, randomly, from a given list of elements?
@
choice
-----------------
Any Python Script can act like a Module. State if the statement is True or
False?
@
True
----------------

This study source was downloaded by 100000832806195 from CourseHero.com on 10-21-2021 04:05:31 GMT -05:00

https://www.coursehero.com/file/61825524/Python-3-Functions-and-OOPstxt/
Powered by TCPDF (www.tcpdf.org)

You might also like