Showing 1–10 of 25 questions
What will be printed?
my_list = [1, 2, 3, 4, 5]
print(my_list[::-1])
A
[5, 4, 3, 2, 1]
B
[1, 2, 3, 4, 5]
C
Error
D
None
Correct Answer:
A. [5, 4, 3, 2, 1]
EXPLANATION
Slice [::-1] reverses the list by using step -1 (backwards through entire list). It returns a reversed copy.
What will be the result of executing this code?
my_list = [1, 2, 3]
my_list.insert(1, 99)
print(my_list)
A
[1, 99, 2, 3]
B
[99, 1, 2, 3]
C
[1, 2, 99, 3]
D
[1, 2, 3, 99]
Correct Answer:
A. [1, 99, 2, 3]
EXPLANATION
insert(1, 99) inserts 99 at index 1, shifting existing elements right. Result is [1, 99, 2, 3].
What will be the output?
my_list = [1, 2, 3, 4, 5]
print(my_list[-2])
EXPLANATION
Negative indexing starts from the end: -1 is 5, -2 is 4, -3 is 3, etc. So -2 returns 4.
What is the correct way to create a true copy of a list?
A
my_copy = my_list
B
my_copy = my_list.copy()
C
my_copy = list(my_list)
D
Both B and C
Correct Answer:
D. Both B and C
EXPLANATION
Both .copy() method and list() constructor create shallow copies. Simple assignment creates only a reference.
What will be the output?
my_list = [1, 2, 3]
my_list_copy = my_list
my_list[0] = 99
print(my_list_copy[0])
EXPLANATION
Assignment creates a reference, not a copy. Both variables point to the same list object, so changes are reflected in both.
What is the output of: print(type(lambda x: x))?
EXPLANATION
Lambda functions are functions in Python. type(lambda x: x) returns <class 'function'>.
Consider: class A: x = 5. What is A.x and how can it be modified?
A
Instance variable, modify via obj.x
B
Class variable, modify via A.x or obj.x
C
Local variable, cannot be modified
D
Error
Correct Answer:
B. Class variable, modify via A.x or obj.x
EXPLANATION
x = 5 is a class variable. It can be accessed via A.x and modified via A.x = new_value or instance modification.
What will be the output of: print({x: x**2 for x in range(3)})?
A
{0: 0, 1: 1, 2: 4}
B
{0: 1, 1: 2, 2: 3}
C
{1: 1, 2: 4}
D
Error
Correct Answer:
A. {0: 0, 1: 1, 2: 4}
EXPLANATION
Dictionary comprehension creates {0: 0, 1: 1, 2: 4} where keys are from range(3) and values are their squares.
Consider the code: with open('file.txt') as f: data = f.read(). What happens when the block exits?
A
File remains open
B
File is automatically closed
C
Error occurs
D
data is deleted
Correct Answer:
B. File is automatically closed
EXPLANATION
The 'with' statement ensures the file is automatically closed when the block exits, even if an error occurs.
What is the output of: print(eval('2+3*4'))?
EXPLANATION
eval() evaluates the string as Python code following operator precedence. 2+3*4 = 2+12 = 14.