All Exams  >   Software Development  >   Basics of C++  >   All Questions

All questions of 2D Arrays for Software Development Exam

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

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

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.

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

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.

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.

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

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 - Basics of C++ 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 C++ 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 C++

70 videos|45 docs|15 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