Showing 11–20 of 25 questions
Consider: *a, b = [1, 2, 3]. What is a?
A
[1, 2]
B
1
C
[1, 2, 3]
D
Error
Correct Answer:
A. [1, 2]
EXPLANATION
The * operator in unpacking collects remaining elements. a gets [1, 2] and b gets 3.
What will be the result of: set([1, 2, 2, 3, 3, 3])?
A
{1, 2, 2, 3, 3, 3}
B
{1, 2, 3}
C
[1, 2, 3]
D
Error
Correct Answer:
B. {1, 2, 3}
EXPLANATION
Sets remove duplicate elements. set([1, 2, 2, 3, 3, 3]) returns {1, 2, 3}.
Consider: f = lambda x: x**2. What is f(5)?
EXPLANATION
Lambda creates an anonymous function. f = lambda x: x2 creates a function that returns x squared. f(5) = 52 = 25.
What is the output of: print(*[1, 2, 3])?
A
[1, 2, 3]
B
1 2 3
C
123
D
Error
EXPLANATION
The * operator unpacks the list. print(*[1, 2, 3]) becomes print(1, 2, 3) which prints '1 2 3' with spaces.
Consider: try: int('abc'); except: print('error'). What will be printed?
A
Nothing
B
'error'
C
error
D
ValueError
EXPLANATION
int('abc') raises a ValueError. The except block catches it and prints 'error' (without quotes).
What will be the output of: print(any([False, False, True]))
A
True
B
False
C
Error
D
None
EXPLANATION
any() returns True if at least one element in the iterable is True.
Consider: x = [i**2 for i in range(4)]. What is x?
A
[0, 1, 2, 3]
B
[1, 4, 9, 16]
C
[0, 1, 4, 9]
D
[2, 4, 6, 8]
Correct Answer:
C. [0, 1, 4, 9]
EXPLANATION
List comprehension: i**2 for i in [0,1,2,3] produces [0, 1, 4, 9].
What will be the output of: d = {'a': 1, 'b': 2}; print(list(d.items()))
A
[('a', 1), ('b', 2)]
B
['a', 'b']
C
[1, 2]
D
Error
Correct Answer:
A. [('a', 1), ('b', 2)]
EXPLANATION
dict.items() returns key-value pairs as tuples, converted to a list: [('a', 1), ('b', 2)].
Which of the following will raise a TypeError in Python?
A
int('10')
B
int(10.5)
C
int('10.5')
D
int(True)
Correct Answer:
C. int('10.5')
EXPLANATION
int('10.5') raises TypeError because '10.5' is a string representation of a float, not a valid integer string.
What will be the output of: print({1, 2, 3} | {3, 4, 5})?
A
{1, 2, 3, 3, 4, 5}
B
{1, 2, 3, 4, 5}
C
{3}
D
{1, 2, 4, 5}
Correct Answer:
B. {1, 2, 3, 4, 5}
EXPLANATION
The | operator performs set union, combining all unique elements from both sets. Result is {1, 2, 3, 4, 5}.