LearnFrenzy
Sign in Take a free test

Tricky Python Output Questions · Exercise 3 of 4

Tricky Python Output Questions Tricky Output Questions – Functions, OOP and Advanced

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Fifteen harder predict-the-output questions on functions, closures, generators, classes, MRO and exceptions. Every program was run on Python 3, so the answers are the real output.

Question 1 Medium

What is the output of the following Python program?

main.py
def f(x, items=[]):
    items.append(x)
    return len(items)

print(f(1), f(2), f(3, []), f(4))
A 1 2 1 3
B 1 1 1 1
C 1 2 3 4
D 1 2 1 4
Question page & discussion
Question 2 Hard

What is the output of the following Python program?

main.py
funcs = [lambda x: x * i for i in range(3)]
print([f(10) for f in funcs])
A [0, 10, 20]
B [0, 0, 0]
C [20, 20, 20]
D [30, 30, 30]
Question page & discussion
Question 3 Hard

What is the output of the following Python program?

main.py
g = (i for i in range(5))
print(2 in g, list(g), 2 in g)
A True [0, 1, 2, 3, 4] True
B True [] False
C True [3, 4] False
D True [2, 3, 4] False
Question page & discussion
Question 4 Hard

What is the output of the following Python program?

main.py
class A:
    items = []

a, b = A(), A()
a.items.append(1)
b.items = [2]
print(A.items, a.items, b.items)
A [] [1] [2]
B [1, 2] [1, 2] [1, 2]
C [1] [1] [2]
D [2] [1] [2]
Question page & discussion
Question 5 Hard

What is the output of the following Python program?

main.py
t = ([1],)
try:
    t[0] += [2]
except TypeError:
    print("error", t)
A error ([1],)
B error ([1, 2],)
C ([1, 2],)
D error
Question page & discussion
Question 6 Medium

What is the output of the following Python program?

main.py
def f():
    try:
        print("a", end=" ")
        return "b"
    finally:
        print("c", end=" ")

print(f())
A a b c
B a c b
C a b
D c a b
Question page & discussion
Question 7 Medium

What is the output of the following Python program?

main.py
x = 1
def g():
    print(x, end=" ")
def h():
    x = 2
    print(x, end=" ")
g()
h()
print(x)
A 1 2 1
B 1 2 2
C 2 2 1
D 1 1 1
Question page & discussion
Question 8 Hard

What is the output of the following Python program?

main.py
class A:
    def who(self): return "A"
class B(A):
    def who(self): return "B" + super().who()
class C(A):
    def who(self): return "C" + super().who()
class D(C, B):
    pass

print(D().who(), [k.__name__ for k in D.__mro__][1:3])
A CBA ['C', 'B']
B CA ['C', 'B']
C BCA ['B', 'C']
D CBA ['B', 'C']
Question page & discussion
Question 9 Hard

What is the output of the following Python program?

main.py
class P:
    def __eq__(self, other):
        return True

print(P() == 5, P() != 5)
try:
    {P()}
except TypeError:
    print("unhashable")
A False True unhashable
B True False unhashable
C True False
D True True unhashable
Question page & discussion
Question 10 Medium

What is the output of the following Python program?

main.py
import math
print(int(-3.5), -3.5 // 1, round(-3.5), math.floor(-3.5), math.ceil(-3.5))
A -3 -3.0 -3 -4 -3
B -3 -4.0 -4 -4 -3
C -4 -4.0 -4 -4 -3
D -3 -4 -3 -4 -3
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links