Introduction to C++ Programming, Y. Daniel Liang

Chapter 6 Arrays


Sections 6.2 Array Basics
1  What is the representation of the third element in an array called a?

A. a[2]
B. a(2)
C. a[3]
D. a(3)

2  Which of the following is incorrect?

A. int a[2];
B. int a[];
C. int a(2);
D. int a = new int[2];
E. int a() = new int[2];

3  What is the correct term for numbers[99]?

A. index
B. index variable
C. indexed variable
D. array variable
E. array

4  Suppose int i = 5, which of the following can be used as an index for array double t[100]?

A. i
B. rand() % 100
C. i + 10
D. i + 6.5
E. rand() / 100

5  Which of the following statements are true?

A. Every element in an array has the same type.
B. The array size is fixed after it is created.
C. The array size used to declare an array must be a constant expression.
D. The array elements are initialized when an array is created.

6  Analyze the following code.

int main()
{
   int x[3];
   cout << "x[0] is " << x[0];
}

A. The program has a syntax error because the size of the array wasn't specified when declaring the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x[0] is 0.
D. The program has a runtime error because the array element x[0] is not defined.
E. x[0] has an arbitrary value.

7  Which of the following statements is valid?

A. int i(30);
B. double d[30];
C. int i[] = {3, 4, 3, 2};
D. int[] i = {3, 4, 3, 2};
E. int i[4] = {3, 4, 3, 2};

8  How can you initialize an array of two characters to 'a' and 'b'?

A. char[] charArray = {'a', 'b'};
B. char[2] charArray = {'a', 'b'};
C. char charArray[] = {'a', 'b'};
D. char charArray[2] = {'a', 'b'};

9  Given the following two arrays:

char s1[] = {'a', 'b', 'c'};
char s2[] = "abc";

Which of the following statements is correct?

A. s1 has three characters
B. s2 has three characters
C. s1 has four characters
D. s2 has four characters

10  Which of the following statements are correct?

A. A string literal is a C-string.
B. A C-string is a sequence of characters ending with a null terminator.
C. An array of characters is a C-string.
D. A C-string is a sequence of characters.

11  Assume int t[] = {1, 2, 3, 4}. What is t[4]?

A. 0
B. 3
C. 4
D. 5
E. accessing t[4] may result in a runtime error

12  Analyze the following code:

  int main()
  {
     int x[5];
     int i;
     for (i = 0; i < 5; i++)
       x[i] = i;
     cout << x[i] << " ";
  }


A. The program displays 0 1 2 3 4.
B. The program displays 4.
C. The program may have a runtime error because the last statement in the main function has the out of bound index for the array.
D. The program has a syntax error because i is not defined in the last statement in the main function.

13  (Tricky) What is the output of the following code:

  #include <iostream>
  using namespace std;

  int main()
  {
    int x[] = {120, 200, 016};
    for (int i = 0; i < 3; i++)
      cout << x[i] << " ";
  }


A. 120 200 16
B. 120 200 14
C. 120 200 20
D. 016 is a syntax error. It should be written as 16.

14  Can you copy an array as follows:

   int x[] = {120, 200, 016};
   int y[3];

   y = x;


A. Yes
B. No

Section 6.4 Passing Arrays to Functions
15  When you pass an array to a function, the function receives __________.

A. a copy of the array
B. a copy of the first element
C. the reference of the array
D. the length of the array

16  Show the output of the following code:

  #include <iostream>
  using namespace std;

  void increase(int x[], int size)
  {
    for (int i = 0; i < size; i++)
      x[i] ++;
  }

  void increase(int y)
  {
    y++;
  }

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

    increase(x, 5);

    int y[] =
    {
      1, 2, 3, 4, 5
    };

    increase(y[0]);

    cout << x[0] << " " << y[0];
  }


A. 0 0
B. 1 1
C. 2 2
D. 2 1
E. 1 2

17  Analyze the following code:

   #include <iostream>
   using namespace std;
  
   void reverse(int list[], const int size, int newList[])
   {
     for (int i = 0; i < size; i++)
       newList[i] = list[size - 1 - i];
   }
  
   int main()
   {
     int list[] = {1, 2, 3, 4, 5};
     int newList[5];
  
     reverse(list, 5, newList);
     for (int i = 0; i < 5; i++)
       cout << newList[i] << " ";
   }

A. The program displays 1 2 3 4 6.
B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.
C. The program displays 5 4 3 2 1.
D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

Section 6.10 Two-Dimensional Arrays
18  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'}};

19  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);