Search Tutorials


Python Variables MCQ Questions and Answers | JavaInUse

Python Variables MCQ Questions and Answers

Q. What is a variable in Python?

A. A named memory location used to store data
B. A fixed value that cannot be changed
C. A reserved keyword in Python
D. A data type used to hold values

Q. How do you declare a variable in Python?

A. By assigning a value to it
B. Using the "var" keyword
C. No declaration needed
D. Using the "declare" keyword

Q. What happens when you assign a value to a variable?

A. The variable's value is set
B. The variable's data type is determined
C. Both A and B
D. The variable's memory location is reserved

Q. Can a variable's data type change during the program's execution?

A. Yes, Python allows dynamic typing
B. No, the data type is fixed once assigned
C. Only if the variable is declared as "dynamic"
D. It depends on the variable's scope

Q. What is variable scope in Python?

A. The part of the program where a variable can be accessed
B. The lifetime of a variable
C. The visibility of a variable to other functions or modules
D. All of the above

Q. What is the difference between local and global variables?

A. Local variables are defined inside a function, while global variables are defined outside all functions
B. Global variables can be accessed from anywhere in the program, while local variables are limited to their respective functions
C. Local variables are created when a function is called, while global variables exist throughout the programs execution
D. All of the above

Q. How do you access a global variable from within a function?

A. Use the "global" keyword
B. Pass it as an argument to the function
C. Use the "extern" keyword
D. Global variables cannot be accessed from within a function

Q. What happens if you try to access an undefined variable?

A. Python raises a NameError
B. Python assigns a default value to the variable
C. Python ignores the reference to the undefined variable
D. Python terminates the program

Q. Can you use reserved keywords as variable names?

A. Yes, Python allows it
B. No, it will result in a syntax error
C. Only if the keyword is prefixed with an underscore
D. It depends on the keyword

Q. What is variable shadowing in Python?

A. When a local variable has the same name as a global variable
B. When a variable is assigned a new value
C. When a variable is declared but not assigned a value
D. When a variables value is hidden by another variable with the same name in an inner scope

Q. How can you avoid variable shadowing?

A. Use unique variable names
B. Declare all variables as global
C. Use the "nonlocal" keyword
D. Avoid using nested scopes

Q. What is the purpose of the "nonlocal" keyword in Python?

A. To access a variable in the nearest enclosing scope
B. To declare a variable as global
C. To create a constant variable
D. To access a variable in the outermost scope

Q. Can you assign multiple values to multiple variables in a single line?

A. Yes, using parallel assignment
B. No, variables must be assigned one at a time
C. Only if the variables have the same data type
D. Only if the variables are declared as global

Q. What is the purpose of the "del" statement in Python?

A. To delete a variable
B. To remove a variables value
C. To free up memory
D. To make a variable inaccessible

Q. What is the output of the following code snippet?
x = 5
y = "Python"
print(x + y)

A. 5Python
B. Error: unsupported operand types for +: int and str
C. 5
D. Python

Q. What is the type of variable that can change its value during the execution of a program?

A. Static Variable
B. Local Variable
C. Global Variable
D. Dynamic Variable

Q. What is the output of the following code?

x = 5
y = "Python"
print(x + y)
A. 5Python
B. Error: unsupported operand type(s) for +: 'int' and 'str'
C. 10
D. None of the above

Q. What is the purpose of using the "global" keyword in Python?

A. To declare a global variable
B. To access a global variable from within a function
C. To create a constant variable
D. To make a variable accessible to all functions

Q. What is the output of the following code?

a, b = 5, "Python"
print(a + int(b))
A. 5Python
B. 55
C. Error: invalid literal for int() with base 10: 'Python'
D. None of the above

Q. What is the output of the following code?

x = 10
y = x
x = 20
print(x, y)
A. 20 10
B. 10 20
C. 20 20
D. Error: invalid syntax

Q. What is the purpose of the "nonlocal" keyword in Python?

A. To declare a non-local variable
B. To access a non-local variable from within a nested function
C. To create a constant variable
D. To make a variable accessible only within a specific function

Q. What is the output of the following code?

x = "Hello"
print(x.upper())
A. HELLO
B. hello
C. Error: attribute error
D. None of the above

Q. What is the output of the following code?

x = "Python"
y = " "
z = "is fun"
print(x + y + z)
A. Python is fun
B. Python is fun
C. Pythonis fun
D. Error: cannot concatenate str and int

Q. What is the purpose of the "del" statement in Python?

A. To delete a variable
B. To remove a key-value pair from a dictionary
C. To delete a file or directory
D. All of the above

Q. What is the output of the following code?

x = 10
y = x
x += 5
print(x, y)
A. 15 10
B. 15 15
C. 10 15
D. Error: invalid syntax

Q. What is the output of the following code?

name = "Python"
print("Hello, " + name)
A. Hello, Python
B. Hello Python
C. Hello
D. None of the above

Q. What is the output of the following code?

x = 5
y = "5"
print(type(x) == type(y))
A. True
B. False

Q. What is the output of the following code?

x = "Hello"
y = "World"
print(x, end=" ")
A. Hello
B. Hello World
C. Hello World!
D. None of the above

Q. What is the output of the following code?

x = "Python"
print(x\[1:4\])
A. yth
B. ytho
C. thon
D. Error: invalid syntax

Q. What is the output of the following code?

x = "Hello"
y = x
x = "World"
print(x, y)
A. World Hello
B. Hello World
C. World World
D. Hello Hello