Showing 21–30 of 119 questions
What is the output of: print(None == False)?
A
True
B
False
C
None
D
Error
EXPLANATION
None and False are distinct objects. None == False evaluates to False. None is a unique singleton object, not equivalent to False or 0.
Consider the code: x = (1, 2, 3); x[0] = 5. What happens?
A
x becomes (5, 2, 3)
B
TypeError is raised
C
x remains (1, 2, 3)
D
IndexError is raised
Correct Answer:
B. TypeError is raised
EXPLANATION
Tuples are immutable. Attempting to modify a tuple element with x[0] = 5 raises a TypeError: 'tuple' object does not support item assignment.
What will be the output of: x = 5; x += 3; print(x)?
EXPLANATION
x += 3 is equivalent to x = x + 3. Starting with x=5, after adding 3, x becomes 8.
Consider: d = {'a': 1, 'b': 2}; d['c'] = 3. What is d?
A
{'a': 1, 'b': 2}
B
{'a': 1, 'b': 2, 'c': 3}
C
Error
D
None
Correct Answer:
B. {'a': 1, 'b': 2, 'c': 3}
EXPLANATION
Dictionaries are mutable. The assignment d['c'] = 3 adds a new key-value pair to the dictionary. d becomes {'a': 1, 'b': 2, 'c': 3}.
What is the output of: print('a' in 'banana')?
A
True
B
False
C
1
D
Error
EXPLANATION
The 'in' operator checks if 'a' is a substring of 'banana'. It is present (multiple times), so it returns True.
Consider: x = [1, 2, 3, 4, 5]; y = x[1:4]. What is y?
A
[1, 2, 3]
B
[2, 3, 4]
C
[2, 3, 4, 5]
D
[1, 2, 3, 4]
Correct Answer:
B. [2, 3, 4]
EXPLANATION
List slicing x[1:4] includes indices 1, 2, 3 (but excludes 4). Elements are 2, 3, 4 from the list [1, 2, 3, 4, 5].
What will be the output of: print(10 if 5 > 3 else 20)?
EXPLANATION
This is a ternary conditional expression. Since 5 > 3 is True, the expression evaluates to 10 (the value before 'if').
Consider the code: def func(x, y=5): return x + y. What is func(3)?
EXPLANATION
y has a default value of 5. When func(3) is called, x=3 and y=5 (default). The function returns 3 + 5 = 8.
Consider: a = 'Hello'; b = 'Hello'. Are a and b the same object?
A
Always True
B
Always False
C
Depends on interpreter implementation
D
Cannot be determined
Correct Answer:
C. Depends on interpreter implementation
EXPLANATION
Due to string interning in Python, small strings may be cached and refer to the same object. However, this is an implementation detail. a == b is True, but a is b may vary.
What will be printed: print(3 ** 2 ** 2)?
EXPLANATION
Exponentiation is right-associative. 3 2 2 = 3 (2 2) = 3 ** 4 = 81.