Python Arrays MCQ

1. What is a Python array?

a) A built-in data type for storing key-value pairs
b) A linear data structure for storing elements of the same data type
c) A collection of unordered elements
d) A list of dictionaries

Answer:

b) A linear data structure for storing elements of the same data type

Explanation:

A Python array is a linear data structure for storing elements of the same data type.

2. How are elements accessed in a Python array?

a) Using a key
b) Using an index
c) Using a hash code
d) Using a label

Answer:

b) Using an index

Explanation:

Elements in a Python array are accessed using an index.

3. Which module is commonly used for working with Python arrays?

a) array
b) list
c) collections
d) numpy

Answer:

a) array

Explanation:

The `array` module is commonly used for working with Python arrays.

4. What is the purpose of the append() method in Python arrays?

a) To remove an element from the array
b) To add an element to the end of the array
c) To insert an element at a specific index
d) To clear all elements from the array

Answer:

b) To add an element to the end of the array

Explanation:

The `append()` method is used to add an element to the end of a Python array.

5. How can you remove an element from a Python array by its value?

a) Using the `pop()` method
b) Using the `remove()` method
c) Using slicing
d) Using the `delete()` method

Answer:

b) Using the `remove()` method

Explanation:

The `remove()` method is used to remove an element from a Python array by its value.

6. What is the purpose of the pop() method in Python arrays?

a) To push an element onto the array
b) To remove and return the last element of the array
c) To remove and return the first element of the array
d) To clear all elements from the array

Answer:

b) To remove and return the last element of the array

Explanation:

The `pop()` method removes and returns the last element of a Python array.

7. Which method is used to check if an element exists in a Python array?

a) `find()`
b) `in`
c) `contains()`
d) `exists()`

Answer:

b) `in`

Explanation:

You can check if an element exists in a Python array using the `in` keyword.

8. Which method is used to sort a Python array in ascending order?

a) `sort()`
b) `reverse()`
c) `sorted()`
d) `shuffle()`

Answer:

a) `sort()`

Explanation:

The `sort()` method is used to sort a Python array in ascending order.

9. How can you reverse the order of elements in a Python array?

a) Using the `sort()` method with reverse=True
b) Using the `reverse()` method
c) Using the `reversed()` function
d) Using a loop

Answer:

b) Using the `reverse()` method

Explanation:

The `reverse()` method is used to reverse the order of elements in a Python array.

10. Which method is used to clear all elements from a Python array?

a) `clear()`
b) `delete()`
c) `pop()`
d) `remove_all()`

Answer:

a) `clear()`

Explanation:

The `clear()` method is used to clear all elements from a Python array.

11. What is the time complexity of accessing an element in a Python array by index?

a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)

Answer:

a) O(1)

Explanation:

Accessing an element in a Python array by index has a time complexity of O(1).

12. Which data structure is used to implement Python arrays?

a) Linked list
b) Dynamic array
c) Hash table
d) Queue

Answer:

b) Dynamic array

Explanation:

Python arrays are implemented as dynamic arrays.

13. What is the primary purpose of using arrays in Python?

a) To create a collection of key-value pairs
b) To represent a stack data structure
c) To store and manipulate a collection of elements efficiently
d) To store elements in a random order

Answer:

c) To store and manipulate a collection of elements efficiently

Explanation:

The primary purpose of using arrays in Python is to store and manipulate a collection of elements efficiently.

14. Which module provides support for numerical arrays and mathematical operations in Python?

a) array
b) math
c) numpy
d) statistics

Answer:

c) numpy

Explanation:

The `numpy` module provides support for numerical arrays and mathematical operations in Python.

15. What is the result of using the extend() method to add elements from one array to another?

a) The original array is modified to include the elements from the second array.
b) A new array is created with elements from both arrays.
c) The second array is cleared, and its elements are added to the first array.
d) An error is raised because the `extend()` method is not supported in Python.

Answer:

a) The original array is modified to include the elements from the second array.

Explanation:

The `extend()` method modifies the original array to include elements from the second array.

16. Which method is used to insert an element at a specific index in a Python array?

a) `add()`
b) `insert()`
c) `append()`
d) `put()`

Answer:

b) `insert()`

Explanation:

The `insert()` method is used to insert an element at a specific index in a Python array.

17. How can you find the index of the first occurrence of an element in a Python array?

a) Using the `search()` method
b) Using the `index()` method
c) Using the `find()` method
d) Using the `locate()` method

Answer:

b) Using the `index()` method

Explanation:

The `index()` method is used to find the index of the first occurrence of an element in a Python array.

18. Which method is used to count the number of occurrences of an element in a Python array?

a) `count()`
b) `find()`
c) `search()`
d) `locate()`

Answer:

a) `count()`

Explanation:

The `count()` method is used to count the number of occurrences of an element in a Python array.

19. What is the result of using the copy() method to create a copy of a Python array?

a) A shallow copy of the original array is created.
b) A deep copy of the original array is created.
c) The original array is cleared, and its elements are copied to a new array.
d) An error is raised because the `copy()` method is not supported in Python.

Answer:

a) A shallow copy of the original array is created.

Explanation:

The `copy()` method creates a shallow copy of the original array.

20. Which method is used to remove the element at a specific index in a Python array?

a) `pop()`
b) `remove()`
c) `delete()`
d) `clear()`

Answer:

a) `pop()`

Explanation:

The `pop()` method is used to remove the element at a specific index in a Python array.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top