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

Python OOPs and Functions

This document contains multiple choice questions (MCQs) about Python object-oriented programming concepts, functions, exceptions, modules, and built-in functions. It tests knowledge of topics like custom error messages with raise, handling multiple exceptions in a single except block, accessing undefined objects causing NameError, finally blocks always executing, and more. Methods for operators like __add__, context managers with __enter__ and __exit__, setting metaclasses, checking file existence, and using modules like random, itertools, and builtins are also assessed.

Uploaded by

yipemet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
341 views

Python OOPs and Functions

This document contains multiple choice questions (MCQs) about Python object-oriented programming concepts, functions, exceptions, modules, and built-in functions. It tests knowledge of topics like custom error messages with raise, handling multiple exceptions in a single except block, accessing undefined objects causing NameError, finally blocks always executing, and more. Methods for operators like __add__, context managers with __enter__ and __exit__, setting metaclasses, checking file existence, and using modules like random, itertools, and builtins are also assessed.

Uploaded by

yipemet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python OOPs and Functions:

MCQ’s

1.k = [print(i) for i in "maverick" if i not in "aeiou"]

Ans-> Prints all characters that are not vowels

2.What is the default return value of a Python function?

Ans-> None

3.The output of the expression [ chr(i) for i in [65, 66, 67] ] is _______.

Ans ->['A', 'B', 'C']

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

Ans ->[1, 4]

5. Generators consume more space in memory than the lists.

Ans ->False

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

Ans -> __add__

2.Which methods are invoked on entering into and exiting from the block of code written in 'with'
statement?

Ans -> __enter__, __exit__

3.Which of the following statement sets the metaclass of class A to B?

Ans -> class A:

__metaclass__ = B
4. what is the output of the code.

class A:

def __init__(self, x=5, y=4):

self.x = x

self.y = y

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

def f1():

a = A(12, 3)

b = A(3, 12)

if (a == b):

print(b != a)

print(a)

f1()

Ans ->False
A(x: 12, y: 3)
5. what Is the output of the 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

x = B()

x.f1()

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

Ans-> 4 - 10

1. Which of the keyword is used to display a customised error message to


the user?
Ans -> raise

2. Can one block of except statements handle multiple exception?


Ans-> Yes, like except NameError, SyntaxError, ...
3. Which of the following execption occurs, when an undefined object is
accessed?
Ans-> NameError

4. In which of the following scenarios, finally block is executed?


Ans-> Always

5. The output of the expression '2' == 2 is _________.


Ans-> False

1. Which of the following statement retreives names of all builtin module


names?
Ans-> import sys; sys.builtin_module_names

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


single element, randomly, from a given list of elements?
Ans-> Choice

3. Which of the following is not a way to import the module 'm1' or the
functions 'f1' and 'f2' defined in it?
Ans-> import f1, f2 from m1

4. In Python, which of the following files is mandatory to treat a folder as a


package?
Ans-> __init__.py

5. Which of the following expression can be used to check if the file


'C:\Sample.txt' exists and is also a regular file?
Ans-> os.path.isfile(C:\Sample.txt)
1. Which of the following modules are used to deal with Data compression
and archiving?
Ans->All the options

2.

class grandpa(object):
pass

class father(grandpa):
pass

class mother(object):
pass

class child(mother, father):


pass

print(child.__mro__)

Ans->

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


<class '__main__.grandpa'>, <class 'object'>)

3.Which of the following statement is not true about Python functions?

Ans-> Non-keyword arguments can be passed after keyword arguments

4.Which of the following error occurs, if an iterator is accessed, when it has


no elements?
Ans- StopIteration

5.If a list has 5 elements, then which of the following exceptions is raised
when 8th element is accessed?
Ans-> IndexError

6. The output of expression [x*y for x, y in zip([3,4],[5,6])]  is


_______.

Ans-> [15,24]

7. Which of the following modules contain functions that create iterators for
efficient looping?

Ans-> itertools

8. Which of the following statement retreives names of all builtin objects?

Ans-> import builtins; builtins.dict.keys()

9. The output of the expression 'list(itertools.dropwhile(lambda x: x<5,


[1,4,6,4,1]))' is _______.

Ans-> [6,4,1]

You might also like