Assignment: 2D Array

# Assignment: 2D Array | Basics of Python - Software Development PDF Download

 Table of contents Multiple Choice Questions (MCQs) HOTS (Higher Order Thinking Questions): Fill in the blanks True or False Hands-On Questions

## Multiple Choice Questions (MCQs)

Q.1. What is a 2D array in Python?
(a) A list of lists
(b) A matrix with two dimensions
(c) A collection of elements arranged in rows and columns
(d) All of the above
Ans. (d)

Q.2. How do you access an element in a 2D array in Python?
(a) Using a single index
(b) Using two indices
(c) Using the 'get()' function
(d) Accessing is not possible in a 2D array
Ans. (b)

Q.3. Which of the following methods is used to create a 2D array in Python?
(a) Using the 'array()' function from the NumPy library
(b) Using the 'list()' function
(c) Using the 'tuple()' function
(d) Using the 'dictionary()' function
Ans. (a)

Q.4. Which of the following statements is true about a 2D array in Python?
(a) The number of rows and columns can be different
(b) All elements in a 2D array must have the same data type
(c) A 2D array can have more than two dimensions
(d) A 2D array can only be created using the 'numpy' module
Ans. (b)

Q.5. What is the output of the following code?

my_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print(my_array[1][2])

(a) 1
(b) 2
(c) 5
(d) 6
Ans. (c)

## HOTS (Higher Order Thinking Questions):

Q.1. Write a Python function that takes a 2D array as input and returns the sum of all the elements in the array.

def sum_2d_array(arr):

total_sum = 0

for row in arr:

total_sum += sum(row)

Q.2. Write a Python program to find the row with the highest sum in a given 2D array.

def highest_sum_row(arr):

max_sum = float('-inf')

max_row = None

for row in arr:

row_sum = sum(row)

if row_sum > max_sum:

max_sum = row_sum

max_row = row

return max_row

Q.3. Write a Python program to transpose a given 2D array (swap rows with columns).

def transpose_2d_array(arr):

transposed_arr = [[arr[j][i] for j in range(len(arr))] for i in range(len(arr[0]))]

return transposed_arr

Q.4. Write a Python program to check if a given 2D array is symmetric (a[i][j] == a[j][i] for all elements).

def is_symmetric(arr):

for i in range(len(arr)):

for j in range(len(arr[0])):

if arr[i][j] != arr[j][i]:

return False

return True

Q.5. Write a Python program to rotate a given 2D array by 90 degrees clockwise.

def rotate_2d_array(arr):

rotated_arr = [[arr[j][i] for j in range(len(arr) - 1, -1, -1)] for i in range(len(arr[0]))]

return rotated_arr

## Fill in the blanks

1. A 2D array is a collection of elements arranged in ______ and ______.

Ans. rows, columns

2. The number of rows and columns in a 2D array is called its ______ and ______, respectively.

Ans. rows, columns

3. To access an element in a 2D array, we use ______ indices.

Ans. two

4. The function used to create a 2D array in Python is ______.

Ans. array()

5. The process of swapping rows with columns in a 2D array is called ______.

Ans. transposition

## True or False

1. In Python, all elements in a 2D array must have the same data type.

Ans. True

2. A 2D array can have a different number of rows and columns.

Ans. True

3. Accessing an element in a 2D array requires using two indices.

Ans. True

4. The 'numpy' module is required to create a 2D array in Python.

Ans. False

5. The process of swapping rows with columns in a 2D array is called transposition.

Ans. True

## Hands-On Questions

Q.1. Write a Python program to create a 3x3 2D array and print its elements.

my_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for row in my_array:

print(row)

Q.2. Write a Python program to calculate the sum of all the elements in a given 2D array.

def sum_2d_array(arr):

total_sum = 0

for row in arr:

total_sum += sum(row)

my_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print(sum_2d_array(my_array))

Q.3. Write a Python program to find the maximum value in each row of a given 2D array.

def max_value_each_row(arr):

max_values = []

for row in arr:

max_values.append(max(row))

return max_values

my_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print(max_value_each_row(my_array))

Q.4. Write a Python program to check if a given 2D array is a square matrix (number of rows equals number of columns).

def is_square_matrix(arr):

num_rows = len(arr)

num_columns = len(arr[0])

return num_rows == num_columns

my_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print(is_square_matrix(my_array))

Q.5. Write a Python program to multiply two given 2D arrays element-wise.

def multiply_2d_arrays(arr1, arr2):

if len(arr1[0]) != len(arr2):

return "Matrices cannot be multiplied"

result = [[0 for _ in range(len(arr2[0]))] for _ in range(len(arr1))]

for i in range(len(arr1)):

for j in range(len(arr2[0])):

for k in range(len(arr2)):

result[i][j] += arr1[i][k] * arr2[k][j]

return result

array1 = [[1, 2], [3, 4]]

array2 = [[5, 6], [7, 8]]

print(multiply_2d_arrays(array1, array2))

The document Assignment: 2D Array | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development

## Basics of Python

49 videos|38 docs|18 tests

## Basics of Python

49 videos|38 docs|18 tests

### Up next

 Explore Courses for Software Development exam

### Top Courses for Software Development

Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

;