Study Guide 7: Arrays

  1. An array element is accessed using
    1. a first-in-first-out approach.
    2. a last-in-first-out approach.
    3. the dot operator.
    4. a member name.
    5. an index number.
  2. All the elements in an array must be the ____________ data type.
  3. Write a statement that defines a one-dimensional automatic array called doubleArray of type double that holds 100 elements; define the array as an automatic variable.
  4. The elements of a 10-element array are indexed from _______ to _______ .
  5. Write a statement that takes element j of array doubleArray and writes it to cout with the insertion operator.
  6. Write a single statement that defines an automatic int array named coins and initializes the elements to the numeric values (in pennies) of US coins: penny, nickel , dime, quarter, half-dollar, and dollar.
  7. When a multidimensional array is accessed, each array index is
    1. separated by commas.
    2. surrounded by brackets and separated by commas.
    3. separated by commas and surrounded by brackets.
    4. surrounded by brackets.
  8. C++ allows four dimensional arrays.
    1. true
    2. false

  9. An array with five rows and three columns. An array with five rows and three columns. The element in the fourth row and the second column is colored blue.
    (a)(b)
  10. Choose the best statement that defines an array of type double named coefficients that matches the array illustrated in (a) above.
    1. double coefficients;
    2. double coefficients[5, 3];
    3. double coefficients[5][3];
    4. double coefficients[3, 5];
    5. double coefficients[3][5];
    6. double coefficients[4, 2];
    7. double coefficients[4][2];
    8. double coefficients[2, 4];
    9. double coefficients[2][4];
  11. Choose the best statement that stores 2 in the shaded element of the array illustrated in (b) above.
    1. double coefficients[4, 2] = 2;
    2. double coefficients[4][2] = 2;
    3. coefficients[4, 2] = 2;
    4. coefficients[4][2] = 2;
    5. double coefficients[2, 4] = 2;
    6. double coefficients[2][4] = 2;
    7. coefficients[2, 4] = 2;
    8. coefficients[2][4] = 2;
    9. double coefficients[3, 2] = 2;
    10. double coefficients[3][1] = 2;
    11. coefficients[3, 1] = 2;
    12. coefficients[3][1] = 2;
    13. double coefficients[1, 3] = 2;
    14. double coefficients[1][3] = 2;
    15. coefficients[1, 3] = 2;
    16. coefficients[1][3] = 2;

  12. For a two-dimensional automatic array of type int, called arr, choose a statement that defines the array and initializes the first row to 52, 27, 83; the second row to 94, 73, 49; and the third row to 3, 6, 1.
    1. int arr[3][3] = {{52,27,83}, {94,73,49}, {3,6,1}};
    2. int arr[][3] = {{52,27,83}, {94,73,49}, {3,6,1}};
    3. int arr[3][3] = {52,27,83,94,73,49,3,6,1}
  13. The name of an array, without square brackets, represents the ____ of the array.
  14. Given the following definitions
    int	scores[50];
    int*	my_pointer = ...;	// initialized to a valid value

    Which statements are valid?

    1. int* spointer = &scores;
    2. int* spointer = scores;
    3. scores = my_pointer;
  15. When an array is passed as an argument to a function,
    1. the function and the function caller accesses the same array.
    2. the function accesses a copy of the array.
  16. An array function argument allows data to be both passed into and out of the function.
    1. True
    2. False
  17. If scores is a C++ array, you can write scores.length to find the length of the array.
    1. True
    2. False
  18. Assuming that employee is the name of structure, tell what this statement defines:
    employee emplist[1000];
    1. An array with 1000 elements of a structure named employee
    2. An array with 1000 elements that are pointers to structures named employee
    3. An pointer to an array with 1000 elements of a structure named employee
    4. There is insufficient information to determine what the array contains
  19. Assuming that employee is the name of a structure, tell what this statement defines:
    employee* emplist[1000];
    1. An array with 1000 elements of a structure named employee
    2. An array with 1000 elements that are pointers to structures named employee
    3. An pointer to an array with 1000 elements of a structure named employee
    4. There is insufficient information to determine what the array employee
  20. Assuming that employee is the name of a structure, tell what this statement defines:
    employee* emplist = new employee[1000];
    1. An array with 1000 elements of a structure named employee
    2. An array with 1000 elements that are pointers to structures named employee
    3. An pointer to an array with 1000 elements of a structure named employee
    4. There is insufficient information to determine what the array contains
  21. Write a statement to define an automatic array of ints, named scores, as illustrated below.

    An array with three rows and five columns.

  22. An array of ints named scores is illustrated below. Write a statement to assign 100 to the array element indicated by the shaded region:

    An array with three rows and five columns. The element in the second row and the fourth column is colored blue.

  23. In a stack (implemented as an array), the data item placed on the stack first is
    1. not given an index number.
    2. given the index number 0.
    3. the first data item to be removed.
    4. the last data item to be removed.
  24. Write a statement that defines an automatic array called manybirds that holds 50 objects of type bird.
    1. bird manybirds[49]; // 49 because the first element is 0
    2. bird manybirds[50];
    3. bird manybirds = new bird[49];
    4. bird manybirds = new bird[50];
    5. bird* manybirds = new bird[50];
  25. Choose the statement that creates a dynamic array called manybirds that holds 50 objects of type bird.
    1. bird manybirds[49]; // 49 because the first element is 0
    2. bird manybirds[50];
    3. bird manybirds = new bird[49];
    4. bird manybirds = new bird[50];
    5. bird* manybirds = new bird[50];
  26. The compiler will detect and report an error when you compile a program that accesses array element 14 in an array of size 10.
    1. true
    2. false
  27. The computer will always detect and report an error when you run a program that tries to access array element 14 in an array of size 10.
    1. true
    2. false
  28. A program defines an array as: int a[10]; which function is able to correctly accept "a" as an argument?
    1. void func(int m[]) { ... }
    2. void func(int* m) { ... }
    3. void func(int m[10]) { ... }
    4. None of the above
  29. A program defines a two-dimensional array as: int mat[5][10]; which function is able to correctly accept mat as an argument?
    1. void func(int m[][]) { ... }
    2. void func(int m[][10]) { ... }
    3. void func(int m[5][10]) { ... }
    4. void func(int m[5][]) { ... }
    5. None of the above
  30. Given an array whose size is stored in the variable size, choose the correct for-loop to use to reverse the elements in the array.
    1. for (int i = 0; i < size; i++)
    2. for (int i = 0; i < size / 2; i++)
    3. for (int i = 1; i <= size; i++)
    4. for (int i = 1; i <= size / 2; i++)
  31. Examine the following function and briefly (a) describe what is wrong and (b) one way to correct the error.
    int* get_data()
    {
    	int data[100];
    	// fill the data array
    	return data;
    }
  32. If "a" is an array, it is possible in a C++ program loop through the elements of the array by:
    for (int i = 0; i < a.length; i++) . . .
    1. True
    2. False
  33. A C++ program defines a function as
    void f(foo* data, int x, int y) { . . . }
    foo is the name of a fully specified structure and data is a one-dimensional array of foo objects. The variables x and y are valid indexes into data. Write the statements to swap the values stored in data[x] and data[y]. The size of data (i.e., the number of elements) is not important for this problem.
  34. Choose the best statement to describe the following code fragment:
    char data[100];
    int count = sizeof(data) / sizeof(char);
    1. the value saved in count is the number of elements in the array named data
    2. the value saved in count is meaningless because sizeof(data) is the size of the variable named data.
  35. Choose the best statement to describe the following code fragment:
    char* data = new char[100];
    int count = sizeof(data) / sizeof(char);
    1. the value saved in count is the number of elements in the array named data
    2. the value saved in count is meaningless because sizeof(data) is the size of the variable named data.
  36. Choose the best statement to describe the following code fragment:
    void function(char* data)
    {
    	count = sizeof(data) / sizeof(char);
    }
    1. the value saved in count is the number of elements in the array named data
    2. the value saved in count is meaningless because sizeof(data) is the size of the variable named data.
  37. Complete the following function that copies a character array, source, to a second array, destination.
    void copy(char* destination, char* source, int size)
    {
    
    
    }
  38. Write a complete function named average that takes two arguments as described below and returns a double: