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?
a) 1_variable
b) variable_name
c) variable-name
d) None of the above
Answer:
b) variable_name
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?
a) variable = value
b) variable <- value
c) variable -> value
d) variable == value
Answer:
a) variable = value
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?
a) int x = 10
b) x int = 10
c) x = 10
d) 10 = x
Answer:
c) x = 10
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?
a) int
b) float
c) str
d) bool
Answer:
b) float
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)
a) 10, 20
b) 20
c) 10
d) Error
Answer:
c) 10
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?
a) for
b) True
c) import
d) begin
Answer:
d) begin
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?
a) It is automatically declared
b) A syntax error occurs
c) A runtime error occurs
d) None of the above
Answer:
c) A runtime error occurs
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?
a) check(type)
b) type()
c) typeof()
d) checktype()
Answer:
b) type()
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?
a) global x = 5
b) x = 5
c) def x: global 5
d) global x; x = 5
Answer:
d) global x; x = 5
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)
a) PythonPythonPython
b) Python3
c) Error
d) None of the above
Answer:
a) PythonPythonPython
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)
a) HelloWorld
b) Hello World
c) Error
d) x + y
Answer:
a) HelloWorld
Explanation:
The + operator concatenates strings without adding any spaces.
12. In Python, which of the following is a mutable data type?
a) string
b) tuple
c) list
d) integer
Answer:
c) list
Explanation:
Lists are mutable in Python, meaning their elements can be changed.
13. What is the correct way to delete a variable in Python?
a) delete(x)
b) del x
c) remove(x)
d) erase(x)
Answer:
b) del x
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?
a) int
b) str
c) float
d) NoneType
Answer:
d) NoneType
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)
a) 2
b) 2.0
c) 4
d) 1
Answer:
a) 2
Explanation:
The // operator in Python performs integer (floor) division.
Comments
Post a Comment