Python Programming - Variables

Exercise : Variables - General Questions
  • Variables - General Questions
21.
Which of the following statements about Python variables is true?
Python is a statically typed language.
A variable must be declared with a type before it can be used.
Python allows multiple assignments in a single line.
A variable's scope is determined by the type of data it holds.
Answer: Option
Explanation:
Python is a dynamically typed language. In Python, a variable's type is inferred from the value it's assigned. A variable's scope is determined by where it's defined in the code.

For example:

a, b, c = 1, 2, 3

This statement assigns the values 1, 2, and 3 to the variables a, b, and c, respectively.

You can also assign the same value to multiple variables in one line:

a = b = c = "Orange"

This statement assigns the value "Orange" to the variables a, b, and c.

If you have a collection of values in a list, tuple, etc. Python allows you to extract the values into variables. This is called unpacking. For example:

fruits = ["apple", "banana", "cherry"]
a, b, c = fruits

This statement extracts the values "apple", "banana", and "cherry" from the list fruits and assigns them to the variables a, b, and c, respectively.


22.
What is the output of the following code:
x = 10
y = 5
x, y = y, x
print(x, y)
5 10
10 5
An error is raised
None of the above
Answer: Option
Explanation:

In this code, two variables x and y are assigned the values 10 and 5 respectively.

The third line swaps the values of the variables using the tuple packing and unpacking technique.

When the variables are printed to the console, the output is 5 10.


23.
Which of the following is true about global variables?
They can only be accessed within a function.
They can be accessed and modified anywhere in the code.
They can be accessed but can't be modified anywhere in the code.
They can be accessed anywhere in the code, but can only be modified within a function.
Answer: Option
Explanation:

Global variables are defined outside of any function, and can be accessed and modified from anywhere in the code.

However, it's generally not recommended to use global variables extensively, as they can make the code harder to understand and debug.


24.
Which of the following statements about Python constants is true?
They are variables whose values cannot be changed.
They are defined using the const keyword.
Python doesn't have a built-in constant type.
None of the above
Answer: Option
Explanation:
Python doesn't have a built-in constant type, unlike some other programming languages. Conventionally, constants in Python are defined using all uppercase letters to indicate that their values should not be changed, but this is only a convention and not enforced by the language.

25.
What is the output of the following code:
my_dict = {'a': 1, 'b': 2, 'c': 3}
del my_dict['b']
print(my_dict)
{'a': 1}
{'a': 1, 'c': 3}
{'a': 1, 'b': 2, 'c': 3}
An error is raised
Answer: Option
Explanation:

In this code, a dictionary my_dict is defined with three key-value pairs.

The second line uses the del statement to remove the key-value pair with the key 'b' from the dictionary.

When the dictionary is printed to the console, it shows the modified dictionary {'a': 1, 'c': 3}.