All Exams  >   Software Development  >   Basics of Python  >   All Questions

All questions of 2D Arrays for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

Which of the following statements about 2D arrays in Python is true?
  • a)
    2D arrays are always rectangular in shape.
  • b)
    2D arrays can have different data types in each row.
  • c)
    2D arrays are not supported in Python.
  • d)
    2D arrays can only store numerical values.
Correct answer is option 'B'. Can you explain this answer?

2D arrays in Python

Introduction:
A 2D array, also known as a two-dimensional array or matrix, is a collection of elements organized in a grid-like structure. Each element in a 2D array can be accessed by specifying its row and column indices. In Python, 2D arrays are typically implemented using nested lists or NumPy arrays.

Statement analysis:
Let's analyze each statement to determine which one is true:

a) 2D arrays are always rectangular in shape.
This statement is not true. 2D arrays can have a rectangular shape, but they can also have irregular shapes. In Python, 2D arrays implemented using nested lists can have rows of different lengths, resulting in a jagged or irregular shape.

b) 2D arrays can have different data types in each row.
This statement is true. In Python, each element in a 2D array can have a different data type. For example, one row of a 2D array can contain integers, while another row can contain strings. This flexibility allows for more diverse and complex data structures.

c) 2D arrays are not supported in Python.
This statement is not true. Python supports 2D arrays through various data structures, such as nested lists or NumPy arrays. These data structures provide the necessary functionality to work with 2D arrays in Python.

d) 2D arrays can only store numerical values.
This statement is not true. 2D arrays in Python can store any data type, not just numerical values. You can have 2D arrays that store integers, strings, booleans, or even other arrays.

Conclusion:
Based on the analysis, the correct statement is option B: "2D arrays can have different data types in each row." In Python, 2D arrays can have a rectangular or irregular shape, and they can store any data type in each element. This flexibility makes 2D arrays a powerful tool for representing and manipulating complex data structures in Python.

What is the time complexity for accessing an element in a 2D array?
  • a)
    O(1)
  • b)
    O(n)
  • c)
    O(log n)
  • d)
    O(n2)
Correct answer is option 'A'. Can you explain this answer?

Time Complexity for Accessing an Element in a 2D Array

When it comes to accessing an element in a 2D array, the time complexity can vary depending on how the array is implemented. However, assuming we have a traditional 2D array, the time complexity for accessing an element is O(1), which means it takes constant time regardless of the size of the array.

Explanation:
To understand why the time complexity is O(1), let's consider how a 2D array is stored in memory.

- In a 2D array, elements are arranged in rows and columns.
- The array is essentially a contiguous block of memory, where each element is stored next to each other.
- The elements are accessed using their indices, which correspond to the row and column they belong to.

Accessing an Element:
To access an element in a 2D array, we use the indices of the row and column where the element is located. The steps involved in accessing an element are as follows:

1. Calculate the memory address of the element using the row and column indices.
2. Access the element at the calculated memory address.

Since the array is stored as a contiguous block of memory, calculating the memory address of an element is a simple arithmetic operation. It involves multiplying the row index by the number of columns and adding the column index.

Constant Time Complexity:
Regardless of the size of the 2D array, the calculation of the memory address and the access operation both take a constant amount of time.

- The calculation of the memory address involves basic arithmetic operations, which are considered constant time operations.
- The access operation involves directly accessing the memory location where the element is stored, which is also a constant time operation.

Therefore, the time complexity for accessing an element in a 2D array is constant, denoted as O(1). It does not depend on the size of the array, making it an efficient operation.

What will be the output of the following code snippet?
array = [[1, 2, 3], [4, 5, 6]]
print(sum(array[0]))
  • a)
    6
  • b)
    10
  • c)
    15
  • d)
    21
Correct answer is option 'A'. Can you explain this answer?

Explanation:

The output of the code snippet will be 6.

To understand why, let's break down the code:

1. The variable "array" is assigned a 2-dimensional list: [[1, 2, 3], [4, 5, 6]]. This is a list of two sublists, each containing three elements.

2. The "sum()" function is used to calculate the sum of the elements in the first sublist of "array" (array[0]). In this case, the first sublist is [1, 2, 3].

3. The sum of the elements in [1, 2, 3] is 1 + 2 + 3 = 6.

Therefore, the output of the code snippet is 6.

What is the output of the following code snippet?
arr = [[1, 2], [3, 4], [5, 6]]
print([arr[i][j] for i in range(len(arr)) for j in range(len(arr[i]))])
  • a)
    [1, 2, 3, 4, 5, 6]
  • b)
    [1, 3, 5, 2, 4, 6]
  • c)
    [[1, 2], [3, 4], [5, 6]]
  • d)
    [[1, 3, 5], [2, 4, 6]]
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Given Code:
arr = [[1, 2], [3, 4], [5, 6]]
print([arr[i][j] for i in range(len(arr)) for j in range(len(arr[i]))])

Explanation:

Nested List Comprehension:
- The given code snippet uses nested list comprehension to iterate over each element in the 2D list 'arr'.
- The outer loop iterates over each sublist in 'arr' using 'i'.
- The inner loop iterates over each element in the sublist using 'j'.
- The list comprehension extracts each element from the sublist using the indices 'i' and 'j' and stores them in a new list.

Output:
- The output of the code snippet is [1, 2, 3, 4, 5, 6].
- This is because the list comprehension extracts each element from the 2D list 'arr' in a row-wise manner.
- The elements are extracted in the order they appear in the 2D list.
Therefore, the correct answer is option 'A' [1, 2, 3, 4, 5, 6].

What is the output of the following code snippet?
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(arr[1:])
  • a)
    [[4, 5, 6]]
  • b)
    [[2, 3], [5, 6], [8, 9]]
  • c)
    [[4, 5, 6], [7, 8, 9]]
  • d)
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Correct answer is option 'A'. Can you explain this answer?

Output Explanation:

The code snippet provided creates a 2-dimensional list called 'arr' with three sublists. Each sublist contains three elements.

The line 'print(arr[1:])' is using slicing to retrieve a sublist from index 1 to the end of the list 'arr'.

The output of this code snippet will be:

[[4, 5, 6], [7, 8, 9]]

Explanation:
- The slicing notation 'arr[1:]' retrieves all elements starting from index 1 to the end of the list 'arr'.
- In Python, indexing starts from 0, so 'arr[1:]' includes the sublist at index 1 and all the sublists after it.
- Therefore, the output is a new list containing the sublists: '[4, 5, 6]' and '[7, 8, 9]'.

Summary:
The output of the code snippet is [[4, 5, 6], [7, 8, 9]]. Using slicing with 'arr[1:]' retrieves all sublists starting from index 1 to the end of the list 'arr'.

What will be the output of the following code snippet?
arr = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
print(len(arr[1]))
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'D'. Can you explain this answer?

Code Explanation:
The given code snippet initializes a 2-dimensional list called "arr" with three sublists. Each sublist contains a different number of elements.
The second line of code prints the length of the second sublist in the "arr" list.

Code Execution:
The code will execute in the following steps:

1. The list "arr" is initialized with three sublists:
- The first sublist [1, 2] contains 2 elements.
- The second sublist [3, 4, 5] contains 3 elements.
- The third sublist [6, 7, 8, 9] contains 4 elements.

2. The print statement is executed to display the length of the second sublist, arr[1].
- The index 1 refers to the second sublist [3, 4, 5].
- The len() function returns the number of elements in the sublist.
- Therefore, the output will be the length of the second sublist, which is 3.

Output:
The output of the code snippet will be:
3

What will be the output of the following code snippet?
array = [[1, 2, 3], [4, 5, 6]]
print(max(array[1]))
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    6
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The max() function returns the maximum value from the given list. In this case, it returns 6, which is the maximum value in the second row of the 2D array.

What will be the output of the following code snippet?
array = [[1, 2], [3, 4]]
print(len(array))
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The len() function returns the number of elements in the given list. In this case, it returns 2, which is the number of rows in the 2D array.

What is a 2D array in Python?
  • a)
    A collection of elements of different data types
  • b)
    A list of lists, where each inner list represents a row of elements
  • c)
    An array with two dimensions
  • d)
    A dictionary with two keys and values
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
In Python, a 2D array is typically represented as a list of lists. It is a data structure that allows you to store elements in a grid-like format with rows and columns. Each inner list within the main list represents a row of elements, and the length of each inner list determines the number of columns in the 2D array.

What is the time complexity to access an element in a 2D array with n rows and m columns?
  • a)
    O(1)
  • b)
    O(n)
  • c)
    O(m)
  • d)
    O(n*m)
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
Accessing an element in a 2D array with n rows and m columns takes constant time, O(1), as the indexing operation is direct.

What is the correct way to access an element in a 2D array in Python?
  • a)
    array[i, j]
  • b)
    array[i][j]
  • c)
    array[i][j] = value
  • d)
    array(i, j)
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
To access an element in a 2D array, we use the syntax array[i][j], where i represents the row index and j represents the column index.

Chapter doubts & questions for 2D Arrays - Basics of Python 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of 2D Arrays - Basics of Python in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of Python

49 videos|38 docs|18 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev