Python Programming - Arrays

Exercise : Arrays - General Questions
  • Arrays - General Questions
1.
What will be the output of the following code snippet?
numbers = [1, 2, 3, 4, 5]
numbers[1] = 9
print(numbers)
[1, 9, 3, 4, 5]
[1, 2, 3, 4, 5]
[9, 2, 3, 4, 5]
Error
Answer: Option
Explanation:
Lists in Python are mutable, so you can change the value of an element using indexing.

2.
What will be the output of the following code snippet?
numbers = [1, 2, 3, 4, 5]
sliced_numbers = numbers[1:4]
print(sliced_numbers)
[1, 2, 3, 4, 5]
[2, 3, 4]
[1, 2, 3]
[3, 4, 5]
Answer: Option
Explanation:
Slicing is used to extract a portion of a list. In this case, it extracts elements from index 1 to 3.
numbers = [1, 2, 3, 4, 5]
sliced_numbers = numbers[1:4]
print(sliced_numbers)

3.
What will be the output of the following code snippet?
fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
print(fruits)
['apple', 'banana', 'cherry']
['apple', 'cherry']
['banana', 'cherry']
Error
Answer: Option
Explanation:
The remove() method is used to remove a specific element from the list.
fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
print(fruits)

4.
In Python, which module is commonly used for array operations?
list
array
numpy
math
Answer: Option
Explanation:
The numpy module is commonly used for efficient array operations in Python.

5.
How do you create an array in Python using the array module?
arr = array.create([1, 2, 3])
arr = array([1, 2, 3])
arr = array.array([1, 2, 3])
arr = create.array([1, 2, 3])
Answer: Option
Explanation:
The array module provides the array() function to create arrays in Python.