Python Variables Quiz - MCQ Questions and Answers

1. What is a variable in Python?

a) A fixed value in the program
b) A reserved memory location to store values
c) A type of Python function
d) A command that executes a program

Answer:

b) A reserved memory location to store values

Explanation:

In Python, a variable is a reserved memory location to store values. It acts as a container for data in a program.

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

a) 2data
b) data_value
c) data-value
d) data.value

Answer:

b) data_value

Explanation:

In Python, variable names can contain letters, digits, or underscores, but cannot start with a digit and should not contain special characters like hyphens or periods.

3. How do you assign a value to a variable in Python?

a) variable == value
b) variable = value
c) variable := value
d) value -> variable

Answer:

b) variable = value

Explanation:

In Python, the equal sign '=' is used to assign a value to a variable.

4. What is the output of the following Python code?

   x = 5
   y = x
   print(y)
a) 5
b) x
c) Error
d) None

Answer:

a) 5

Explanation:

The variable y is assigned the value of x, which is 5. Therefore, when print(y) is executed, it outputs 5.

5. How do you create a multi-word variable name in Python?

a) Using camelCase
b) Using underscores between words
c) Using hyphens between words
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

Multi-word variable names in Python are typically created using either camelCase (e.g., myVariable) or underscores (e.g., my_variable).

6. What is the correct way to declare a global variable in Python?

a) global variableName
b) variableName = value at the top of the script
c) declare variableName
d) global variableName = value

Answer:

b) variableName = value at the top of the script

Explanation:

In Python, global variables are declared by placing the variable outside of any function, typically at the top of the script.

7. What type of variable is "age" in this Python code?

age = 25
a) String
b) Integer
c) Float
d) Boolean

Answer:

b) Integer

Explanation:

The variable 'age' is assigned the value 25, which is an integer.

8. Which keyword in Python is used to check the type of a variable?

a) type
b) check
c) typeof
d) varType

Answer:

a) type

Explanation:

The type() function in Python is used to check the data type of a variable.

9. In Python, which of the following is a mutable data type?

a) String
b) Tuple
c) List
d) Integer

Answer:

c) List

Explanation:

In Python, lists are mutable, meaning their elements can be changed after they are created.

10. What does the following Python code print?

x = 10
   y = 20
   x, y = y, x
   print(x)
a) 10
b) 20
c) x, y
d) Error

Answer:

b) 20

Explanation:

The code swaps the values of x and y. After swapping, x holds the value of y, which is 20.

11. Can a variable in Python start with an underscore?

a) Yes
b) No
c) Only in special cases
d) Only if it is a global variable

Answer:

a) Yes

Explanation:

In Python, variable names can start with an underscore, although such names are often used for special purposes in Python conventions.

12. What is the lifetime of a local variable in Python?

a) Throughout the program execution
b) As long as the function is executing
c) Until the program exits
d) Indefinite

Answer:

b) As long as the function is executing

Explanation:

A local variable in Python exists and can be accessed only within the function where it is declared. Its lifetime is limited to the function execution.

13. What is the result of using the del keyword on a variable in Python?

a) The variable is cleared
b) The variable's value is set to None
c) The variable is deleted and becomes undefined
d) The variable becomes a global variable

Answer:

c) The variable is deleted and becomes undefined

Explanation:

The del keyword in Python is used to delete variables, after which they become undefined.

14. What is a dynamically-typed language?

a) A language where variable types must be explicitly declared
b) A language where the type of a variable is set at runtime
c) A language that can only handle dynamic data types
d) A language that changes its type during execution

Answer:

b) A language where the type of a variable is set at runtime

Explanation:

Python is a dynamically-typed language, meaning the type of a variable is determined at runtime, and you don't need to declare it explicitly.

15. What is the value of x after this code is executed in Python?

x = "100"
   x = int(x)
a) "100"
b) 100
c) Error
d) None

Answer:

b) 100

Explanation:

The variable x is initially a string "100". The int() function is used to convert it to an integer, so the value of x becomes 100.

16. What does immutability mean in the context of Python variables?

a) The variable can be deleted
b) The variable can change type
c) The value of the variable cannot be changed
d) The variable can be reassigned

Answer:

c) The value of the variable cannot be changed

Explanation:

An immutable data type means that once a value is assigned to a variable of that type, the value cannot be altered. Examples include strings and tuples.

17. How do you create a constant in Python?

a) By using the const keyword
b) By writing the variable name in uppercase
c) By declaring it outside any function
d) Python does not support constants

Answer:

b) By writing the variable name in uppercase

Explanation:

Python does not have built-in support for constants. By convention, constants are represented by writing variable names in uppercase.

18. What is type inference in Python?

a) Specifying the type of a variable
b) The process by which Python automatically determines a variable's type
c) Converting one data type to another
d) Defining custom data types

Answer:

b) The process by which Python automatically determines a variable's type

Explanation:

Type inference is the ability of Python to automatically deduce the type of a variable from the value assigned to it.

19. What is the scope of a global variable in Python?

a) Inside the function where it is declared
b) Throughout the module where it is declared
c) Only at the top level of the script
d) Throughout all modules in the program

Answer:

b) Throughout the module where it is declared

Explanation:

A global variable in Python is accessible from anywhere within the module where it is declared.

20. What happens when you assign a new value to an existing variable in Python?

a) The variable type changes
b) The original value is overwritten
c) A new variable is created
d) The variable becomes immutable

Answer:

b) The original value is overwritten

Explanation:

Assigning a new value to an existing variable in Python overwrites its original value, regardless of the new value's data type.

Comments