Exercise: Tricky Questions

Questions for: Tricky Questions

What will be the output of the following Python code?
string1 = "Python"
string2 = "Python"
result = string1 is string2
print(result)
A:
True
B:
False
C:
This code will result in an error.
D:
Undefined
Answer: A
String interning in Python makes identical string literals refer to the same memory location.
What is the output of the following Python code?
x = 5
y = 2
result = x / y
print(result)
A:
2.5
B:
2
C:
2.0
D:
This code will result in an error.
Answer: A
Division (/) in Python 3 returns a floating-point result.
What is the output of the following Python code?
def some_function(x, y, z):
    return x + y ** z

result = some_function(x=1, y=2, z=3)
print(result)
A:
9
B:
7
C:
8
D:
1
Answer: A
The exponent has higher precedence than addition, so the result is 1 + (2 ** 3) = 9.
What is the purpose of the is keyword in Python?
A:
It is used to compare values for equality.
B:
It is used to compare object identity.
C:
It is used to create instances of a class.
D:
It is used to define conditional statements.
Answer: B
The is keyword checks if two objects refer to the same memory location.
What will be the output of the following Python code?
x = 0.1
y = 0.2
result = x + y
print(result)
A:
0.3
B:
0.30000000000000004
C:
0.2
D:
This code will result in an error.
Answer: B
Floating-point arithmetic may result in small precision errors, and the actual result is approximately 0.30000000000000004.
Ad Slot (Above Pagination)
Quiz