0% found this document useful (0 votes)
4 views11 pages

IMP Python Mcqs G

The document contains multiple-choice questions (MCQs) covering Python functions, modules, packages, object-oriented programming, file handling, and exception handling. Each question is followed by the correct answer and, in some cases, explanations for the answers. The content is structured into units focusing on different aspects of Python programming.

Uploaded by

r45736620
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)
4 views11 pages

IMP Python Mcqs G

The document contains multiple-choice questions (MCQs) covering Python functions, modules, packages, object-oriented programming, file handling, and exception handling. Each question is followed by the correct answer and, in some cases, explanations for the answers. The content is structured into units focusing on different aspects of Python programming.

Uploaded by

r45736620
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/ 11

PYTHON MCQS : CHAPTER 4,5,6

UNIT 4: PYTHON FUNCTIONS , MODULES AND PACKAGES

1] What will be the output of the following python function?


import math
abs(math.sqrt(25))
a) Error
b) -5
c) 5
d) 5.0

Answer: d) 5.0

2. Which of the following functions is a built-in function in python?


a) seed()
b) sqrt()
c) factorial()
d) print()

Answer: d
Explanation: The function seed is a function which is present in the random
module. The functions sqrt and factorial are a part of the math module. The
print function is a built-in function which prints a value directly to the system
output.

1] What will be the output of the following Python code?


1. x = 50
2. def func(x):
3. print('x is', x)
4. x=2
5. print('Changed local x to', x)
6. func(x)
7. print('x is now', x)
a)
x is 50
Changed local x to 2
x is now 50

b)
x is 50
Changed local x to 2
x is now 2

c)
x is 50
Changed local x to 2
x is now 100

d) None of the mentioned


Answer: a

2] Which keyword is used for function?


a) Fun
b) Define
c) Def
d) Function
Answer: c

3] If return statement is not used inside the function, the function will return :

a. None

b. 0

c. Null

d. Arbitary value

Answer: a

1. Which of these definitions correctly describes a module?


a) Denoted by triple quotes for providing the specification of certain program
elements
b) Design and implementation of specific functionality to be incorporated into
a program
c) Defines the specification of how it is to be used
d) Any program that reuses code
Answer: b

2. Program code making use of a given module is called a ______ of the


module.
a) Client
b) Docstring
c) Interface
d) Modularity
Answer: a

1] Which statement is correct to import all modules from the package


a) from package import all
b) from package import *
c) from package include all
d) from package include*

2] Which library offers many functions and tools to produce quality output in
variety of formats such as plot, Charts,Graph etc.
a) Numpy Library
b) Scipy ibra Library
c) Thinter Library
d) Malplotlib library
Answer- d)

3]. Which of the following is not an advantage of using modules?


a) Provides a means of reuse of program code
b) Provides a means of dividing up tasks
c) Provides a means of reducing the size of the program
d) Provides a means of testing individual parts of the program
Answer-c)
UNIT 5: OBJECT ORIENTED PROGRAMMING IN PYTHON

1] _____ is used to create an object.


a) class
b) constructor
c) User-defined functions
d) In-built functions

Answer: b) Constructor

2] What is getattr() used for?


a) To access the attribute of the object
b) To delete an attribute
c) To check if an attribute exists or not
d) To set an attribute

Answer: a) To access the attribute of the object

1) Select appropriate outcome for the following code:


Code:
class A:
def sayHello(self):
print ("Hello from class A")
class B(A):
def sayHello(self):
print ("Hello from class B")
b1 = B()
b1.sayHello()

A. Hello from class B.


B. Hello from class A.
C. Compile time error.
D. Both A and B.
Answer:- A. Hello from class B.
5.3
1. Which of the following is false about protected class members?
A. They can be accessed by subclasses
B. They can be accessed within a class
C. They begin with one underscore
D. They can be accessed by name mangling method
Answer: D. They can be accessed by name mangling method

1)The _________ module in the Python library provides the infrastructure for
defining custom abstract base classes.
1)math
2) abstractclass
3) abc
4) @abstractmethod
Ans:3)abc

2) _______ metaclass for defining the abstract class and _______ helper class ,
we can use it in the area where we want to avoid the confusion of metaclass
usage.
1)ABC , ABCMeta
2)ABCMeta , ABC
3) abstractclass , ABC
4) ABCMeta , abstractmethod
Ans : 2)ABCMeta , ABC

3) What is the importance of abstraction?


a. to reduce the complexity.
b. enhance application efficiency
c. Promote polymorphism
d. Both (a) and (b)

Answer: d) Both a and b

Which of the following best describes inheritance?


a) Ability of a class to derive members of another class as a part of its own
definition
b) Means of bundling instance variables and methods in order to restrict access
to certain class members
c) Focuses on variables and passing of variables to functions
d) Allows for implementation of elegant software that is well designed and
easily modified
Answer: a

Explanation: If the class definition is class B(A): then class B inherits the
methods of class A. This is called inheritance.

Which of the following statements is wrong about inheritance?


a) Protected members of a class can be inherited
b) The inheriting class is called a subclass
c) Private members of a class can be inherited and accessed
d) Inheritance is one of the features of OOP

Answer: c
Explanation: Any changes made to the private members of the class in the
subclass aren’t reflected in the original members.

1. What will be the output of the following Python code?


class A():
def disp(self):
print("A disp()")
class B(A):
pass
obj = B()
obj.disp()

a) Invalid syntax for inheritance


b) Error because when object is created, argument must be passed
c) Nothing is printed
d) A disp()

Answer: d

UNIT 6: FILE AND EXCEPTION HANDLING


1. What is the output of print('[%c]' % 65)
a) 65
b) A
c) [A]
d) Syntax error

Answer: c) [A]
The c conversion type supports character conversion from ASCII code to
the character. It also works for the Unicode

2. What is the output of print('%x, %X' % (15, 15))

A). 15 15
B). F F
C). ff
D). f F

Ans: D) f F

1. Which of the following writes a list in a file.


a. write()
b. writeline()
c. writelines()
d. writepara()

Ans: c.writelines()

1. What will be the output of the following Python code?


f = None
for i in range (5):
with open("data.txt", "w") as f:
if i > 2:
break
print(f.closed)
a) True
b) False
c) None
d) Error

Ans: a. True

1. Which mode create new file if the file does not exist?

a. write mode
b. append mode
c. Both of the above
d. None of the above

Ans. c. Both of the above

How many except statements can a try-except block have?


A. 0
B. 1
C. more than one
D. more than zero
Answer : D

Can one block of except statements handle multiple exception?


a) yes, like except TypeError, SyntaxError [,…]
b) yes, like except [TypeError, SyntaxError]
c) no
d) none of the mentioned
Answer: a
Which exception raised when a calculation exceeds maximum limit for a
numeric type?
A. StandardError
B. ArithmeticError
C. OverflowError
D. FloatingPointError
Answer : C

What will be the output of the following Python code?


def foo():
try:
print(1)
finally:
print(2)
foo()
a) 1 2
b) 1
c) 2
d) none of the mentioned

Select the correct statement for module


a) To create a module just save the code you want in a file with the file
extension .py
b) we can use the module, by using the import statement.
c) The module can contain functions, variables of all types (arrays, dictionaries,
objects etc)
d)All of the mentioned

What will be the output of the following Python function?


min(max(False,-3,-4), 2,7)
A. -4
B. -3
C. 2
D. False

What will be the output of the following Python code?


class tester:
def __init__(self, id):
self.id = str(id)
id="224"

temp = tester(12)
print(temp.id)
a) 1 2
b) 224
c)None
d) Error

To open a file c:\scores.txt for reading, we use _____________


a. infile = open(“c:\\scores.txt”, “r”)
b. infile = open(“c:\scores.txt”, “r”)
c. infile = open(file = “c:\scores.txt”, “r”)
d. infile = open(file = “c:\\scores.txt”, “r”)
Select output of following code:

try:

print(x)

except:

print(""An
exception
occurred"")

a. X
b. An exception occurred
c. Syntax Error
d. None of the above

You might also like