Python Arrays Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
How do you get the number of elements in a Python list?
  • A len(list)
  • B list.size()
  • C list.length()
  • D list.count()
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?
  • A A concise way to create lists
  • B A method to comprehend list structure
  • C A way to read lists from files
  • D A technique to compress lists
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?
  • A clear()
  • B empty()
  • C delete_all()
  • D remove_all()
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]?
  • A [0, 4, 16]
  • B [0, 1, 4, 9, 16]
  • C [0, 4]
  • D [1, 9]
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?
  • A sort(reverse=True)
  • B sort(descending=True)
  • C sort(order="desc")
  • D reverse_sort()
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()?
  • A sort() modifies in-place, sorted() returns new list
  • B sort() is faster than sorted()
  • C sorted() works only for numbers
  • D There is no difference
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?
  • A element in list
  • B list.contains(element)
  • C list.exists(element)
  • D list.find(element) != -1
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?
  • A Elements from index 1 to 3
  • B Elements from index 1 to 4
  • C First 4 elements
  • D Elements from index 0 to 3
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?
  • A [0] * 5
  • B list(5, 0)
  • C zeros(5)
  • D list.zeros(5)
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])?
  • A 8
  • B 1
  • C 5
  • D 3
Answer: Option A
Explanation: max() function returns the largest element in the iterable. For numbers, it compares numerically. For strings, it compares lexicographically.
Questions and Answers for Competitive Exams Various Entrance Test