15+ Python MCQs with Answers
Looking for Python MCQs? Get ready to test your Python knowledge with 15+ Python MCQs with Answers and their answers in this comprehensive guide.
Q.1 Which of the following is correct with respect to the OOP concept in Python?
A. Objects are real-world entities while classes are not real.
B. Classes are real-world entities while objects are not real.
C. Both objects and classes are real-world entities.
D. Both objects and classes are not real.
Ans: A
Explanation:
In OOP, classes are basically the blueprint of the objects. They do not have physical existence.
Q.2 How many objects and reference variables are there for the given Python code?
class A:
print(“Inside class”) A() A() obj=A() |
A. 2 and 1
B. 3 and 3
C. 3 and 1
D. 3 and 2
Ans: C
Explanation:
obj is the reference variable here and an object will be created each time A() is called. So there will be 3 objects created.
Q.3 Which of the following is False with respect to Python code?
A. “std” is the reference variable for object Student(1,20)
B. id and age are called the parameters.
C. Every class must have a constructor.
D. None of the above
Ans : C
It is not mandatory for a class to have a constructor.
Q.4 What will be the output of below Python code?
A. 1
1
B. 1
2
C. 2
1
D. 2
2
Ans : B
Explanation:
When object with id =1 is created for Student, the constructor is invoked and it prints 1. Later, id has been changed to 2, so next print statement prints 2.
Q.5 What will be the output of below Python code?
A. 100
100
B. 100
102
C. 102
102
D. Error
Ans : B
Explanation:
By default,the value of “count” is 100, so obj1.count=100. For the second object, the value of “count” is 102, so obj2.count=102.
For Free, Demo classes Call: 02071171500
Registration Link: Click Here!
Q.6 Which of the following is correct?
A. id(a1) and id(a2) will have the same value.
B> id(a1) and id(a2) will have different values.
C. Two objects with same value of the attribute cannot be created.
D. None of the above
Ans : B
Explanation:
Although both a1 and a2 have same value of attributes,but these two point to two different objects. Hence, their id will be different.
Q.7. Which of the following is correct?
A. 5
B. 6
C. 0
D. Error
Ans : D
Explanation:
It will throw an error as inside the constructor, “count” is not defined.
Note: Unlock the power of Python with SevenMentor’s comprehensive Python training in Pune! Learn from industry experts, gain hands-on experience, and enhance your coding skills.
Q.8 Which of the following is correct?
A. Both book 1 and book 2 will have reference to two different objects of the class Book.
B. id(book1) and id(book2) will have the same value.
C. It will throw an error as multiple references to the same object is not possible.
D. None of the above
Ans: B
Explanation:
book1 and book2 will reference to the same object. Hence, id(book1) and id(book2) will have the same value.
Q.9 What will be the output of below Python code?
A. 5
7
B. 5
5
C. 3
3
D. 3
7
Ans : D
Explanation:
Inside constructor, self.num=3. First print statement prints 3. As, method change() is invoked, self.num=7 and hence second print statement prints 7.
Q.10. What is Instantiation in terms of OOP terminology?
A. Creating an instance of class
B. Copying an instance of class
C. Deleting an instance of class
D. Modifying an instance of class
Ans :- A
Q.11 Which of the following best describes inheritance?
A. Focuses on variables and passing of variables to functions
B. Ability of a class to derive members of another class as a part of its own definition
C. Allows for implementation of elegant software that is well designed and easily modified
D. Means of bundling instance variables and methods in order to restrict access to certain class members
Ans :- B
Explanation:-
If the class definition is class B(A): then class B inherits the methods of class A. This is called inheritance.
For Free, Demo classes Call: 02071171500
Registration Link: Click Here!
Q.12 Which of the following statements is wrong about inheritance?
A. The inheriting class is called a subclass
B. Inheritance is one of the features of OOP
C. Protected members of a class can be inherited
D. Private members of a class can be inherited and accessed
Ans :- D
Explanation:-
Any changes made to the private members of the class in the subclass aren’t reflected in the original members.
Q.13 Which of the following best describes polymorphism?
A. Focuses on variables and passing of variables to functions
B. Ability of a class to derive members of another class as a part of its own definition
C. Allows for objects of different types and behavior to be treated as the same general type
D. Means of bundling instance variables and methods in order to restrict access to certain class members
Ans :- C
Explanation:-
Polymorphism is a feature of object-oriented programming languages. It allows for the implementation of elegant software that is well designed and easily modified.
Q.14 Which of these is not a fundamental feature of OOP?
A. Inheritance
B. Instantiation
C. Encapsulation
D. Polymorphism
Ans :- B
Explanation:-
Instantiation simply refers to the creation of an instance of the class. It is not a fundamental feature of OOP.
Q.15 Which of the following is the most suitable definition for encapsulation?
A. Focuses on variables and passing of variables to functions
B. Ability of a class to derive members of another class as a part of its own definition
C. Allows for implementation of elegant software that is well-designed and easily modified
D. Means of bundling instance variables and methods in order to restrict access to certain class members
Ans :- D
Explanation:-
The values assigned by the constructor to the class members are used to create the object.
Q.16 What type of inheritance is illustrated in the following Python code?
class A():
pass
class B(A):
pass
class C(B):
pass
A. Multiple inheritances
B. Single-level inheritance
C. Multi-level inheritance
D. Hierarchical inheritance
Ans :- C
Explanation:-
In multi-level inheritance, a subclass derives from another class which itself is derived from another class.
For Free, Demo classes Call: 02071171500
Registration Link: Click Here!
Q.17 What will be the output of the following Python code?
class A:
def __init__(self):
self.__x = 1
class B(A):
def display(self):
print(self.__x)
def main():
obj = B()
obj.display()
main()
A. 0
B. 1
C. Error, invalid syntax for object declaration
D. Error, private class member can’t be accessed in a subclass
Ans :- D
Explanation:-
Private class members in the superclass can’t be accessed in the subclass.
Q.18 ______ is a string literal denoted by triple quotes for providing the specifications of certain program elements.
A. Client
B. Interface
C. Docstring
D. Modularity
Ans :- C
Explanation:-
Docstring used for providing the specifications of program elements.
Q.19 What does the function re. match do?
A. such a function does not exist
B. matches a pattern at the start of the string
C. matches a pattern at any position in the string
D. none of the mentioned
Ans :- B
Explanation:-
It will look for the pattern at the beginning and return None if it isn’t found.
Note: Join Python Course in Pune now and embark on an exciting journey into the world of Python programming
Q.20 What does the function re. search do?
A. such a function does not exist
B. matches a pattern at the start of the string
C. matches a pattern at any position in the string
D. none of the mentioned
Ans :- C
Explanation:-
It will look for the pattern at any position in the string.
Do visit our Python Video: Click Here
Author:-
Call now!!! | SevenMentor Pvt Ltd.
© Copyright 2021 | Sevenmentor Pvt Ltd.