wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz3-OOP-MakScie

Total questions: 40

Worksheet time: 40mins

Name
Class
Date
1.

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()

a)

Error

b)

Class A

c)

None

d)

No output

2.

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)

a)

15

b)

10

c)

20

d)

0

3.

What is the output of the above code?

class Parent:

    name = "Parent"

 

class Child(Parent):

    name = "Child"

 

c = Child()

print(c.name)

a)

Parent

b)

Child

c)

Error

d)

None

4.

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()

a)

Hello from A

b)

Hello from B

c)

Error

d)

None

5.

What will be the output?

class X:

    def init(self):

        print("X")

 

class Y(X):

    def init(self):

        super().__init__()

        print("Y")

 

Y()

a)

X

b)

Y

c)

X Y

d)

Error

6.

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)

A

b)

B

c)

A B

d)

Error

7.

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)  

8.

class A:

    def show(self):

        print("A")

 

class B(A):

    def show(self):

        print("B")

 

class C(A, B):

    pass

 

C().show()

a)

A

b)

B

c)

Error

d)

None

9.

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)

A

b)

B

c)

A B

d)

None

10.

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)

A B C

b)

C B A

c)

A C B

d)

B A C

11.

class Car:

    def start(self):

        print("Starting engine")

 

class Electric(Car):

    def start(self):

        print("Powering electric motor")

 

obj = Electric()

obj.start()

a)

Starting engine

b)

Powering electric motor

c)

Error

d)

None

12.

class Base:

    def init(self):

        self.data = 100

 

class Derived(Base):

    def init(self):

        super().__init__()

        self.data += 50

 

print(Derived().data)

(a)  

13.

class A:

    def info(self):

        print("A info")

 

class B(A):

    def info(self):

        print("B info")

 

obj = B()

A.info(obj)

a)

A info

b)

B info

c)

Error

d)

None

14.

class A:

    pass

 

class B(A):

    pass

 

print(issubclass(B, A))

a)

True

b)

False

c)

Error

d)

None

15.

class Parent:

    def init(self):

        self.msg = "Hello"

 

class Child(Parent):

    pass

 

print(hasattr(Child(), "msg"))

a)

True

b)

False

c)

Error

d)

None

16.

Given the following code: class Animal:

    def sound(self):

        print("Some sound")

 

class Dog(Animal):

    def sound(self):

        super().sound()

        print("Bark")

 

Dog().sound()

a)

Bark

b)

Some sound Bark

c)

Error

d)

None

17.

class A:

    def method(self):

        print("A")

 

class B(A):

    pass

 

class C(B):

    pass

 

C().method()

a)

A

b)

B

c)

C

d)

D

18.

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)

A

b)

B

c)

Error

d)

None

19.

class Vehicle:

    wheels = 4

 

class Bike(Vehicle):

    wheels = 2

 

print(Vehicle.wheels, Bike.wheels)

a)

2 2

b)

4 2

c)

4 4

d)

Error

20.

class Parent:

    x = 5

 

class Child(Parent):

    pass

 

Child.x += 10

print(Parent.x)

a)

5

b)

10

c)

15

d)

Error

21.

What will be the output?

f = open("data.txt", "w")

f.write("Hello")

f.close()

f = open("data.txt", "r")

print(f.read())

a)

Hello

b)

Nothing

c)

Error

d)

data.txt

22.

What will be the output?
with open("test.txt", "w") as f:

    f.write("Python")

print(f.closed)

a)

True

b)

False

c)

Error

d)

None

23.

f = open("info.txt", "w")

f.write("Data")

f.seek(0)

print(f.read()) What will be the output?

a)

Data

b)

""

c)

Error

d)

None

24.

with open("file.txt", "a") as f:

    f.write("End")

 

print(f.mode)

a)

w

b)

a

c)

r

d)

rb

25.

f = open("log.txt", "w+")

f.write("123")

f.seek(0)

print(f.read())

a)

""

b)

123

c)

Error

d)

None

26.

f = open("data.txt", "w")

print(f.tell())

a)

0

b)

1

c)

None

d)

Error

27.

f = open("file.txt", "w")

print(f.name)

a)

file.txt

b)

file

c)

Error

d)

None

28.

f = open("data.txt", "w")

f.write("Python\nRocks")

f.close()

f = open("data.txt")

print(len(f.readlines()))

a)

1

b)

2

c)

3

d)

Error

29.

f = open("data.txt", "w")

f.write("Hi")

f.truncate(1)

f.close()

f = open("data.txt")

print(f.read())

a)

H

b)

Hi

c)

i

30.

with open("test.txt", "w") as f:

    f.write("Hello\nWorld")

    f.seek(0)

a)

File pointer moves to end

b)

Moves to start

c)

Error

d)

None

31.

f = open("sample.txt", "w")

print(f.readable())

a)

True

b)

False

c)

Error

d)

None

32.

with open("nums.txt", "w") as f:

    f.writelines(["1\n", "2\n", "3\n"])

f = open("nums.txt")

print(f.readlines())

a)

['1\n', '2\n', '3\n']

b)

['123']

c)

Error

d)

None

33.

try:

    f = open("missing.txt", "r")

except FileNotFoundError:

    print("File not found")

a)

Error

b)

File not found

c)

None

d)

Empty output

34.

with open("log.txt", "w") as f:

    f.write("Test")

print(f.closed)

a)

True

b)

False

c)

Error

d)

None

35.

f = open("data.txt", "wb")

print(f.mode)

a)

w

b)

wb

c)

rb

d)

Error

36.

f = open("file.txt", "w+")

print(f.writable())

a)

True

b)

False

c)

Error

d)

None

37.

with open("file.txt", "w") as f:

    f.write("12345")

print(f.tell())

a)

5

b)

0

c)

1

d)

10

38.

What is the output of the following code? f = open("data.txt", "w") f.write("Hello") f.close() print(f.write("World"))

a)

0

b)

5

c)

Error

d)

None

39.

What is the output of the following code? f = open("data.txt", "w") f.write("Hello") f.flush() print("Done")

a)

Hello

b)

Done

c)

None

d)

Error

40.

What is the output of the following code? import os print(os.path.exists("test.txt"))

a)

True

b)

False

c)

Depends on file existence

d)

Error