Table of contents | |
Multiple Choice Questions (MCQs) | |
High Order Thinking Questions (HOTS) | |
Fill in the Blanks | |
True or False | |
Hands-On Questions |
Q.1. Which of the following is the correct way to create an array in Python?
(a) array = [1, 2, 3]
(b) array = (1, 2, 3)
(c) array = {1, 2, 3}
(d) array = "1, 2, 3"
Ans. (a)
Q.2. How are arrays stored in memory in Python?
(a) Contiguous blocks of memory
(b) Linked list
(c) Randomly scattered memory locations
(d) Heap memory
Ans. (a)
Q.3. Which library in Python is commonly used for working with arrays?
(a) NumPy
(b) Math
(c) Pandas
(d) Scikit-learn
Ans. (a)
Q.4. How can you obtain the number of elements in an array in Python?
(a) len(array)
(b) array.length()
(c) array.size()
(d) array.count()
Ans. (a)
Q.5. What is the index of the last element in an array with n elements?
(a) n-1
(b) n
(c) n+1
(d) n/2
Ans. (a)
Q.1. Write a Python program to find the sum of all elements in an array.
array = [1, 2, 3, 4, 5]
sum_of_elements = sum(array)
print("Sum of elements:", sum_of_elements)
Q.2. Write a Python program to find the maximum and minimum element in an array.
array = [1, 5, 3, 2, 4]
max_element = max(array)
min_element = min(array)
print("Maximum element:", max_element)
print("Minimum element:", min_element)
Q.3. Write a Python program to find the average of elements in an array.
array = [1, 2, 3, 4, 5]
average = sum(array) / len(array)
print("Average:", average)
Q.4. Write a Python program to count the number of occurrences of a specific element in an array.
array = [1, 2, 3, 2, 4, 2, 5]
element = 2
count = array.count(element)
print("Number of occurrences:", count)
Q.5. Write a Python program to reverse an array.
array = [1, 2, 3, 4, 5]
reversed_array = array[::-1]
print("Reversed array:", reversed_array)
Fill in the Blanks
Q.1. In Python, arrays are created using the _______ module.
Ans. array
Q.2. The index of the first element in an array is _______.
Ans. 0
Q.3. Arrays in Python can store elements of _______ data types.
Ans. different
Q.4. The process of creating a new array with the same elements as an existing array is called _______.
Ans. copying
Q.5. Arrays in Python are _______-based, which means the first element has an index of 0.
Ans. 0-based
True or False
Q.1. Arrays in Python can store elements of different data types. (True/False)
Ans. False
Q.2. The size of an array in Python is fixed once it is created. (True/False)
Ans. True
Q.3. Arrays can be resized dynamically in Python. (True/False)
Ans. True
Q.4. NumPy is a library in Python that provides support for large, multi-dimensional arrays and matrices. (True/False)
Ans. True
Q.5. In Python, passing an array to a function requires making a copy of the array. (True/False)
Ans. False
Hands-On Questions
Q.1. Write a Python program to create an array of 5 numbers entered by the user and display the sum of all elements.
array = []
for _ in range(5):
number = int(input("Enter a number: "))
array.append(number)
sum_of_elements = sum(array)
print("Sum of elements:", sum_of_elements)
Q.2. Write a Python program to create an array of 10 integers entered by the user and find the average of the positive numbers.
array = []
for _ in range(10):
number = int(input("Enter an integer: "))
array.append(number)
positive_numbers = [x for x in array if x > 0]
average = sum(positive_numbers) / len(positive_numbers)
print("Average of positive numbers:", average)
Q.3. Write a Python program to copy the elements of one array into another array.
array1 = [1, 2, 3, 4, 5]
array2 = array1.copy()
print("Copied array:", array2)
Q.4. Write a Python function that takes an array as a parameter and returns the maximum element in the array.
def find_maximum(array):
max_element = max(array)
return max_element
array = [1, 5, 3, 2, 4]
maximum = find_maximum(array)
print("Maximum element:", maximum)
Q.5. Write a Python function that takes a matrix (2D array) as a parameter and returns the sum of all elements in the matrix.
def sum_of_matrix(matrix):
total = 0
for row in matrix:
for element in row:
total += element
return total
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum_matrix = sum_of_matrix(matrix)
print("Sum of matrix elements:", sum_matrix)
49 videos|38 docs|18 tests
|
|
Explore Courses for Software Development exam
|