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())
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)
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
?
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)
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)
Answer: Option
Explanation:
Floating-point arithmetic may result in small precision errors, and the actual result is approximately 0.30000000000000004.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers