Python Programming — Data Types & Lists
Python fundamentals to advanced for interviews
Showing 1–5 of 5 questions
in Data Types & Lists
What will be printed?
my_list = [1, 2, 3, 4, 5]
print(my_list[::-1])
my_list = [1, 2, 3, 4, 5]
print(my_list[::-1])
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)
my_list = [1, 2, 3]
my_list.insert(1, 99)
print(my_list)
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])
my_list = [1, 2, 3, 4, 5]
print(my_list[-2])
Correct Answer:
C. 4
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?
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])
my_list = [1, 2, 3]
my_list_copy = my_list
my_list[0] = 99
print(my_list_copy[0])
Correct Answer:
B. 99
EXPLANATION
Assignment creates a reference, not a copy. Both variables point to the same list object, so changes are reflected in both.