Python Arrays Questions and Answers
Practice ModeShowing 10 of 25 questions
Q11
How do you get the number of elements in a Python list?
Answer: Option A
Explanation: len() function returns the number of elements in a list. The count() method returns the number of occurrences of a specific value.
Q12
What is list comprehension in Python?
Answer: Option A
Explanation: List comprehension provides a concise way to create lists by applying an expression to each item in an iterable, optionally with filtering.
Q13
Which method removes all elements from a list?
Answer: Option A
Explanation: clear() method removes all items from the list, leaving it empty. del list[:] also achieves the same result.
Q14
What is the output of [x**2 for x in range(5) if x % 2 == 0]?
Answer: Option A
Explanation: This list comprehension squares each even number from 0 to 4: 0²=0, 2²=4, 4²=16.
Q15
How do you sort a list in descending order?
Answer: Option A
Explanation: sort(reverse=True) sorts the list in-place in descending order. sorted(list, reverse=True) returns a new sorted list.
Q16
What is the difference between sort() and sorted()?
Answer: Option A
Explanation: sort() modifies the list in-place and returns None. sorted() returns a new sorted list and leaves the original unchanged.
Q17
How do you check if an element exists in a list?
Answer: Option A
Explanation: The "in" operator checks for membership in O(n) time for lists. For frequent membership tests, consider using sets for O(1) lookups.
Q18
What does the slice notation list[1:4] return?
Answer: Option A
Explanation: Slice notation [start:stop] returns elements from index start (inclusive) to stop (exclusive). So [1:4] returns elements at indices 1, 2, and 3.
Q19
How do you create a list with 5 zeros?
Answer: Option A
Explanation: [0] * 5 creates a list with five zeros. This creates a list where all elements reference the same object (fine for immutable types like integers).
Q20
What is the output of max([1, 5, 2, 8, 3])?
Answer: Option A
Explanation: max() function returns the largest element in the iterable. For numbers, it compares numerically. For strings, it compares lexicographically.