Assignment 2: Python Variables
Part A: Conceptual (Q1–Q8)
1. What is a variable in Python?
2. How are variables created in Python?
3. Do variables need explicit declaration of data type in Python? Why?
4. Can multiple variables be assigned in one line? Give an example.
5. Explain variable re-assignment with an example.
6. What is the difference between global and local variables?
7. Can a variable name start with _? Explain.
8. What is dynamic typing in Python?
Part B: MCQs (Q9–Q16)
9. Which statement assigns values to multiple variables?
a) a, b = 10, 20
b) a = b = 10
c) Both a and b
d) None
10. What is the output?
x=y=z=5
print(x, y, z)
a) 5 5 5
b) 5 0 0
c) Error
d) None
11. Python variables are:
a) Statically typed
b) Dynamically typed
c) Strongly typed
d) Untyped
12. Which is valid?
a) 1value = 10
b) value1 = 10
c) value-1 = 10
d) @value = 10
13. Global variables can be accessed:
a) Only inside functions
b) Outside functions
c) Both inside and outside (with rules)
d) None
14. Which keyword is used to modify global variables inside a function?
a) static
b) global
c) extern
d) var
15. What will be the output?
x = 10
def fun():
x = 20
print(x)
fun()
print(x)
a) 20, 10
b) 10, 20
c) 20, 20
d) Error
16. Which is true for variables in Python?
a) Must be declared before use
b) No declaration required
c) Must specify type
d) Must be constants
Part C: Brainstorming (Q17–Q25)
17. Assign values to 3 variables in one line.
18. Swap two variables without using a temporary variable.
19. Predict output:
a = 10
b=a
a = 20
print(a, b)
20. Can two variables point to the same memory location? Explain.
21. Write a program to store and print your name, age, and city using variables.
22. What happens if you assign a string to a variable that earlier held an integer?
23. How are variables destroyed in Python?
24. Why is id() function used for variables?
25. Differentiate between mutable and immutable variables with examples.