Home_Hero_Pro
Home_Hero_Pro 1d ago β€’ 0 views

Multiple Choice Questions on Arrays and Lists in Python

Hey there! πŸ‘‹ Let's solidify your understanding of Arrays and Lists in Python with a quick study guide and practice questions. Good luck! πŸ€
πŸ’» Computer Science & Technology

1 Answers

βœ… Best Answer

πŸ“š Quick Study Guide

  • 🧠 Arrays: Store elements of the same data type contiguously in memory. Accessed using index.
  • 🐍 Lists: More flexible than arrays; can store elements of different data types. Dynamic size.
  • βž• Adding Elements:
    • Arrays: Usually involves creating a new, larger array.
    • Lists: Use `append()`, `insert()`, or `extend()` methods.
  • βž– Removing Elements:
    • Arrays: Similar to adding, often involves creating a new array.
    • Lists: Use `remove()`, `pop()`, or `del` statement.
  • ⏱️ Time Complexity:
    • Accessing elements: Arrays (O(1)), Lists (O(1)).
    • Insertion/Deletion: Arrays (O(n)), Lists (average O(1) for append/pop at the end, O(n) for others).
  • πŸ’Ύ Memory: Arrays are generally more memory-efficient for storing large amounts of homogenous data. Lists have higher memory overhead per element.
  • πŸ’‘ Key Differences: Arrays require all elements to be of the same type and have a fixed size (in many implementations), while Lists are more dynamic and can hold mixed data types.

πŸ§ͺ Practice Quiz

  1. Which data structure in Python can store elements of different data types?
    1. Array
    2. Tuple
    3. List
    4. Set

  2. What is the time complexity of accessing an element in an array by its index?
    1. O(n)
    2. O(log n)
    3. O(1)
    4. O(n^2)

  3. Which method is used to add an element to the end of a list in Python?
    1. add()
    2. insert()
    3. append()
    4. extend()

  4. Which of the following is NOT a valid way to remove an element from a list?
    1. remove()
    2. pop()
    3. delete()
    4. discard()

  5. What happens if you try to access an index that is out of bounds in an array or list?
    1. Returns None
    2. Returns 0
    3. Raises an IndexError
    4. Creates a new element at that index

  6. Which of the following is true about arrays in Python?
    1. They can store elements of different data types.
    2. They have a dynamic size.
    3. They are mutable.
    4. They require all elements to be of the same data type.

  7. What is the purpose of the `insert()` method in Python lists?
    1. To add multiple elements at the end of the list.
    2. To add an element at a specified index.
    3. To remove an element from the list.
    4. To find the index of a specific element.
Click to see Answers
  1. C
  2. C
  3. C
  4. D
  5. C
  6. D
  7. B

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€