Python Programming - Tricky Questions

Exercise : Tricky Questions - General Questions
  • Tricky Questions - General Questions
31.
What is the output of the following Python code?
def modify_list(my_list):
    my_list = [0, 1, 2]

original_list = [1, 2, 3]
modify_list(original_list)
print(original_list)
[0, 1, 2]
[1, 2, 3]
This code will result in an error.
None
Answer: Option
Explanation:
The function reassigns my_list to a new list, but it does not modify the original list outside the function.

32.
What will be the output of the following Python code?
x = "Python"
y = x.lower()
z = x.upper()
result = y + z
print(result)
"PythonPYTHON"
"pythonPYTHON"
"pythonPYTHONpython"
This code will result in an error.
Answer: Option
Explanation:
The lower() method makes the string lowercase, and upper() makes it uppercase.

33.
What is the output of the following Python code?
x = 3
y = 2
result = x ** y
print(result)
6
8
9
1
Answer: Option
Explanation:
The double asterisk (**) is the exponentiation operator, and 3 raised to the power of 2 is 9.

34.
What is the output of the following Python code?
x = [1, 2, 3]
y = x
y[0] = 10
print(x)
[1, 2, 3]
[10, 2, 3]
[10, 2, 3] (but with a warning)
[1, 2, 3, 10]
Answer: Option
Explanation:
y is assigned a reference to the same list as x, so modifying y also modifies x.

35.
What will be the output of the following Python code?
string1 = "Python"
string2 = string1
string2 += " is great"
result = string1 + string2
print(result)
"Python is greatPython is great"
"PythonPython is great"
"Python is great is great"
This code will result in an error.
Answer: Option
Explanation:
Immutable strings, when concatenated, create a new string.