What is a variable in Python?
A. A reserved keyword in Python.
B. A reserved memory locations to store data values.
C. A symbolic name given to a physical location in the computer’s memory.
D. Both (B) and (C)
Both (B) and (C)
A variable acts as a container that holds data value during the execution of a program.
How do we declare a variable in Python?
A. var x
B. x = variable
C. declare x
D. x = 10
x = 10
Which of the following is a valid variable name in Python?
A. while
B. raise%
C. student@
D. _0_0_9
_0_0_9
Variable names in Python must start with a letter or underscore. They cannot start with a number, and cannot contain hyphens or spaces.
What will be the output of the following code?
x = 10
y = x
x = 20
print(y)
10
When y = x is executed, y gets the value of x, which is 10. Changing x later does not affect the value of y.
Which of the following is not a valid data type for a Python variable?
A. int
B. float
C. array
D. string
array
Python has built-in data types like int, float, and string, but array is not a built-in data type.
What is the scope of a variable defined inside a function?
A. Global scope
B. Local scope
C. External scope
D. Both (A) and (B)
Local scope
A variable defined inside a function has local scope, meaning that it can only be accessed within that function.
What will be the output of the following code?
a = 5
b = "10"
c = 15
print(a + b + c)
A. 20
B. 30
C. 20 + “10”
D. TypeError
TypeError
Python does not allow operations between different data types, such as int and str. This will raise a TypeError.
Which keyword is used to delete a variable in Python?
A. remove
B. del
C. delete
D. erase
del
We use the del keyword to delete a variable in Python, freeing up memory.
What is the output of the following code?
x = 5
y = 2
x, y = y, x
print(x, y)
2 5
The line x, y = y, x swaps the values of x and y. After the swap, x becomes 2 and y becomes 5.
Which of the following is true about Python variables?
A. Variables must be declared with a specific data type.
B. Variables cannot be reassigned.
C. Variables can change their data type during the execution of program.
D. Variables are case-insensitive.
Variables can change their data type during the execution of program.
Python is dynamically typed language, which means that a variable can hold values of different data types during its lifetime.
What is the output of the following code?
x = "Hello"
y = x
x = "World"
print(y)
A. Hello
B. World
C. Hello World
D. None of these
Hello
When y = x is executed, y gets the value of x, which is “Hello”. Changing x later does not affect the value of y.
What is the expected output of the following code?
a = [10, 20, 30]
b = a
a.append(40)
print(b)
A. [10, 20, 30]
B. [10, 20, 30, 40]
C. [40]
D. [10, 20, 40]
[10, 20, 30, 40]
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.
What is the output of the following code?
x = 5
def func():
x = 10
print(x)
func()
print(x)A. 10 10
B. 5 5
C. 10 5
D. 5 10
10 5
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.
What is the purpose of the global keyword in Python?
A. To create a new global variable.
B. To delete a global variable.
C. To define a local variable.
D. To modify a global variable inside a function.
To modify a global variable inside a function.
The global keyword is used to modify a global variable inside a function. Without it, Python will create a new local variable instead.
What is the output of the following code?
x = 10
def func():
print(x)
x = 20
func()A. 10
B. 20
C. Error
D. None
Error
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.
What is the difference between mutable and immutable variables in Python?
A. Immutable variables can be changed after creation, while mutable variables cannot.
B. Mutable variables can be changed after creation, while immutable variables cannot.
C. Both mutable and immutable variables can be changed after creation.
D. Neither mutable nor immutable variables can be changed after creation.
Mutable variables can be changed after creation, while immutable variables cannot.
Mutable objects, such as lists, dictionaries can be modified after creation. While immutable objects, such as integers, strings, tuples cannot be modified after creation.
What is the output of the below code?
x = 10
def func():
global x
x = 20
print(x)
print(x)
func()
print(x)A. 10 20 10
B. 10 20 20
C. 20 20 20
D. 10 10 10
10 20 20
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.
What is the output of the code below?
x = [10, 20, 30]
y = x[:]
x.append(40)
print(y)
A. [10, 20, 30]
B. [10, 20, 30, 40]
C. [40]
D. TypeError
[10, 20, 30]
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.
What is the role of the nonlocal keyword in Python?
A. To create a global variable.
B. To modify the value of a variable in the nearest enclosing scope (excluding global scope).
C. To define a new local variable.
D. To delete a local variable.
To modify the value of a variable in the nearest enclosing scope (excluding global scope).
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.
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)A. 20 10
B. 10 20
C. 30 10
D. 20 20
30 10
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.
Where does Python store the actual object when a variable is created?
A. Stack memory
B. Heap memory
C. Register memory
D. Both A and B
Heap memory
Python stores the actual object in heap memory, while the variable name (reference) is stored in stack memory.
Where does Python store the local variable when you create it?
A. Stack memory
B. Heap memory
C. Both stack and heap
D. Neither stack nor heap
Stack memory
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.
What does Python use to track how many variables reference an object?
A. Garbage collector
B. Pointer arithmetic
C. Memory stack
D. Reference counting
Reference counting
Python uses a system called reference counting to track how many variables reference an object.
What happens when an object in Python is no longer referenced by any variable?
A. It remains in memory permanently.
B. It is moved to stack memory.
C. It is cleared automatically by the garbage collector.
D. It remains in memory until the program ends.
It is cleared automatically by the garbage collector.
Python uses a garbage collector that automatically removes objects when they are no longer referenced to free up memory and prevent memory leaks.
Where is a variable name stored in Python memory?
A. Heap memory
B. Stack memory
C. Cache memory
D. None
Stack memory
The variable name (reference) is stored in stack memory.
Which memory area is responsible for storing dynamically allocated objects in Python?
A. Stack
B. Heap
C. Register
D. CPU Cache
Heap
In Python, dynamically allocated objects such as variables and data structures are stored in heap memory.
What happens when multiple variables refer to the same object in Python?
A. Each variable gets a separate copy of the object.
B. Python duplicates the object in memory.
C. Python increases the reference count of the object.
D. Python moves the object to stack memory.
Python increases the reference count of the object.
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.
What will happen if the reference count of an object in Python drops to zero?
A. The object will remain in memory.
B. The object will be moved to stack memory.
C. The object will be deleted by the garbage collector.
D. The object will be stored in a cache for later use.
The object will be deleted by the garbage collector.
When no variable references an object, Python’s garbage collector removes it from memory to free up space.
What will be the output when the code executes?
def func():
x = 10
return x
y = func()
y = y + 5
print(y)A. 10
B. 15
C. None
D. None of these
15
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.
What will be the output of the below code when executed?
a = 10
b = 10
print(id(a) == id(b))
A. True
B. False
C. true
D. Error
True
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.
What is the expected output of this code?
x = 10
y = x
x = x + 5
print(y is x)
A. True
B. False
C. Error
D. None
False
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.
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)A. 20 30
B. 30 30
C. 20 20
D. 30 20
20 30
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.
What happens when this code runs?
x = 10
def func():
global x
x = 20
def inner():
nonlocal x
x = 30
inner()
func()
print(x)Error
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.
What will be the expected output of this code?
a = [1, 2, 3]
b = a
a.append(4)
print(b is a)
A. True
B. False
C. Error
D. None
True
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.
What result will this code give upon execution?
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)
A. True
B. False
C. Error
D. None
False
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.