Python Programming - Tricky Questions

Exercise : Tricky Questions - General Questions
  • Tricky Questions - General Questions
16.
What will be the output of the following Python code?
def outer_func(x):
    def inner_func():
        return x + 1
    return inner_func

closure1 = outer_func(5)
closure2 = outer_func(10)

print(closure1() + closure2())
11
16
17
6
Answer: Option
Explanation:
Each closure captures the value of x from its own outer function call, resulting in 6 + 11 = 17.

17.
What is the output of the following Python code?
result = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1
print(result)
0.5000000000000001
0.6
0.4
0.0
Answer: Option
Explanation:
Floating-point arithmetic may result in small precision errors, and the actual result is approximately 0.5.

18.
Consider the following Python code:
class CustomClass:
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        return self.value == other.value

obj1 = CustomClass(10)
obj2 = CustomClass(10)
result = obj1 == obj2
print(result)
What will be the value of result?
True
False
This code will result in an error.
This code will result in an infinite loop.
Answer: Option
Explanation:
The custom __eq__ method is defined to compare the value attribute of the objects.

19.
What is the output of the following Python code?
a = [1, 2, 3]
b = a[:]
a[0] = 10
print(b)
[1, 2, 3]
[10, 2, 3]
This code will result in an error.
[10, 2, 3] (but with a warning)
Answer: Option
Explanation:
Slicing creates a new copy of the list, so modifying a does not affect b.

20.
What will be the output of the following Python code?
x = 0.1
y = 0.2
result = x + y
print(result)
0.3
0.30000000000000004
0.2
This code will result in an error.
Answer: Option
Explanation:
Floating-point arithmetic may result in small precision errors, and the actual result is approximately 0.30000000000000004.