Font size
WorksheetsQuiz3-OOP-MakScie
Total questions: 40
Worksheet time: 40mins
What will be the output of the above code?
class A:
def show(self):
print("Class A")
class B(A):
pass
obj = B()
obj.show()
Error
Class A
None
No output
What is the value of b.value after executing the above code?
class A:
def init(self):
self.value = 10
class B(A):
def init(self):
super().__init__()
self.value += 5
b = B()
print(b.value)
15
10
20
0
What is the output of the above code?
class Parent:
name = "Parent"
class Child(Parent):
name = "Child"
c = Child()
print(c.name)
Parent
Child
Error
None
Consider the following code: class A:
def greet(self):
print("Hello from A")
class B(A):
def greet(self):
print("Hello from B")
class C(B):
pass
obj = C()
obj.greet()
Hello from A
Hello from B
Error
None
What will be the output?
class X:
def init(self):
print("X")
class Y(X):
def init(self):
super().__init__()
print("Y")
Y()
X
Y
X Y
Error
What will be the output of the above code?
class A:
def show(self):
print("A")
class B(A):
def show(self):
super().show()
print("B")
B().show()
A
B
A B
Error
What will be the output of the above code? Fill in the blank.
class A:
def init(self, x):
self.x = x
class B(A):
pass
b = B(5)
print(b.x)
(a)
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
class C(A, B):
pass
C().show()
A
B
Error
None
Given the following code: class A:
def init(self):
print("A")
class B(A):
def init(self):
print("B")
B()
What will be the output?
A
B
A B
None
Given the following code: class A:
def display(self):
print("A")
class B(A):
def display(self):
super().display()
print("B")
class C(B):
def display(self):
super().display()
print("C")
C().display() What will be the output?
A B C
C B A
A C B
B A C
class Car:
def start(self):
print("Starting engine")
class Electric(Car):
def start(self):
print("Powering electric motor")
obj = Electric()
obj.start()
Starting engine
Powering electric motor
Error
None
class A:
def info(self):
print("A info")
class B(A):
def info(self):
print("B info")
obj = B()
A.info(obj)
A info
B info
Error
None
class A:
pass
class B(A):
pass
print(issubclass(B, A))
True
False
Error
None
class Parent:
def init(self):
self.msg = "Hello"
class Child(Parent):
pass
print(hasattr(Child(), "msg"))
True
False
Error
None
Given the following code: class Animal:
def sound(self):
print("Some sound")
class Dog(Animal):
def sound(self):
super().sound()
print("Bark")
Dog().sound()
Bark
Some sound Bark
Error
None
class A:
def method(self):
print("A")
class B(A):
pass
class C(B):
pass
C().method()
A
B
C
D
class A:
def display(self):
print("A")
class B(A):
def display(self):
print("B")
class C(B):
def display(self):
super(A, self).display()
C().display()
A
B
Error
None
class Vehicle:
wheels = 4
class Bike(Vehicle):
wheels = 2
print(Vehicle.wheels, Bike.wheels)
2 2
4 2
4 4
Error
class Parent:
x = 5
class Child(Parent):
pass
Child.x += 10
print(Parent.x)
5
10
15
Error
What will be the output?
f = open("data.txt", "w")
f.write("Hello")
f.close()
f = open("data.txt", "r")
print(f.read())
Hello
Nothing
Error
data.txt
What will be the output?
with open("test.txt", "w") as f:
f.write("Python")
print(f.closed)
True
False
Error
None
Data
""
Error
None
with open("file.txt", "a") as f:
f.write("End")
print(f.mode)
w
a
r
rb
""
123
Error
None
f = open("data.txt", "w")
print(f.tell())
0
1
None
Error
f = open("file.txt", "w")
print(f.name)
file.txt
file
Error
None
f = open("data.txt", "w")
f.write("Python\nRocks")
f.close()
f = open("data.txt")
print(len(f.readlines()))
1
2
3
Error
H
Hi
i
with open("test.txt", "w") as f:
f.write("Hello\nWorld")
f.seek(0)
File pointer moves to end
Moves to start
Error
None
f = open("sample.txt", "w")
print(f.readable())
True
False
Error
None
with open("nums.txt", "w") as f:
f.writelines(["1\n", "2\n", "3\n"])
f = open("nums.txt")
print(f.readlines())
['1\n', '2\n', '3\n']
['123']
Error
None
try:
f = open("missing.txt", "r")
except FileNotFoundError:
print("File not found")
Error
File not found
None
Empty output
with open("log.txt", "w") as f:
f.write("Test")
print(f.closed)
True
False
Error
None
f = open("data.txt", "wb")
print(f.mode)
w
wb
rb
Error
f = open("file.txt", "w+")
print(f.writable())
True
False
Error
None
with open("file.txt", "w") as f:
f.write("12345")
print(f.tell())
5
0
1
10
What is the output of the following code? f = open("data.txt", "w") f.write("Hello") f.close() print(f.write("World"))
0
5
Error
None
What is the output of the following code? f = open("data.txt", "w") f.write("Hello") f.flush() print("Done")
Hello
Done
None
Error
What is the output of the following code? import os print(os.path.exists("test.txt"))
True
False
Depends on file existence
Error
