Review each question and reveal answers to strengthen your understanding
1Which function is used to close a file in python?
✅ Correct Answer: 1
2 Is it possible to create a text file in python?
✅ Correct Answer: 1
3Which of the following are the modes of both writing and reading in binary format in file?
✅ Correct Answer: 1
4Which of the following is not a valid mode to open a file?
✅ Correct Answer: 2
5What is the difference between r+ and w+ modes?
✅ Correct Answer: 2
6How do you get the name of a file from a file object (fp)?
✅ Correct Answer: 1
7Which of the following is not a valid attribute of a file object (fp)?
✅ Correct Answer: 4
8How do you close a file object (fp)?
✅ Correct Answer: 3
9How do you get the current position within the file?
✅ Correct Answer: 2
10How do you rename a file?
✅ Correct Answer: 2
11How do you delete a file?
✅ Correct Answer: 3
12How do you change the file position to an offset value from the start?
✅ Correct Answer: 1
13What happens if no arguments are passed to the seek function?
✅ Correct Answer: 4
14Which function is called when the following Python code is executed?
f = foo()
format(f)
✅ Correct Answer: 4
15Which function overloads the + operator?
✅ Correct Answer: 1
16Which operator is overloaded by __invert__()?
✅ Correct Answer: 2
17 Which function overloads the == operator?
✅ Correct Answer: 1
18Which operator is overloaded by __lg__()?
✅ Correct Answer: 4
19Which function overloads the >> operator?
✅ Correct Answer: 4
20Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
✅ Correct Answer: 1
21Which operator is overloaded by the __or__() function?
✅ Correct Answer: 2
22Which function overloads the // operator?
✅ Correct Answer: 3
23_____ represents an entity in the real world with its identity and behaviour.
✅ Correct Answer: 2
24____ is used to create an object.
✅ Correct Answer: 2
25What will be the output of the following Python code?
class test:
def __init__(self,a="Hello World"):
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
✅ Correct Answer: 3
26What is setattr() used for?
✅ Correct Answer: 2
27 What is getattr() used for?
✅ Correct Answer: 1
28What will be the output of the following Python code?
class change:
def __init__(self, x, y, z):
self.a = x + y + z
x = change(1,2,3)
y = getattr(x, 'a')
setattr(x, 'a', y+1)
print(x.a)
✅ Correct Answer: 2
29What will be the output of the following Python code?
class test:
def __init__(self,a):
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
✅ Correct Answer: 3
30What will be the output of the following Python code?
class test:
def __init__(self):
self.variable = 'Old'
self.Change(self.variable)
def Change(self, var):
var = 'New'
obj=test()
print(obj.variable)
✅ Correct Answer: 3
31What is Instantiation in terms of OOP terminology?
✅ Correct Answer: 4
32 What will be the output of the following Python code?
class fruits:
def __init__(self, price):
self.price = price
obj=fruits(50)
obj.quantity=10
obj.bags=2
print(obj.quantity+len(obj.__dict__))
✅ Correct Answer: 3
33 What will be the output of the following Python code?
class Demo:
def __init__(self):
pass
def test(self):
print(__name__)
obj = Demo()
obj.test()
✅ Correct Answer: 2
34The assignment of more than one function to a particular operator is _____
✅ Correct Answer: 3
35Which of the following is not a class method?
✅ Correct Answer: 1
36 Is the following Python code valid?
class B(object):
def first(self):
print("First method called")
def second():
print("Second method called")
ob = B()
B.first(ob)
✅ Correct Answer: 3
37What are the methods which begin and end with two underscore characters called?
✅ Correct Answer: 1
38 What will be the output of the following Python code?
>>> class demo():
def __repr__(self):
return '__repr__ built-in function called'
def __str__(self):
return '__str__ built-in function called'
>>> s=demo()
>>> print(s)
✅ Correct Answer: 3
39What will be the output of the following Python code?
>>> class demo():
def __repr__(self):
return '__repr__ built-in function called'
def __str__(self):
return '__str__ built-in function called'
>>> s=demo()
>>> print(s)
✅ Correct Answer: 1
40What is hasattr(obj,name) used for?
✅ Correct Answer: 3
41What will be the output of the following Python code?
class stud:
def __init__(self, roll_no, grade):
self.roll_no = roll_no
self.grade = grade
def display (self):
print("Roll no : ", self.roll_no, ", Grade: ", self.grade)
stud1 = stud(34, 'S')
stud1.age=7
print(hasattr(stud1, 'age'))
✅ Correct Answer: 2
42What is delattr(obj,name) used for?
✅ Correct Answer: 2
43What will be the output of the following Python code?
class stud:
‘Base class for all students’
def __init__(self, roll_no, grade):
self.roll_no = roll_no
self.grade = grade
def display (self):
print("Roll no : ", self.roll_no, ", Grade: ", self.grade)
print(student.__doc__)
✅ Correct Answer: 4
44 What does print(Test.__name__) display (assuming Test is the name of the class)?
✅ Correct Answer: 3
45Which of the following best describes inheritance?
✅ Correct Answer: 1
46Which of the following statements is wrong about inheritance?
✅ Correct Answer: 3
47What will be the output of the following Python code?
class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()
✅ Correct Answer: 3
48What 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()
✅ Correct Answer: 2
49Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?
✅ Correct Answer: 1
50 What will be the output of the following Python code?
class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
Test.__init__(self)
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()