Self-test 6:
Array

  1. Consider the following program. Which of the following is true?

    #include <iostream>
    using namespace std;
    int main(void)
    {
        int n = 0;
        int i = n+1;
        int z[10] = {1,2,3,4,5,6,7,8,9,10};
    
        for (i = 0; i <= 5; i++)
        {
            cout << z[2*i+1] << " " << 2*i+1 << endl;
        }
    
        return 0;
    }
    
    1. The program fails to compile.
    2. The output of the program is:
      2 1
      4 3
      6 5
      8 7
      10 9
      
    3. We cannot determine the output of the program definitely.
    4. The program outputs the 10 elements in array z.
    Solution

    C
    While the for loop iterates from i = 0 to 5, the values of elements z[1], z[3], z[5], z[7], z[9], z[11] should be displayed. However, since the size of the array z is only 10, the index 11 is out of bound. We cannot guarantee what happens when the compute tries to display the value of z[11]; if the location of z[11] does not belong to the user, it will give the runtime error of "segmentation fault".


  2. What is the output of the following program segment?

    char a[7] = "ABC8900";
    int i = 0;
    while (i*2 < 8)
    {
        cout << a[i];
        i++;
    }
    
    1. AC90
    2. B80
    3. Segmentation error
    4. Cannot even compile
    Solution

    D.
    The character string "ABC8900" is equivalent to {'A','B','C','8','9','0','0','\0'}. Thus, "ABC8900" actually consists of 8 characters, and the a array, which has a size of 7, is not big enough to store "ABC8900". Initializing an array with more elements than its size is a compilation error.


  3. What is the output of the following program segment?

    char a[6][4] = {"ABC", "abc", "123", "789", "689", "123"};
    
    for (int i = 2; i >= 0; i--)
    {
        for (int j = 0; j < 3; j++)
            cout << a[2*i][j];
    
        cout << endl;
    }
    
    1.  
      689
      123
      ABC
      
    2.  
      abc
      789
      123
      
    3.  
      ABC
      abc
      123
      
    4. Segmentation error
    Solution

    A.
    The declaration: char a[6][4] represents a list of 6 words, each with a maximum length of 3-character long. The outer i loop picks the words to print, while the inner j picks the characters to print in each word. The expression: 2*i picks the words with index = 4, 2, 0 since i iterates from 2 to 0. Since j iterates from 0 to 2, all the 3 characters of each words are printed.


  4. Which of the function(s) below will never produce segmentation fault at runtime?

    void boo1 (const char x[3][20])
    {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 20; j++)
                cout << x[j][i];
    }
    
    void boo2 (const char x[3][20])
    {
        for (int i = 0; i < 20; i++)
            for (int j = 0; j < 3; j++)
                cout << x[j][i];
    }
    
    void boo3 (const char x[3][20])
    {
        for (int i = 1; i <= 6; i++)
            for (int j = 0; j < 3; j++)
                cout << x[j][i*2-1];
    }
    
    1. boo1 & boo2
    2. boo2 & boo3
    3. boo1 & boo3
    4. None of the above
    Solution

    B.
    In the output statement of boo1, the first index of x (which is j) iterates from 0 to 19, but the size of the first dimension of the x array is only 3. So the index j will soon get out of bound, causing the segmentation fault runtime error.


  5. Which of the function(s) cannot be compiled?

    void foo1 (const char x[10][10])
    {
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
                cout << x[i][j];
    }
    
    void foo2 (const char x[][10])
    {
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
                cout << x[i][j];
    }
    
    void foo3 (const char x[10][])
    {
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
                cout << x[i][j];
    }
    
    1. foo2 only
    2. foo3 only
    3. foo2 and foo3 only
    4. All of the functions
    5. None of the functions
    Solution

    B.
    In the function declaration of multidimensional arrays, the size of all dimensions except the first one (which is optional) must be specified.


  6. Which of the function(s) below can successfully be compiled and add 1 to every element of the 10x10 array?

    void cfoo1 (char arr[10][10])
    {
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
                arr[i][j]++;
    }
    
    void cfoo2 (const char arr[10][10])
    {
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
                arr[i][j]++;
    }
    
    void cfoo3 (char& arr[10][10])
    {
        for (int i = 0; i <10; i++)
            for (int j = 0; j < 10; j++)
                arr[i][j]++;
    }
    
    1. cfoo1 only
    2. cfoo3 only
    3. cfoo1 and cfoo2 only
    4. cfoo2 and cfoo3 only
    5. All of functions
    Solution

    A. In function cfoo2, the char array is declared as read only (const), and its elements cannot be modified using the increment operator. In function cfoo3, x is declared as an array of character references (char&), but array of references is not allowed in C++.


  7. Which of the following(s) show(s) the correct input and corresponding output of the program?

    int main(void)
    {
        char x[20];
        cout << "enter a C string:";
        cin >> x;
        cout << x << endl;
        return 0;
    }
    
    1.  
      enter a C string:    hkust comp2011
      hkust
      
    2.  
      enter a C string:    hkust comp2011
      hkust comp2011
      
    3.  
      enter a C string:    hkust comp2011
          hkust comp2011
      
    4.  
      enter a C string:    hkust comp2011 c++ programming
          hkust comp2011 c+
      
    Solution

    A.
    cin will skip all the leading white spaces when reading data of the required type until it sees the next white space. So if one enters a sentence with spaces between words, only the first word is read into x.


  8. Which of the following is the correct way to assign values to EACH element of an array?

    1.  
      char x[][] = {"a", "b", "c", "d", "e" };
      
    2.  
      int x[] = {{1}, {2}, {3}, {4}, {5}};
      
    3.  
      int x[]; for (int j = 0; j < 5; ++j) x[j] += 2;
      
    4.  
      float x[6]; for (int j = 5; j > -1; j--) x[j] = 8;
      
    5.  
      float x[1] = 3.4;
      

    Solution

    D.
    Others are wrong:
    Answer A: All dimenision, except the first MUST be specified when initializing an array.
    Answer B: The initializer is a 2D array initializer.
    Answer C: the size of the x array is missing.
    Answer E: x is not a single integer variable! To initialize it, you need something like "float x[1] = {3.4};"