In Python, variables are used to store data values. They are essentially labels that you can assign to data, and once a variable is set, you can refer to it by its name to access the value it represents. Here we present 15 multiple-choice questions to test your knowledge of Python variables. Each MCQ has the correct answer with an explanation.
1. Which of the following is a valid variable name in Python?
Answer:
Explanation:
In Python, variable names can start with a letter or an underscore, but not with a number. They can contain alphanumeric characters and underscores. "variable_name" is the only valid option.
2. How do you assign a value to a variable in Python?
Answer:
Explanation:
In Python, the equal sign (=) is used to assign values to variables.
3. Which of the following is the correct way to declare a variable in Python?
Answer:
Explanation:
Python uses dynamic typing, so you don't need to declare the variable type. The correct syntax is just variable_name = value.
4. What will be the data type of the variable 'x' after the assignment x = 5.0?
Answer:
Explanation:
In Python, a number with a decimal point is considered a float, so x will be of type float.
5. What is the output of the following code?
x, y = 10, 20
print(x)
Answer:
Explanation:
The code uses multiple assignment where x is assigned 10 and y is assigned 20. The print statement only prints the value of x.
6. Which of the following is not a reserved keyword in Python?
Answer:
Explanation:
"begin" is not a reserved keyword in Python, while the others are.
7. What happens when you try to use an undeclared variable in Python?
Answer:
Explanation:
Using an undeclared variable in Python results in a NameError at runtime.
8. How do you check the type of a variable in Python?
Answer:
Explanation:
The type() function is used to check the data type of a variable in Python.
9. What is the correct way to declare a global variable in Python?
Answer:
Explanation:
To declare a global variable inside a function, you use the global keyword followed by the variable name.
10. What will be the output of the following code?
x = "Python"
print(x*3)
Answer:
Explanation:
In Python, multiplying a string by an integer n repeats the string n times.
11. What is the result of this Python code?
x = "Hello"
y = "World"
z = x + y
print(z)
Answer:
Explanation:
The + operator concatenates strings without adding any spaces.
12. In Python, which of the following is a mutable data type?
Answer:
Explanation:
Lists are mutable in Python, meaning their elements can be changed.
13. What is the correct way to delete a variable in Python?
Answer:
Explanation:
The del statement is used to delete objects in Python.
14. What is the data type of a variable set to None in Python?
Answer:
Explanation:
None is a special type in Python represented by NoneType.
15. What is the output of the following code?
x = 8
y = 4
print(x // y)
Answer:
Explanation:
The // operator in Python performs integer (floor) division.