VOOZH about

URL: https://www.analyticsvidhya.com/blog/2024/02/mcqs-on-python-variable/

⇱ 30 Multiple-Choice Questions on Python Variables


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

30+ Multiple-Choice Questions on Python Variables

Ayushi Trivedi Last Updated : 19 May, 2025
9 min read

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.

30+ Python Interview Questions on Python Variable

Q1. What is a variable in Python?

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.

Q2. Which symbol is used to assign a value to a variable in Python?

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.

Q3. What is the correct way to create an integer variable named β€œx” with the value 5 in Python?

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.

Q4. In Python, variable names are case-sensitive. What does this mean?

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.

Q5. Which of the following is a valid variable name in Python?

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.

Q6. What is the type of the variable β€œx” after executing the following code: x = 5.0?

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.

Q7. What happens if you try to use a variable that has not been defined in Python?

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.

Q8. Which of the following is not a valid variable name in Python?

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.

Q9. What is the output of the following code?

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.

Q10. Which data type is mutable in Python?

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.

Q11. What is the scope of a variable in Python?

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.

Q12. What does the β€œglobal” keyword do in Python?

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.

Q13. What is the purpose of the β€œdel” keyword in Python?

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.

Q14. Which of the following statements about Python variables is correct?

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.

Q15. What is the output of the following code?

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.

Q16. What is the difference between β€œ==” and β€œis” in Python when comparing variables?

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.

Q17. What is the output of the following code?

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.

Q18. Which of the following is not a valid way to comment out a line in Python?

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.

Q19. What is the output of the following code?

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”.

Q20. What is the purpose of the β€œid()” function in Python?

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.

Q21. What is the difference between local and global variables in Python?

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.

Q22. What does the β€œnonlocal” keyword do in Python?

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.

Q23. What is the output of the following code?

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.

Q24. Which of the following is true about immutable data types in Python?

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.

Q25. What is the output of the following code?

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”.

Q26. What is the purpose of the β€œlocals()” function in Python?

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.

Q27. What does the β€œid()” function return in Python?

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.

Q28. What is the output of the following code?

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()”.

Q29. Which of the following is not a valid way to delete a variable in Python?

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.

Q30. What is the output of the following code?

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.

Q31. What is the output of the following code snippet?

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.

Q32. What is the output of the following code snippet?

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.

Q33. What is the output of the following code snippet?

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.

Q34. What is the output of the following code snippet?

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.

Q35. What is the output of the following code snippet?

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.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
πŸ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
πŸ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

πŸ‘ Popup Banner
πŸ‘ AI Popup Banner