Lists — Python's Workhorse — Concept Check
Questions on Lists — Python's Workhorse. Lists are mutable, ordered, dynamic arrays — the data structure behind 80% of Python code, with slicing, in-place methods, and O(1) append but O(n) insert-at-front.
Questions in this quiz
8 questions on Lists — Python's Workhorse — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You need to add an element to the end of a list frequently in a time-efficient manner. Which operation should you use?
- Apop()
- Binsert()
- Cappend()
- Dremove()
2.A developer uses `nums.insert(0, 5)` in a loop on a large list. What performance issue might arise?
- AThe operation is O(log n) and will be efficient.
- BThe operation is O(n) and will slow down the program.
- CThe operation is O(n^2) and will crash the program.
- DThe operation is O(1) and will not cause issues.
3.You want to create a reversed copy of a list. Which slicing operation should you use?
- Alst[1:3]
- Blst[:1]
- Clst[::2]
- Dlst[::-1]
4.A developer mistakenly assigns `result = nums.sort()`. What will `result` contain?
- AThe original list
- BNone
- CThe sorted list
- DAn error message
5.Which operation is most efficient for implementing a stack using a list?
- Ainsert() and remove()
- Bremove() and insert()
- Cpop(0) and append()
- Dappend() and pop()
6.A developer uses `nums.pop(0)` to dequeue elements from a list. Why is this inefficient?
- AIt is O(n^2) and will crash.
- BIt is O(1) and efficient.
- CIt is O(log n) and inefficient.
- DIt is O(n) because it shifts all elements.
7.What is the time complexity of checking if an element exists in a list using `x in list`?
- AO(1)
- BO(n)
- CO(log n)
- DO(n^2)
8.A developer writes `matrix = [[0] * 3] * 3`. What issue might this cause?
- AAll rows reference the same list.
- BEach row is independent.
- CThe matrix is filled with None.
- DThe matrix is not initialized.