![]() |
VOOZH | about |
Python variables play a crucial role in programming as they serve as containers to store and manipulate data during program execution. Understanding how variables work in Python is fundamental for anyone learning the language. This set of multiple-choice questions aims to test your knowledge of Python variables, covering topics ranging from basic variable creation to more advanced concepts such as variable scope and mutability. By answering these questions, youβll not only reinforce your understanding of Python variables but also sharpen your problem-solving skills in Python Interview Questions.
a) A container to store data
b) A function to perform mathematical operations
c) A loop to iterate through data
d) A conditional statement for decision-making
Answer: a
Explanation: A container to store data β This is the correct definition of a variable. In Python, variables are used to store data which can be accessed and manipulated during program execution.
a) =
b) :
c) #
d) $
Answer: a
Explanation: The equals sign (=) is used for assignment in Python. It assigns the value on the right to the variable on the left.
a) x = 5
b) int x = 5
c) x := 5
d) 5 = x
Answer: a
Explanation: x = 5 β This is the correct syntax to create an integer variable named βxβ with the value 5 in Python. The variable name comes first, followed by the assignment operator (=), and then the value.
a) Variable names cannot contain uppercase letters.
b) Variable names must always start with an uppercase letter.
c) Variable names must always be written in uppercase letters.
d) Variable names are distinguished by uppercase and lowercase letters.
Answer: d
Explanation: Variable names are distinguished by uppercase and lowercase letters in Python. Therefore, variables with different cases (e.g., βxβ and βXβ) are treated as different variables.
a) 3total
b) my_variable
c) global
d) class
Answer: b
Explanation: my_variable β This is a valid variable name in Python. Variable names can contain letters, numbers, and underscores, but cannot start with a number.
a) int
b) str
c) float
d) bool
Answer: c
Explanation: float β After executing the code βx = 5.0β, the variable βxβ will be of type float because the value assigned to it is a floating-point number.
a) Python will automatically define the variable.
b) Python will prompt the user to define the variable.
c) Python will raise a NameError.
d) Python will ignore the variable.
Answer: c
Explanation: Python will raise a NameError indicating that the variable is not defined if you try to use a variable that has not been defined.
a) my_variable_1
b) 1st_variable
c) variable2
d) _variable
Answer: b
Explanation: 1st_variable β Variable names cannot start with a number in Python. They must start with a letter or an underscore.
x = 10
y = x + 5
print(y)
a) 10
b) 5
c) 15
d) x + 5
Answer: c
Explanation: The code assigns the value 10 to the variable βxβ, then adds 5 to it and assigns the result to the variable βyβ, and finally prints the value of βyβ, which is 15.
a) int
b) float
c) list
d) tuple
Answer: c
Explanation: Lists are mutable data types in Python, meaning that their elements can be changed after the list is created.
a) The range of values a variable can hold.
b) The visibility of a variable within a program.
c) The data type of a variable.
d) The memory location of a variable.
Answer: b
Explanation: Scope refers to the visibility of a variable within a program. Local variables are only accessible within the function they are defined in, while global variables are accessible from any part of the program.
a) Declares a variable that is accessible from anywhere in the program.
b) Declares a variable that is local to a specific function.
c) Declares a variable that is accessible only within a specific module.
d) Declares a variable that cannot be modified.
Answer: a
Explanation: The βglobalβ keyword is used to declare a variable that can be accessed from anywhere in the program, not just within a specific function.
a) To delete a variable from memory.
b) To define a new variable.
c) To initialize a variable.
d) To declare a variable as global.
Answer: a
Explanation: The βdelβ keyword in Python is used to delete a variable, freeing up the memory it occupied.
a) Variables must be declared before use.
b) Variables can only store numeric values.
c) Variables can be reassigned to different data types.
d) Variables cannot be assigned values within loops.
Answer: c
Explanation: Unlike some other programming languages, Python allows variables to be reassigned to different data types.
x = 10
x += 5
print(x)
a) 5
b) 10
c) 15
d) 20
Answer: c
Explanation: The code increments the value of βxβ by 5 using the β+=β operator and then prints the updated value, which is 15.
a) β==β checks for equality of values, while βisβ checks for equality of memory location.
b) β==β checks for equality of memory location, while βisβ checks for equality of values.
c) There is no difference; they are interchangeable.
d) β==β is used for assignment, while βisβ is used for comparison.
Answer: a
Explanation: The β==β operator checks whether the values of two variables are equal, while the βisβ operator checks whether the two variables refer to the same object in memory.
x = 5
y = x
x = 10
print(y)
a) 5
b) 10
c) Error
d) 0
Answer: a
Explanation: The value of βyβ is assigned to be the same as the initial value of βxβ (which is 5) before βxβ is reassigned to 10. Therefore, printing βyβ will output 5.
a) // This is a comment
b) # This is a comment
c) ββ This is a comment ββ
d) βββ This is a comment βββ
Answer: a
Explanation: In Python, the β//β symbol is used for integer division, not for commenting. Comments in Python start with the β#β symbol.
x = "Hello"
y = x
x += " World"
print(y)
a) Hello
b) World
c) Hello World
d) Error
Answer: a
Explanation: The variable βyβ is assigned the same value as βxβ before βxβ is modified. Therefore, printing βyβ will output the original value of βxβ, which is βHelloβ.
a) To identify the data type of a variable.
b) To generate random numbers.
c) To return the memory address of an object.
d) To convert a variable to an integer.
Answer: c
Explanation: The βid()β function in Python returns the memory address of an object, which is a unique identifier for that object.
a) Local variables are declared outside of any function, while global variables are declared within functions.
b) Local variables are accessible only within the function they are defined in, while global variables are accessible from any part of the program.
c) Local variables are stored in the global namespace, while global variables are stored in local namespaces.
d) Local variables are immutable, while global variables are mutable.
Answer: b
Explanation: Local variables are only accessible within the function they are defined in, while global variables are accessible from any part of the program.
a) Declares a variable that is local to a specific function.
b) Declares a variable that is accessible from anywhere in the program.
c) Declares a variable that is accessible only within a specific module.
d) Declares a variable that is not local to the current function but not global either.
Answer: d
Explanation: The βnonlocalβ keyword is used to declare a variable in an enclosing scope (outside of the current function), but not in the global scope.
def test():
global x
x = 10
test()
print(x)
a) 0
b) 10
c) Error
d) None
Answer: b
Explanation: Even though βxβ is declared as global within the function βtest()β, it is still accessible globally. Therefore, printing βxβ after calling the function will output the value assigned to it within the function, which is 10.
a) Their values cannot be changed after assignment.
b) They can only store numeric values.
c) They are always passed by reference.
d) They include lists and dictionaries.
Answer: a
Explanation: Immutable data types in Python, such as tuples and strings, cannot be modified after they are created.
x = [1, 2, 3]
y = x
x.append(4)
print(y)
a) [1, 2, 3]
b) [1, 2, 3, 4]
c) [1, 2, 4]
d) Error
Answer: b
Explanation: Both βxβ and βyβ refer to the same list object, so appending a value to βxβ will also affect βyβ.
a) To return a dictionary of local variables in the current scope.
b) To return the memory address of a local variable.
c) To check if a variable is local or global.
d) To assign values to local variables.
Answer: a
Explanation: The βlocals()β function in Python returns a dictionary containing all local variables in the current scope.
a) The identity of an object.
b) The memory address of an object.
c) The data type of an object.
d) The size of an object.
Answer: b
Explanation: The βid()β function in Python returns the memory address of an object, which uniquely identifies it.
x = 10
def test():
print(x)
x = 20
test()
a) 10
b) 20
c) Error
d) None
Answer: c
Explanation: This code will raise an UnboundLocalError because βxβ is being referenced before it is assigned within the function βtest()β.
a) del x
b) x.delete()
c) x = None
d) x = 0
Answer: b
Explanation: There is no method called βdelete()β to delete a variable in Python. The correct ways to delete a variable are using βdelβ or assigning it to None.
x = 10
def test():
global x
x += 5
test()
print(x)
a) 5
b) 10
c) 15
d) Error
Answer: c
Explanation: The βglobalβ keyword inside the function βtest()β allows the function to modify the global variable βxβ, so after calling the function, the value of βxβ becomes 15.
def test():
x = 10
def inner_test():
nonlocal x
x += 5
inner_test()
print(x)
test()
a) 1
b) 15
c) Error
d) 5
Answer: b
Explanation: The nonlocal keyword allows modification of a variable in an outer (but non-global) scope. The inner_test() function modifies the value of x by adding 5 to it. Therefore, the output will be 15.
x = 10
def test():
x = 20
def inner_test():
nonlocal x
x += 5
inner_test()
print(x)
test()
print(x)
a) 25, 10
b) 25, 25
c) 20, 10
d) 20, 20
Answer: c
Explanation: Inside the inner_test() function, the nonlocal keyword allows modification of the x variable from the outer scope, adding 5 to it. However, this change is local to the test() function. Therefore, the output within the test() function is 25, but outside of it, x remains unaffected and prints 10.
x = 10
def test():
global x
x = 20
def inner_test():
nonlocal x
x += 5
inner_test()
print(x)
test()
print(x)
a) 15, 20
b) 25, 20
c) Error
d) 15, 25
Answer: c
Explanation: This code will raise a SyntaxError because a variable cannot be declared both global and nonlocal in the same scope.
x = 10
def test():
x = 20
def inner_test():
global x
x += 5
inner_test()
print(x)
test()
print(x)
a) 25, 25
b) 20, 10
c) Error
d) 20, 25
Answer: a
Explanation: Inside the inner_test() function, the global keyword is used to access the global variable x and modify its value by adding 5 to it. Therefore, the output within the test() function is 25, and outside of it, x remains 25 as well.
x = 10
def test():
x = 20
def inner_test():
nonlocal x
x += 5
inner_test()
print(x)
test()
print(x)
a) 20, 10
b) 25, 10
c) 20, 20
d) 25, 25
Answer: c
Explanation: Inside the inner_test() function, the nonlocal keyword allows modification of the x variable from the outer test() function. Therefore, the output within the test() function is 25, but outside of it, x remains 20.
Congratulations on completing this set of multiple-choice questions on Python variables! Whether youβre a beginner looking to solidify your understanding or an experienced programmer seeking to refresh your knowledge, engaging with these questions has undoubtedly contributed to your mastery of Python variables. Remember, variables are the building blocks of any Python program, and a solid understanding of their concepts is essential for writing efficient and error-free code. Keep practicing and exploring Python, and youβll continue to enhance your programming skills. Keep up the excellent work!
Also read, 90+ Python Interview Questions and Answers
My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.
GPT-4 vs. Llama 3.1 β Which Model is Better?
Llama-3.1-Storm-8B: The 8B LLM Powerhouse Surpa...
A Comprehensive Guide to Building Agentic RAG S...
Top 10 Machine Learning Algorithms in 2026
45 Questions to Test a Data Scientist on Basics...
90+ Python Interview Questions and Answers (202...
8 Easy Ways to Access ChatGPT for Free
Prompt Engineering: Definition, Examples, Tips ...
What is LangChain?
What is Retrieval-Augmented Generation (RAG)?
Edit
Resend OTP
Resend OTP in 45s