Python Programming - Arrays

Exercise : Arrays - General Questions
  • Arrays - General Questions
6.
What is the primary difference between a list and an array?
Lists can only store integers, while arrays can store any data type.
Arrays have a fixed size, while lists can dynamically grow or shrink.
Lists are one-dimensional, while arrays can be multi-dimensional.
Arrays are not iterable, while lists can be iterated over.
Answer: Option
Explanation:
Arrays in Python can be multi-dimensional, allowing for the storage of data in multiple dimensions, while lists are one-dimensional.

7.
What does the array.append(element) method do?
Appends a new array to the existing array.
Adds an element to the end of the array.
Removes the last element from the array.
Raises an IndexError.
Answer: Option
Explanation:
The append() method in the array module adds an element to the end of the array.

8.
How do you access the element at index 2 in a one-dimensional array named arr?
arr.get(2)
arr[2]
arr.element(2)
arr.at(2)
Answer: Option
Explanation:
In Python arrays, elements can be accessed using square brackets and the index, like arr[index].

9.
Which of the following statements about NumPy arrays in Python is true?
NumPy arrays are only one-dimensional.
NumPy arrays can only store integer values.
NumPy arrays are less efficient than Python lists.
NumPy arrays support element-wise operations.
Answer: Option
Explanation:
NumPy arrays in Python support element-wise operations, making them efficient for mathematical computations.

10.
How do you create a 2D NumPy array?
np.array([[1, 2, 3], [4, 5, 6]])
numpy.create_2d_array([[1, 2, 3], [4, 5, 6]])
array.create([[1, 2, 3], [4, 5, 6]])
np.array([1, 2, 3], [4, 5, 6])
Answer: Option
Explanation:
The correct syntax to create a 2D NumPy array is np.array([[1, 2, 3], [4, 5, 6]]).