Showing 51–60 of 100 questions
in Basics & Syntax
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: x = 10; y = 20; x, y = y, x; print(x, y)
A
10 20
B
20 10
C
Error
D
20 20
EXPLANATION
Python tuple unpacking allows simultaneous assignment, effectively swapping x and y.
Consider: s = 'Python'; print(s[2:5]). What will be the output?
EXPLANATION
String slicing s[2:5] includes indices 2, 3, 4. 'Python'[2:5] = 'tho'.
What is the output of: for i in range(3): print(i, end=' ')
A
1 2 3
B
0 1 2
C
0 1 2 3
D
3 2 1
EXPLANATION
range(3) generates 0, 1, 2. The end=' ' parameter prevents newline after each print.
Which operator in Python performs integer division?
EXPLANATION
The // operator performs floor division, returning the integer quotient.
What will be the output of: print(bool(0), bool(''), bool([]))
A
True True True
B
False False False
C
True False True
D
Error
Correct Answer:
B. False False False
EXPLANATION
0, empty string '', and empty list [] are all falsy values in Python, so bool() returns False for each.
Consider the code: x = [10, 20, 30]; y = x; y[0] = 99; print(x[0]). What is the output?
EXPLANATION
y = x creates a reference to the same list, not a copy. Modifying y also modifies x.
What will be the output of: lst = [1, 2, 3]; lst.extend([4, 5]); print(lst)
A
[1, 2, 3, [4, 5]]
B
[1, 2, 3, 4, 5]
C
[4, 5, 1, 2, 3]
D
Error
Correct Answer:
B. [1, 2, 3, 4, 5]
EXPLANATION
extend() adds each element of the iterable to the list, resulting in [1, 2, 3, 4, 5].
What is the output of: x = '25'; y = int(x); print(y + 5)
A
'255'
B
30
C
Error
D
'30'
EXPLANATION
int('25') converts the string to integer 25, then 25 + 5 = 30.
What will be the output of: print(type(3.14))
EXPLANATION
3.14 is a floating-point number, so type() returns <class 'float'>.