Exercise: Python

Questions for: Arrays

Which of the following statements about NumPy arrays in Python is true?
A:
NumPy arrays are only one-dimensional.
B:
NumPy arrays can only store integer values.
C:
NumPy arrays are less efficient than Python lists.
D:
NumPy arrays support element-wise operations.
Answer: D
NumPy arrays in Python support element-wise operations, making them efficient for mathematical computations.
How do you access the element at index 2 in a one-dimensional array named arr?
A:
arr.get(2)
B:
arr[2]
C:
arr.element(2)
D:
arr.at(2)
Answer: B
In Python arrays, elements can be accessed using square brackets and the index, like arr[index].
What does the array.append(element) method do?
A:
Appends a new array to the existing array.
B:
Adds an element to the end of the array.
C:
Removes the last element from the array.
D:
Raises an IndexError.
Answer: B
The append() method in the array module adds an element to the end of the array.
What is the primary difference between a list and an array?
A:
Lists can only store integers, while arrays can store any data type.
B:
Arrays have a fixed size, while lists can dynamically grow or shrink.
C:
Lists are one-dimensional, while arrays can be multi-dimensional.
D:
Arrays are not iterable, while lists can be iterated over.
Answer: C
Arrays in Python can be multi-dimensional, allowing for the storage of data in multiple dimensions, while lists are one-dimensional.
How do you create an array in Python using the array module?
A:
arr = array.create([1, 2, 3])
B:
arr = array([1, 2, 3])
C:
arr = array.array([1, 2, 3])
D:
arr = create.array([1, 2, 3])
Answer: C
The array module provides the array() function to create arrays in Python.
Ad Slot (Above Pagination)
Quiz