All Exams  >   EmSAT Achieve  >   C++ for EmSAT Achieve  >   All Questions

All questions of 2D Arrays for EmSAT Achieve Exam

Which of the following operations can be performed on 2D arrays?
  • a)
    Addition and subtraction
  • b)
    Multiplication and division
  • c)
    Concatenation and splitting
  • d)
    Sorting and searching
Correct answer is option 'A'. Can you explain this answer?

Addition and subtraction can be performed on 2D arrays.

Explanation:
- 2D arrays, also known as matrices, are rectangular arrangements of elements organized in rows and columns.
- Addition and subtraction are arithmetic operations that can be performed on 2D arrays.
- When adding or subtracting two 2D arrays, the corresponding elements in the arrays are added or subtracted.
- For example, if we have two 2D arrays A and B, the sum of A and B, denoted as A + B, is a new 2D array where each element is the sum of the corresponding elements in A and B.
- Similarly, the difference of A and B, denoted as A - B, is a new 2D array where each element is the difference of the corresponding elements in A and B.
- The addition and subtraction operations can be performed on any two 2D arrays as long as they have the same dimensions (i.e., the same number of rows and columns).
- The resulting 2D array will have the same dimensions as the original arrays.
- The addition and subtraction operations are useful in various applications, such as mathematical calculations, image processing, and data analysis.
- They allow us to combine or compare the values in different 2D arrays to obtain meaningful results.
- In contrast, multiplication and division are not defined for 2D arrays in the same way as they are for scalar values or vectors.
- Concatenation and splitting are operations that involve combining or separating different parts of 2D arrays, and they are not directly related to arithmetic operations.
- Sorting and searching are operations that involve arranging the elements of a 2D array in a specific order or finding specific elements in the array, but they do not involve arithmetic operations.

What will be the output of the following code?
#include <iostream>
int main() {
   int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
   std::cout << arr[2][0] / arr[1][1];
   return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code initializes a 2D array 'arr' with values. The expression 'arr[2][0] / arr[1][1]' divides the value at row 2, column 0 (which is 7) by the value at row 1, column 1 (which is 5). The result is 1, and the output will be 1.

How do you initialize a 2D array with values in C++?
  • a)
    int array[] = {1, 2, 3; 4, 5, 6};
  • b)
    int array[][] = {1, 2, 3; 4, 5, 6};
  • c)
    int array[][] = {{1, 2, 3}, {4, 5, 6}};
  • d)
    int array[] = {{1, 2, 3}, {4, 5, 6}};
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
To initialize a 2D array with values in C++, we use the syntax 'int array[][] = {{1, 2, 3}, {4, 5, 6}};', where each inner array represents a row and the values inside the inner arrays represent the elements of each row.

Given the following 2D array, what will be the output of the code?
#include <iostream>
int main() {
   int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
   std::cout << arr[1] - arr[0];
   return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    Compilation error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code subtracts 'arr[0]' (which is a pointer to the first row) from 'arr[1]' (which is a pointer to the second row). Subtracting two pointers of the same type is not allowed in C++, so it results in a compilation error.

How do you declare a 2D array in C++?
  • a)
    int array[][;
  • b)
    int[][] array;
  • c)
    int array[][];
  • d)
    int array[];
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
In C++, the syntax for declaring a 2D array is 'int array[][]'; or 'int array[rowSize][colSize]';, where 'rowSize' and 'colSize' are the desired sizes of the array.

What is the maximum number of dimensions a C++ array can have?
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    There is no maximum limit
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
C++ does not impose a maximum limit on the number of dimensions an array can have. However, the practical limit depends on the available memory and the capabilities of the compiler.

How can you find the number of rows in a 2D array in C++?
  • a)
    sizeof(array) / sizeof(array[0])
  • b)
    array.length
  • c)
    array.size()
  • d)
    There is no direct way to find the number of rows
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
In C++, there is no direct way to find the number of rows in a 2D array. You need to keep track of the row size separately or use a row size constant if the size is fixed.

Given the following 2D array, what will be the output of the code?
#include <iostream>
int main() {
   int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
   std::cout << *(arr[1] + 2);
   return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    6
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code initializes a 2D array 'arr' with values. The expression '*(arr[1] + 2)' accesses the element at index 2 in the second row (row index 1). This is equivalent to 'arr[1][2]', which is 6. The output will be 6.

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

Sonal Yadav answered
The time complexity of accessing an element in a 2D array by its index is constant, O(1), as the index directly determines the memory location of the element.

What is the syntax for accessing an element in a 2D array?
  • a)
    array[row, column]
  • b)
    array(row, column)
  • c)
    array[row][column]
  • d)
    array(row)(column)
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
To access an element in a 2D array, we use the syntax 'array[row][column]', where 'row' and 'column' are the indices of the desired element.

What will be the output of the following code?
#include <iostream>
int main() {
   int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
   std::cout << arr[1][2] - arr[0][1];
   return 0;
}
  • a)
    2
  • b)
    3
  • c)
    4
  • d)
    5
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code initializes a 2D array 'arr' with values. The expression 'arr[1][2] - arr[0][1]' subtracts the value at row 0, column 1 (which is 2) from the value at row 1, column 2 (which is 6). The result is 4, and the output will be 4.

What will be the output of the following code?
#include <iostream>
int main() {
   int arr[2][2] = {{1, 2}, {3, 4}};
   std::cout << arr[1][1] + arr[0][0];
   return 0;
}
  • a)
    2
  • b)
    3
  • c)
    4
  • d)
    5
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code initializes a 2D array 'arr' with values. The expression 'arr[1][1] + arr[0][0]' adds the values at row 1, column 1 (which is 4) and row 0, column 0 (which is 1). The result is 5, and the output will be 5.

Given the following 2D array, what will be the output of the code?
#include <iostream>
int main() {
   int arr[3][2] = {{1, 2}, {3, 4}, {5, 6}};
   std::cout << *(*(arr + 2) + 1);
   return 0;
}
  • a)
    1
  • b)
    2
  • c)
    5
  • d)
    6
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code initializes a 2D array 'arr' with values. The expression '*(*(arr + 2) + 1)' accesses the element at row 2 (row index 2) and column 1 (column index 1). This is equivalent to 'arr[2][1]', which is 6. The output will be 6.

What will be the output of the following code?
#include <iostream>
int main() {
   int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
   std::cout << arr[2][1] * arr[0][2];
   return 0;
}
  • a)
    2
  • b)
    6
  • c)
    15
  • d)
    21
Correct answer is option 'D'. Can you explain this answer?

Sonal Yadav answered
The code initializes a 2D array 'arr' with values. The expression 'arr[2][1] * arr[0][2]' multiplies the value at row 2, column 1 (which is 8) with the value at row 0, column 2 (which is 3). The result is 24, and the output will be 24.

Given the following 2D array, what will be the output of the code?
#include <iostream>
int main() {
   int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
   std::cout << sizeof(arr) / sizeof(arr[0]);
   return 0;
}
  • a)
    2
  • b)
    3
  • c)
    6
  • d)
    12
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The code uses the 'sizeof' operator to calculate the total size of the 2D array 'arr'. The expression 'sizeof(arr)' returns the total size of the array in bytes. By dividing it by the size of each row ('sizeof(arr[0])'), we get the number of rows. In this case, 'sizeof(arr) / sizeof(arr[0]') is equivalent to '6 / 4', which is 2. The output will be 2.

Given the following 2D array, what will be the output of the code?
#include <iostream>
int main() {
   int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
   std::cout << sizeof(arr[0]) / sizeof(int);
   return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    Compilation error
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The code uses the 'sizeof' operator to calculate the size of 'arr[0]', which represents the first row of the 2D array 'arr'. The expression 'sizeof(arr[0])' returns the size of the first row in bytes. By dividing it by the size of an 'int' (sizeof(int)), we get the number of elements in the row. In this case, 'sizeof(arr[0]) / sizeof(int)' is equivalent to '12 / 4', which is 3. The output will be 3.

Chapter doubts & questions for 2D Arrays - C++ 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 - C++ 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.

C++ for EmSAT Achieve

71 videos|45 docs|15 tests

Top Courses EmSAT Achieve