Python lists start indexing at 0, so fruits[1] is the second item.
Step 2: Identify the item at index 1
The list is ['apple', 'banana', 'cherry'], so index 1 is 'banana'.
Final Answer:
banana -> Option D
Quick Check:
fruits[1] = banana [OK]
Hint: List indexes start at 0, so index 1 is second item [OK]
Common Mistakes:
Thinking index starts at 1
Confusing list with dictionary
Expecting an error for valid index
4. Find the error in this code that tries to add an item to a list:
my_list = [1, 2, 3]
my_list.add(4)
print(my_list)
medium
A. Missing brackets in print statement
B. Using add() instead of append() to add item
C. List cannot hold numbers
D. List must be declared with curly braces
Solution
Step 1: Identify method to add items to list
Python lists use append() to add items, not add().
Step 2: Check the code for method usage
The code uses my_list.add(4), which causes an error because add() is not a list method.
Final Answer:
Using add() instead of append() to add item -> Option B
Quick Check:
Use append() to add items to list [OK]
Hint: Use append() to add items to lists, not add() [OK]
Common Mistakes:
Using add() which is for sets
Thinking lists can't hold numbers
Incorrect print syntax assumptions
5. You have a list of student names and want to add a new student, remove one who left, and keep the order. Which data structure should you use and why?
hard
A. Use a dictionary because it stores key-value pairs
B. Use a set because it automatically sorts items
C. Use a list because it keeps order and allows adding/removing items
D. Use a tuple because it is immutable
Solution
Step 1: Understand requirements for data structure
The data structure must keep order and allow adding/removing students.
Step 2: Match requirements with data structure features
Lists keep order and allow adding/removing items. Sets do not keep order. Dictionaries store key-value pairs, not just items. Tuples are immutable and cannot be changed.
Final Answer:
Use a list because it keeps order and allows adding/removing items -> Option C
Quick Check:
Lists keep order and are changeable [OK]
Hint: Lists keep order and allow changes, perfect for student lists [OK]