Top Python Interview Questions
Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions which are asked in most companies such as Infosys, TCS, Wipro, Accenture, etc. The placement handbooks also have detailed explanations for every question ensuring you revise all the questions with detailed answers and explanations.
Question
33
Explanation
What is __init__() method in python?
Python provides a special method called as __init__() which is similar to constructor method in other programming languages like C++/Java. The term init indicates initialization.
As the name suggests, this method is invoked automatically when the object of a class iscreated.
Consider the example given here –
class Employee:
def __init__(self, name, age,salary):
self.name = name
self.age = age
self.salary = 20000
E1 = Employee("XYZ", 23, 20000)
# E1 is the instance of class Employee.
#__init__ allocates memory for E1.
print(E1.name)
print(E1.age)
print(E1.salary)
OUTPUT
XYZ
23
20000
Question
34
Explanation
What will be the output of the following program?
a = True
b = False
c = False
if a or b and c:
print ("ROOTWORKZ")
else:
print ("rootworkz")
output:
ROOTWORKZ
AND operator has higher precedence than OR operator. So, it is evaluated first. i.e, (b and c) evaluates to false.Now OR operator is evaluated. Here, (True or False) evaluates to True.
So the if condition becomes True and ROOTWORKZ is printed as output.
Question
35
Explanation
What will be the output.
class Hello:
def __init__(self, id):
self.id = id
mono = Hello(100)
mono.__dict__['life'] = 49
print mono.life + len(mono.__dict__)
In the above program we are creating a member variable having name ‘life’ by adding it directly to the dictionary of the object ‘mono’ of class ‘Hello’. Total numbers of items in the dictionary is 2, the variables ‘life’ and ‘id’.
Therefore the size or the length of the dictionary is 2 and the variable ‘life’ is assigned a value ’49’. So the sum of the variable ‘life’ and the size of the dictionary is 49 + 2 = 51.
Question
36
Explanation
What will be the output of the following program?
tuple = (1, 2, 3, 4)
tuple.append( (5, 6, 7) )
print(len(my_tuple))
OUTPUT:
Error
An exception will be thrown as tuples are immutable and don’t have an append method.
.png)