Exercise: Tricky Questions

Questions for: Tricky Questions

What is the output of the following Python code?
x = [1, 2, 3]
y = x
y[0] = 10
print(x)
A:
[1, 2, 3]
B:
[10, 2, 3]
C:
[10, 2, 3] (but with a warning)
D:
[1, 2, 3, 10]
Answer: B
y is assigned a reference to the same list as x, so modifying y also modifies x.
What is the output of the following Python code?
x = 3
y = 2
result = x ** y
print(result)
A:
6
B:
8
C:
9
D:
1
Answer: C
The double asterisk (**) is the exponentiation operator, and 3 raised to the power of 2 is 9.
What will be the output of the following Python code?
x = "Python"
y = x.lower()
z = x.upper()
result = y + z
print(result)
A:
"PythonPYTHON"
B:
"pythonPYTHON"
C:
"pythonPYTHONpython"
D:
This code will result in an error.
Answer: B
The lower() method makes the string lowercase, and upper() makes it uppercase.
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)
A:
[0, 1, 2]
B:
[1, 2, 3]
C:
This code will result in an error.
D:
None
Answer: B
The function reassigns my_list to a new list, but it does not modify the original list outside the function.
Consider the following Python code:
def my_generator():
    yield 1
    yield 2
    yield 3

result = list(my_generator())
print(result)
What will be the output of this code?
A:
[1, 2, 3]
B:
(1, 2, 3)
C:
{1, 2, 3}
D:
This code will result in an error.
Answer: A
The generator function is converted to a list using the list() constructor.
Ad Slot (Above Pagination)
Quiz