Python Variables Quiz
- Last Updated On
- ByScientech Easy
- InPython
- Read Time10 mins
1. What is a variable in Python?
A reserved keyword in Python
A reserved memory locations to store data values
A symbolic name given to a physical location in the computer’s memory
Both (b) and (c)
d
A variable acts as a container that holds data value during the execution of a program.
2. How do we declare a variable in Python?
var x
x = variable
declare x
x = 10
d
3. Which of the following is a valid variable name in Python?
while
raise%
student@
_0_0_9
d
Variable names in Python must start with a letter or underscore. They cannot start with a number, and cannot contain hyphens or spaces.
4. What will be the output of the following code?
x = 10 y = x x = 20 print(y)
10
20
30
Error
a
When y = x is executed, y gets the value of x, which is 10. Changing x later does not affect the value of y.
5. Which of the following is not a valid data type for a Python variable?
int
float
array
string
c
Python has built-in data types like int, float, and string, but array is not a built-in data type.
6. What is the scope of a variable defined inside a function?
Global scope
Local scope
External scope
Both (a) and (b)
b
A variable defined inside a function has local scope, meaning that it can only be accessed within that function.
7. What will be the output of the following code?
a = 5 b = "10" c = 15 print(a + b + c)
20
30
20 + “10”
TypeError
d
Python does not allow operations between different data types, such as int and str. This will raise a TypeError.
8. Which keyword is used to delete a variable in Python?
remove
del
delete
erase
b
We use the del keyword to delete a variable in Python, freeing up memory.
9. What is the output of the following code?
x = 5 y = 2 x, y = y, x print(x, y)
5 2
2 5
2 2
5 5
b
The line x, y = y, x swaps the values of x and y. After the swap, x becomes 2 and y becomes 5.
10. Which of the following is true about Python variables?
Variables must be declared with a specific data type.
Variables cannot be reassigned.
Variables can change their data type during the execution of program.
Variables are case-insensitive.
c
Python is dynamically typed language, which means that a variable can hold values of different data types during its lifetime.
11. What is the output of the following code?
x = "Hello" y = x x = "World" print(y)
Hello
World
Hello World
None of these
a
When y = x is executed, y gets the value of x, which is “Hello”. Changing x later does not affect the value of y.
12. What is the output of the following code?
a = [10, 20, 30] b = a a.append(40) print(b)
[10, 20, 30]
[10, 20, 30, 40]
[40]
[10, 20, 40]
b
When b = a is executed, b will refer to the same list object as a. Modifying the list through a will also reflect in b because they point to the same object in memory.
13. What is the output of the following code?
x = 5 def func(): x = 10 print(x) func() print(x)
10 10
5 5
10 5
5 10
c
Inside the function func(), x is a local variable with a value of 10. Outside the function, x refers to the global variable with a value of 5.
14. What is the purpose of the global keyword in Python?
To create a new global variable.
To delete a global variable.
To define a local variable.
To modify a global variable inside a function.
d
The global keyword is used to modify a global variable inside a function. Without it, Python will create a new local variable instead.
15. What is the output of the following code?
x = 10 def func(): print(x) x = 20 func()
10
20
Error
None
c
The above code will raise an UnboundLocalError because x is referenced before assignment inside the function. Python treats x as a local variable due to the assignment x = 20, but it hasn’t been assigned yet when print(x) is called.
16. What is the difference between mutable and immutable variables in Python?
Immutable variables can be changed after creation, while mutable variables cannot.
Mutable variables can be changed after creation, while immutable variables cannot.
Both mutable and immutable variables can be changed after creation.
Neither mutable nor immutable variables can be changed after creation.
b
Mutable objects, such as lists, dictionaries can be modified after creation. While immutable objects, such as integers, strings, tuples cannot be modified after creation.
17. What is the output of the below code?
x = 10 def func(): global x x = 20 print(x) print(x) func() print(x)
10 20 10
10 20 20
20 20 20
10 10 10
b
The first print(x) outputs 10. Inside the func() function, global x modifies the global x to 20, and print(x) outputs 20. After calling the function func(), the global x remains 20, so the final print(x) outputs 20.
18. What is the output of the below code?
x = [10, 20, 30] y = x[:] x.append(40) print(y)
[10, 20, 30]
[10, 20, 30, 40]
[40]
TypeError
a
The line y = x[:] creates a shallow copy of the list x. Modifying x by appending 4 does not affect y because y is a separate copy.
19. What is the role of the nonlocal keyword in Python?
To create a global variable.
To modify the value of a variable in the nearest enclosing scope (excluding global scope).
To define a new local variable.
To delete a local variable.
b
The nonlocal keyword is used to modify the value of a variable in the nearest enclosing scope in a nested function. It works differently from global keyword, which modifies the value of a variable in the global scope.
20. What will be the result when this code is executed?
x = 10 def func(): x = 20 def inner(): nonlocal x x = 30 inner() print(x) func() print(x)
20 10
10 20
30 10
20 20
c
Inside the func() function, x is a local variable with a value of 20. The nonlocal x in the inner() function modifies the value of x in the enclosing func() scope to 30. Therefore, the line print(x) inside func() outputs 30. The global variable x remains 10, so the final print(x) outputs 10.
21. Where does Python store the actual object when a variable is created?
Stack memory
Heap memory
Register memory
Both a and b
b
Python stores the actual object in heap memory, while the variable name (reference) is stored in stack memory.
22. Where does Python store the local variable when you create it?
Stack memory
Heap memory
Both stack and heap
Neither stack nor heap
a
A local variable that is defined inside a function is stored in the stack memory. However, if a local variable references an object, the object itself is stored in the heap memory.
23. What does Python use to track how many variables reference an object?
Garbage collector
Pointer arithmetic
Memory stack
Reference counting
d
Python uses a system called reference counting to track how many variables reference an object.
24. What happens when an object in Python is no longer referenced by any variable?
It remains in memory permanently.
It is moved to stack memory.
It is cleared automatically by the garbage collector.
It remains in memory until the program ends.
c
Python uses a garbage collector that automatically removes objects when they are no longer referenced to free up memory and prevent memory leaks.
25. Where is a variable name stored in Python memory?
Heap memory
Stack memory
Cache memory
None
b
The variable name (reference) is stored in stack memory.
26. Which memory area is responsible for storing dynamically allocated objects in Python?
Stack
Heap
Register
CPU cache
b
In Python, dynamically allocated objects such as variables and data structures are stored in heap memory.
27. What happens when multiple variables refer to the same object in Python?
Each variable gets a separate copy of the object.
Python duplicates the object in memory.
Python increases the reference count of the object.
Python moves the object to stack memory
c
Python keeps track of how many variables reference an object using reference counting. The object remains in memory as long as there is at least one reference to it.
28. What will happen if the reference count of an object in Python drops to zero?
The object will remain in memory.
The object will be moved to stack memory.
The object will be deleted by the garbage collector.
The object will be stored in a cache for later use.
c
When no variable references an object, Python’s garbage collector removes it from memory to free up space.
29. What will be the output when the below code will execute?
def func(): x = 10 return x y = func() y = y + 5 print(y)
10
15
Error
None of these
b
The integer object 10 is stored in the heap memory and referenced by the local variable x in the stack. When the value of variable x is returned, the value 10 is assigned to y. Adding 5 to y creates a new integer object 15 in the heap, which is then assigned to y.
30. What will be the output of the below code when executed?
a = 10 b = 10 print(id(a) == id(b))
True
False
true
Error
a
The id() function returns the memory address of an object. We can use it to verify if two variables point to the same object. In the above code, both a and b point to the same memory location for the integer 10, so id(a) == id(b) is True.
31. What is the expected output of this code?
x = 10 y = x x = x + 5 print(y is x)
True
False
Error
None
b
The line y = x assigns the value of x (which is 10) to y. Now y is equal to 10. The line x = x + 5 creates a new object for x with the value 15. The variables y and x now point to different objects, so y is x is False.
32. Can you predict the output of this code?
x = 10 def func(): x = 20 def inner(): global x x = 30 inner() print(x) func() print(x)
20 30
30 30
20 20
30 20
a
Inside the func() function, x is a local variable with a value of 20. The global x in inner() function modifies the global x to 30. The line print(x) inside func() function outputs 20. The final line print(x) outputs 30.
33. What happens when this code runs?
x = 10 def func(): global x x = 20 def inner(): nonlocal x x = 30 inner() func() print(x)
10
20
30
Error
d
The nonlocal variable x in the inner() function raises a SyntaxError because x is declared as global in the outer function. The keyword nonlocal can only refer to variables in the nearest enclosing scope (excluding global), but x is global here.
34. What will be the expected output of this code?
a = [1, 2, 3] b = a a.append(4) print(b is a)
True
False
Error
None
a
The line b = a makes b point to the same list as a. Modifying a by appending 4 also affects b because they reference the same object. Therefore, the output is True.
35. What result will this code give upon execution?
a = [1, 2, 3] b = [1, 2, 3] print(a is b)
True
False
Error
None
b
Since lists are mutable objects, Python creates separate objects in memory for the variables a and b. Even though their contents are the same, still a and b are different objects. Therefore, the statement a is b results False.