All Exams  >   EmSAT Achieve  >   Python for EmSAT Achieve  >   All Questions

All questions of 2D Arrays for EmSAT Achieve Exam

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 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?

Sonal Yadav answered
Accessing an element in a 2D array takes constant time because the position of the element is known based on the row and column indices.

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 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 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 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.

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.

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

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

Python for EmSAT Achieve

57 videos|39 docs|18 tests

Top Courses EmSAT Achieve