Assess your understanding of basic sorting algorithms, custom sort keys, and common binary search scenarios. This quiz is designed to reinforce foundational knowledge in ordering data and efficient search methods.
Identifying Built-in Sorting Orders
Given the list [4, 1, 3, 2], what will be the result after applying a standard ascending sort?
- [1, 2, 3, 4]
- [4, 3, 2, 1]
- [4, 2, 1, 3]
- [1, 3, 2, 4]
- [2, 1, 3, 4]
Purpose of a Custom Sort Key
Which statement best describes the purpose of providing a custom sort key when sorting a collection?
- It reverses the sorting order by default.
- It increases sorting performance always.
- It prevents duplicate elements from being sorted.
- It determines how elements are compared during sorting.
- It changes the data type of each element.
Case-insensitive Sorting
When sorting the strings ['apple', 'Banana', 'cherry'] in a case-insensitive way, which function can be used as a custom key?
- str.search
- str.uppercase
- str.append
- str.concat
- str.lower
Binary Search Prerequisite
Binary search requires which important property to work correctly on a list?
- The list must contain only numbers.
- The list size must be even.
- The list must start with zero.
- The list must be sorted.
- The list must have only unique elements.
Binary Search Midpoint Edge Case
In binary search, what is a common mistake when calculating the middle index for very large lists?
- Sorting the list before searching.
- Returning from the function too early.
- Including duplicate values in search.
- Overflow when adding low and high indices.
- Ignoring negative numbers in the list.
Descending Order Sorting
To sort [10, 2, 7, 4] in descending order, which parameter should be set to True in many sorting functions?
- order_by
- invert_sort
- reverse
- invert
- descend
Custom Sort Key for Sorting by Length
If you want to sort a list of words by the number of characters, which built-in function is most appropriate as the key?
- len
- max
- sum
- num
- ord
Non-Existent Element in Binary Search
What is the expected result when performing binary search for 5 in the sorted list [1, 2, 4, 6, 8]?
- Duplicate found
- Middle element
- First element
- Not found
- List will be resorted
Stability in Sorting Algorithms
If a sorting algorithm preserves the order of equal elements, what property is this called?
- Simplicity
- Flexibility
- Stability
- Reversibility
- Scalability
Lowest Index Binary Search
When searching for the value 3 in [1, 2, 3, 3, 3, 4], using binary search, what must be added to return the index of the first occurrence?
- Return the highest index found
- Use linear search after binary search
- Double the size of the list
- Modify binary search to continue left after finding 3
- Sort the list after every comparison