Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 8 Multidimensional Arrays


Section 8.2 Declaring Two-Dimensional Arrays
8.1  Which of the following statements are correct?
A. char charArray[][] = {'a', 'b'};
B. char charArray[2][2] = {{'a', 'b'}, {'c', 'd'}};
C. char charArray[2][] = {{'a', 'b'}, {'c', 'd'}};
D. char charArray[][] = {{'a', 'b'}, {'c', 'd'}};

8.2  When you declare an array using the following statement inside a function, the element values are automatically initialized to 0.

int matrix[5][5];
A. True
B. False

8.3  How many elements are array matrix[5][5]?
A. 14
B. 20
C. 25
D. 30

8.4  What is the index variable for the element at the first row and first column in array a?
A. a[0][0]
B. a[1][1]
C. a[0][1]
D. a[1][0]

Section 8.3 Processing Two-Dimensional Arrays
8.5  What is the output of the following code?

 #include <iostream>
 using namespace std;

 int main()
 {
   int matrix[4][4] =
     {{1, 2, 3, 4},
      {4, 5, 6, 7},
      {8, 9, 10, 11},
      {12, 13, 14, 15}};

   for (int i = 0; i < 4; i++)
     cout << matrix[i][1] << " ";

   return 0;
 }
A. 1 2 3 4
B. 4 5 6 7
C. 1 3 8 12
D. 2 5 9 13
E. 3 6 10 14

8.6  What is the output of the following code?

 #include <iostream>
 using namespace std;

 int main()
 {
   int matrix[4][4] =
     {{1, 2, 3, 4},
      {4, 5, 6, 7},
      {8, 9, 10, 11},
      {12, 13, 14, 15}};

   for (int i = 0; i < 4; i++)
     cout << matrix[1][i] << " ";

   return 0;
 }
A. 1 2 3 4
B. 4 5 6 7
C. 1 3 8 12
D. 2 5 9 13
E. 3 6 10 14

8.7  What is the prinout of the following program?

 #include <iostream>
 using namespace std;

 int main()
 {
   int values[2][4] = {{3, 4, 5, 1}, {33, 6, 1, 2}};

   int v = values[0][0];
   for (int row = 0; row < 2; row++)
     for (int column = 0; column < 4; column++)
       if (v < values[row][column])
         v = values[row][column];

   cout << v << endl;

   return 0;
 }
A. 1
B. 3
C. 5
D. 6
E. 33

Section 8.4 Passing Two-Dimensional Arrays to Functions
8.8  Which of the following function declaration is correct?
A. int f(int[][] a, int rowSize, int columnSize);
B. int f(int a[][], int rowSize, int columnSize);
C. int f(int a[][3], int rowSize);
D. int f(int a[3][], int rowSize);

8.9  What is the prinout of the following program?

 #include <iostream>
 using namespace std;

 int m(int list[], int numberOfElements)
 {
   int v = list[0];
   for (int i = 1; i < numberOfElements; i++)
     if (v < list[i])
       v = list[i];
   return v;
 }

 int main()
 {
   int values[2][4] = {{3, 4, 5, 1}, {33, 6, 1, 2}};

   for (int row = 0; row < 2; row++)
   {
     cout << m(values[row], 4) << " ";
   }


   return 0;
 }
A. 3 33
B. 1 1
C. 5 6
D. 5 33
E. 33 5

Section 8.8 Multidimensional Arrays
8.10  Which of the following statements are correct?
A. int data[2][2][];
B. int data[2][2][2];
C. int data[][][] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
D. int data[][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
E. int data[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};

8.11  What will be displayed by the following code?

 #include <iostream>
 using namespace std;

 int main()
 {
   int data[][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};

   cout << data[1][0][0] << endl;

   return 0;
 }
A. 1
B. 2
C. 4
D. 5
E. 6

8.12  What will be displayed by the following code?

 #include <iostream>
 using namespace std;

 int ttt(int m[][2])
 {
   int v = m[0][0];

   for (int i = 0; i < 2; i++)
     for (int j = 0; j < 2; j++)
       if (v < m[i][j])
         v = m[i][j];

   return v;
 }

 int main()
 {
   int data[][2][2] = {{{1, 2}, {3, 4}},
      {{5, 6}, {7, 8}}};

   cout << ttt(data[0]) << endl;

   return 0;
 }
A. 1
B. 2
C. 4
D. 5
E. 6

8.13  What will be displayed by the following code?

 #include <iostream>
 using namespace std;

 const int COLUMN_SIZE = 3;

 double sum(const double m[][COLUMN_SIZE], int rowSize)
 {
   int sum = 0;

   for (int i = 0; i < rowSize; i++)
     sum += m[i][i];

   return sum;
 }

 int main()
 {
   double m[3][3] = {{1, 2, 3}, {1.5, 2.5, 3.5}, {0.1, 0.1, 0.1}};
   cout << "\nSum of all elements is " << sum(m,3) << endl;

   return 0;
 }
A. 4.5
B. 4
C. 3
D. 3.6
E. 3.0