Python Questions and Answers
1. Which of the following is not a valid Python data type?
Answer: Any type not built into Python (like token , character , pointer ) is invalid. Python built-in
data types include int , float , str , list , tuple , dict , set , bool , etc.
2. Is token a valid Python data type?
Answer: No. token is not a built-in Python data type. It is a term used in parsing or certain libraries but
not a core data type.
3. What does the single-slash operator / return in Python 3 when dividing integers?
Answer: / performs true division and always returns a float.
Example:
7 / 2 # 3.5
4 / 2 # 2.0
- For floor (integer) division, use // .
4. What does the else block in a while loop execute?
Answer: The else block executes only if the loop ends normally (condition becomes False). It is skipped
if the loop exits via break .
Example:
i = 1
while i <= 3:
print(i)
i += 1
else:
print("Loop ended normally")
Output:
1
1
2
3
Loop ended normally
5. Which gate is also known as the inverter gate?
Answer: The NOT gate (inverts input: 0 → 1, 1 → 0).
6. Python program: If False, print yes, else print no.
Answer:
if False:
print("yes")
else:
print("no")
Output:
no
7. Which method removes the first matching value from a list in Python?
Answer: [Link](value) removes the first occurrence of the value.
Example:
numbers = [10, 20, 30, 20]
[Link](20)
print(numbers) # [10, 30, 20]
8. Give the output: nested loops with range(2)
Code:
2
for i in range(2):
for j in range(2):
print(i, j)
Output:
0 0
0 1
1 0
1 1
9. Is defined a Python keyword?
Answer: No. defined is not a keyword. Keywords include if , else , for , while , def , class ,
is , etc.
10. strip() function in Python
Answer: Removes leading and trailing whitespace (or specified characters) from a string.
Example:
text = " hello "
print([Link]()) # "hello"
11. Does randint require importing a module?
Answer: Yes. It comes from the random module.
Example:
import random
print([Link](1, 10))
12. Does the square root function require importing a module?
3
Answer: Yes. sqrt() is in the math module.
Example:
import math
print([Link](16)) # 4.0
- Alternative without import: 16 ** 0.5
13. Is eval a Python keyword?
Answer: No. eval is a built-in function, not a keyword.
14. Are assert and nonlocal Python keywords?
Answer: Yes. Both are keywords. - assert → used for debugging (raises AssertionError if condition
is False) - nonlocal → used in nested functions to modify outer (but not global) variables
15. Break statement explanation
Assertion: True → break exits a loop prematurely.
Reason: Incorrect → skipping iteration is continue , not break .
Example:
for i in range(5):
if i == 3:
break
print(i)
# Output: 0 1 2
16. Assertion: Strings are immutable. Reason: You cannot change individual characters.
Answer: Both are True, and the reason correctly explains the assertion.
Example:
4
s = "hello"
s[0] = "H" # TypeError
# Correct way: s = "H" + s[1:] # 'Hello'