pythonmcqs
Python Variables MCQs – Practice Quiz for Beginners & Experts

Python Variables MCQs - 25 Multiple Choice Questions

1. What is a variable in Python?
A) A reserved keyword
B) A way to store data
C) A type of function
D) None of the above
Answer: B
2. How do you declare a variable in Python?
A) var x = 5
B) x := 5
C) x = 5
D) declare x = 5
Answer: C
3. Which of the following is a valid variable name in Python?
A) 2nd_variable
B) my-variable
C) my_variable
D) my variable
Answer: C
4. What will be the output of the following code: `x = 10; print(x)`?
A) 10
B) x
C) Error
D) None
Answer: A
5. What is the scope of a variable?
A) The lifetime of the variable
B) The visibility of the variable
C) The type of data the variable can hold
D) None of the above
Answer: B
6. Which of the following is an example of a global variable?
A) `x = 5` inside a function
B) `global x` inside a function
C) `x = 5` outside any function
D) None of the above
Answer: C
7. How do you delete a variable in Python?
A) delete x
B) remove x
C) del x
D) None of the above
Answer: C
8. What is the type of the variable after this assignment: `x = "Hello"`?
A) int
B) str
C) list
D) None of the above
Answer: B
9. Which keyword is used to declare a constant in Python?
A) const
B) final
C) constant
D) None, Python does not have constants
Answer: D
10. Which of the following can be used as a variable name?
A) _variable
B) 1variable
C) variable-1
D) None of the above
Answer: A
11. What will be the output of the following code: `x = [1, 2, 3]; print(x[1])`?
A) 1
B) 2
C) 3
D) Error
Answer: B
12. In Python, variables are:
A) Strongly typed
B) Weakly typed
C) Dynamically typed
D) Both A and C
Answer: D
13. What is the output of `x = 10; y = x; print(y)`?
A) 10
B) x
C) Error
D) None
Answer: A
14. Which of the following statements is true about variable assignment?
A) It creates a reference to the object
B) It copies the value of the object
C) Both A and B
D) None of the above
Answer: A
15. How can you check the type of a variable in Python?
A) type(x)
B) var(x)
C) checktype(x)
D) None of the above
Answer: A
16. What is the output of `x = [1, 2, 3]; x.append(4); print(x)`?
A) [1, 2, 3]
B) [1, 2, 3, 4]
C) Error
D) None of the above
Answer: B
17. What will happen if you try to access an undefined variable?
A) It will return None
B) It will raise a NameError
C) It will create a new variable
D) None of the above
Answer: B
18. What is a local variable?
A) A variable declared outside a function
B) A variable declared inside a function
C) A variable that can be accessed globally
D) None of the above
Answer: B
19. Which statement about variable naming is correct?
A) Variable names can contain spaces
B) Variable names must start with a letter or underscore
C) Variable names are case insensitive
D) None of the above
Answer: B
20. Which of the following can be a valid name for a variable in Python?
A) myVar
B) MyVar
C) my_var
D) All of the above
Answer: D
21. How do you create a multi-line string variable in Python?
A) Using triple quotes
B) Using double quotes
C) Using single quotes
D) None of the above
Answer: A
22. What will be the output of `x = 5; y = 2 * x; print(y)`?
A) 5
B) 10
C) 7
D) None of the above
Answer: B
23. Which of the following is used to convert a variable to a string in Python?
A) str(x)
B) toString(x)
C) string(x)
D) None of the above
Answer: A
24. What is the purpose of the `global` keyword?
A) To declare a global variable inside a function
B) To create a constant variable
C) To define a variable in a class
D) None of the above
Answer: A
25. How do you swap the values of two variables `a` and `b` in Python?
A) a, b = b, a
B) swap(a, b)
C) a = b; b = a
D) None of the above
Answer: A

© 2024 Python Variables MCQs Quiz