midterm ch 7 arrays Flashcards

1
Q
  1. The indexed variables (members) of an array must be integers. t/f
A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. The locations of the various indexed variables in an array can be spread out all over the memory. T/F
A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. The following array declaration is legal: double scores[]={0.1,0.2,0.3};
A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Arrays can be passed to functions. T/F
A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Arrays can be returned from a function.
A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. If a function is expecting a pass by reference parameter, you can pass an index variable from an array of the same base type to that function.
A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. When you have a function that expects an array, it should also expect the size of the array or the number of indexed variables with valid data.
A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. The following function declaration guarantees the values in the array argument are not changed:
    void function1(int array[], int numElements);
A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. The following function will work with any size integer array.

void function1(int array[], int numElements);

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. If you use the const modifier in a function declaration, you do not include it in the function definition.
A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Write the code to declare a two dimension array of integers with 10 rows and 20 columns.
A

ANSWER: int array[10][20];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Write the code to declare an array of 10 doubles named list;
A

ANSWER: double list[10];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. The modifier that guarantees that an array argument will not be changed is called ______.
A

ANSWER: const

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. How many indexed variables does the following array have?
    int myArray[]={1,2,3,6,5,4,7,1,2};
A

ANSWER: 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. How many indexed variables does the following array have?
    int myArray[12]={1,2,3,6,5,4,7,1,2};
A

ANSWER: 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. Write the declaration for a function named funct1 that expects an array of floats, the number of elements in the array and does not return any value.
A

ANSWER: void funct1(float myArray[], int numElements);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. If you put a value in the square brackets of a one-dimension array parameter, this value is _________ by the compiler.
A

ANSWER: ignored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. If your index used to access the indexed variables of the array has the value of a non-existent index, this is called _________
A

ANSWER: Index out of range, Index out of bounds, or illegal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
  1. The computer remembers the address of which indexed variable(s) in an array? ______
A

ANSWER: the first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
  1. A computer’s memory consists of numbered locations called __________.
A

ANSWER: bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
  1. In the expression
    double score[10];
    double is called the ___________ of the array
A

ANSWER: base type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
  1. In the expression
    cout &laquo_space;score[i] &laquo_space;endl;
    i is called the
A

ANSWER: index or subscript

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  1. An _______ is used to process a collection of data all of which is the same type
A

ANSWER: array

24
Q
  1. The individual variables that comprise an array are called __________
A

ANSWER: indexed variables, subscripted variables, or elements.

25
Q
  1. Indexes are numbered starting at _________
A

ANSWER: 0

26
Q
  1. What are the valid indexes for the array shown below?
    int myArray[25];
    a. 0-25
    b. 0-24
    c. 1-25
    d. 1-24
A

ANSWER: B

27
Q
  1. What is wrong with the following code?
    float scores[10], total;
    a. Cannot declare regular and array variables together.
    b. Arrays must be integers
    c. The 10 should be replaced with a variable name, whose value is input from the user
    d. Nothing.
A

ANSWER: D

28
Q
  1. Given an array named scores with 25 elements, what is the correct way to access the 25th element?
    a. scores+25
    b. scores[24]
    c. scores[25]
    d. scores[last]
A

ANSWER: B

29
Q
  1. Why should you use a named constant for the size of an array?
    a. Readability of code
    b. Makes changes to the program easier
    c. Helps reduce logic errors
    d. All of the above
A

ANSWER: D

30
Q
  1. Given an array of integers of size 5, how does the computer know where the 3rd indexed variable is located?
    a. It adds 3 to the base address of the array
    b. It adds space for 3 integers to the base address of the array
    c. It remembers where all the indexed variables of the array are located.
    d. None of the above
A

ANSWER: B

31
Q

hat is wrong with the following code fragment?
const int SIZE =5;
float scores[SIZE];
for(int i=0; i<=SIZE;i++)
{
cout &laquo_space;“Enter a score\n”;
cin&raquo_space; scores[i];
}
a. Array indexes start at 1 not 0
b. Arrays must be integers
c. Array indexes must be less than the size of the array
d. Should be cin&raquo_space; scores[0];

A

ANSWER: C

32
Q
  1. Which of the following declare an array of 5 characters, and initializes them to some known values?
    a. char array[5]={‘a’,’b’,’c’,’d’,’e’};
    b. char array[4]={‘a’,’b’,’c’,’d’,’e’};
    c. char array[5]={‘’};
    d. char array[]={‘a’,’b’,’d’,’e’};
    e. A and C
    f. B and D
    g. all of the above
A

ANSWER: E

33
Q
  1. If you declare and initialize an integer array of size 10, but only list 5 values, what values are stored in the remaining 5 indexed variables?
    a. 0
    b. garbage
    c. 0.0
    d. ‘0’
A

ANSWER: A

34
Q
  1. Arrays are always passed to a function using
    a. pass by value
    b. pass by reference
    c. pass by array
    d. you cannot pass arrays to a function
A

ANSWER: C

35
Q
  1. Give the following declarations, which of the following is a legal call to this function?
    int myFunction(int myValue);

int myArray[1000];
a. cout &laquo_space;myFunction(myArray);
b. cout &laquo_space;myFunction(myArray[0]);
c. myArray = myFunction(myArray);
d. myArray[1] = myFunction(myArray[0]);
e. A and B
f. A and C
g. B and D

A

ANSWER: G

36
Q
  1. Which of the following function declarations correctly guarantee that the function will not change any values in the array argument?
    a. void f1(int array[], int size) const;
    b. void f1(int array[], int size);
    c. void f1(int &array, int size);
    d. void f1(const int array[], int size);
    e. void f1(int array[], const int size);
A

ANSWER: D

37
Q
  1. The following function definition has an error in it. What line is this error on?
  2. void f1(const double array[], int size)
  3. {
  4. int i=0;
  5. while(i< size)
  6. {
  7. array[i] += 2;
  8. cout «array[i];
  9. i++;
  10. }
  11. }
    a. 0
    b. 2
    c. 5
    d. 6
    e. 2
A

ANSWER: C

38
Q
  1. Which of the following function declarations could be used to input data from the keyboard into the array?
    a. void input(int array[], int &numElements, int MAX_SIZE);
    b. void input(int array[], int numElements, int MAX_SIZE);
    c. void input(int &array[], int numElements, int MAX_SIZE);
    d. int array[] input(int array[], int &numElements, int MAX_SIZE);
A

ANSWER: A

39
Q
  1. If we want a search function to search an array for some value and return either the index where the value was found, or -1 if not found, which of the following prototypes would be appropriate?
    a. void search(const int array, int target, int numElements);
    b. void search(const int array, int target);
    c. int search(const int array[], int numElements);
    d. int search(const int array[], int target, int numElements);
A

ANSWER: D

40
Q
  1. Given the following function definition for a search function, and the following variable declarations, which of the following are appropriate function invocations?

const int SIZE=1000;
int search(const int array[], int target, int numElements);

int array[SIZE], target, numberOfElements;
a. search(array[0], target, numberOfElements);
b. result=search(array[0], target, numberOfElements);
c. result=search(array, target, numberOfElements);
d. result=search(array, target, SIZE);

A

ANSWER: C

41
Q
  1. Given the following function definition, what modifications need to be made to the search function so that it finds all occurrences of target in the array?
    int search(const int array[], int target, int numElements)
    {
    int index=0;
    bool found=false;while((!found) && (index < numElements))
    {
    if(array[index] == target)
    found=true;
    else
    index++;
    }
    if(found==true)
    return index;
    else
    return -1;
    }
    a. Add another parameter to indicate where to stop searching
    b. Add another parameter to indicate where to start searching
    c. This already can find all occurrences of a given target
    d. Have the function return the whole array
A

ANSWER: B

42
Q
  1. Given the following function definition, will repeated calls to the search function for the same target find all occurrences of that target in the array?
    int search(const int array[], int target, int numElements)
    {
    int index=0;
    bool found=false;while((!found) && (index < numElements))
    {
    if(array[index] == target)
    found=true;
    else
    index++;
    }
    if(found==true)
    return index;
    else
    return -1;
    }
    a. Yes
    b. No
    c. Impossible to tell without looking at the values of the array
    d. It depends on the value of target.
A

ANSWER: B

43
Q
  1. Which sort algorithm does the following outline define?
    for i between 0 and number_used-1 inclusive
    put the ith smallest element at array[i]
    a. sequential
    b. selection
    c. bubble
    d. swap
A

ANSWER:B

44
Q
  1. Which of the following array declarations are legal?
    a. int array[10];
    b. int size;
    cin&raquo_space; size;
    int array[size];
    c. int array[]={0,0,0};
    d. const int size=9;
    int array[size];
    e. All of the above
    f. All but C
    g. All but B
A

ANSWER: G

45
Q
  1. Which of the following function declarations will accept the following two-dimension array?
    int pages[10][30];
    a. void f1(int pages[][], int size);
    b. void f1(int pages[][30], int size);
    c. void f1(int pages[10][], int size);
    d. void f1(int& pages, int size);
A

ANSWER: B

46
Q
  1. If you need a function that will handle multi-dimensional arrays, you must specify the following sizes inside the square brackets.
    a. All the sizes
    b. All sizes except the last dimension
    c. All sizes except the first dimension
    d. None of the sizes
A

ANSWER: C

47
Q
  1. Which of the following correctly declare an array that can hold up to 3 rows of 5 columns of doubles?
    a. int array[3],[5];
    b. int array[3][5];
    c. float array[3][5];
    d. float array[3,5];
A

ANSWER: C

48
Q
  1. What is the output of the following code fragment?
    int array[4][4], index1, index2;
    for(index1=0;index1<4;index1++)
    for(index2=0;index2<4;index2++)
    array[index1][index2]=index1 + index2;
    for(index1=0;index1<4;index1++)
    {
    for(index2=0;index2<4;index2++)
    cout &laquo_space;array[index1][index2] &laquo_space;” “;
    cout &laquo_space;endl;
    }
    a.
    0 1 2 3
    1 2 3 4
    2 3 4 5
    3 4 5 6
    b.
    0 1 2 3
    0 1 2 3
    0 1 2 3
    0 1 2 3
    c.
    0 0 0 0
    1 1 1 1
    2 2 2 2
    3 3 3 3
    d.
    0 0 0 0
    0 1 2 3
    0 2 4 6
    0 3 6 9
A

ANSWER: A

49
Q
  1. Which of the following function declarations can be passed the following array?
    char myArray[6][8];
    a. void f1(char a[][], int sizeOfFirst);
    b. void f1(char a[][8], int sizeOfFirst);
    c. void f1(char& a, int sizeOfFirst);
    d. void f1(char a[6][8], int sizeOfFirst);
    e. B and D
    f. A and D
A

ANSWER: E

50
Q
  1. A two dimension array can also be thought of as
    a. a table
    b. an array of arrays
    c. a file
    d. none of the above
    e. A and C
    f. A and B
A

ANSWER: F

51
Q
  1. Which of the following will correctly assign all the values in one array to the other array? (Assume both arrays are of the same type and have SIZE elements)
    a. array1=array2;
    b. array1[]=array2;
    c. for(i=0;i<SIZE;i++)
    array1[i]=array2[i];
    d. for(i=0;i<SIZE;i++)
    array1[]=array2[];
A

ANSWER: C

52
Q
  1. Which of the following will read values from the keyboard into the array? (Assume the size of the array is SIZE).
    a. cin&raquo_space; array;
    b. cin&raquo_space; array[];
    c. cin&raquo_space; array[SIZE];
    d. for(i=0;i<SIZE;i++)
    cin&raquo_space; array[i];
A

ANSWER: D

53
Q
  1. Which of the following correctly uses C++11’s range-based for statement to iterate through every element of the array variable arr?
    a. for (auto x : arr)
    b. foreach (x in arr)
    c. for (auto x; x < arr.length; x++)
    d. for x in arr
A

ANSWER: A

54
Q
  1. What is the output of this code?
    int arr[] = { 1, 2, 3};
    for (int &element : arr)
    element+=10;
    for (int element : arr)
    cout &laquo_space;element &laquo_space;endl;
    a. 1 2 3
    b. 11 12 13
A

ANSWER: B

55
Q
  1. What is the output of this code?
    int arr[] = { 1, 2, 3};
    for (int element : arr)
    element+=10;
    for (int element : arr)
    cout &laquo_space;element &laquo_space;endl;
    a. 1 2 3
    b. 11 12 13
A

ANSWER: A

56
Q
  1. The following code should search the array “arr” of size 10 and set the variable “found” to true if 7 is in the array and set it to false if 7 is not in the array. What is wrong?
    bool found = true;
    for (int i = 0; i < 10; i++)
    {
    if (arr[i]==7)
    found = true;
    else
    found = false;
    }
    if (found)
    cout &laquo_space;“Found 7!” &laquo_space;endl;
    else
    cout &laquo_space;“7 not found.” &laquo_space;endl;
    a. The if statement should check if found == true
    b. If 7 is not in the array then found is left as its initial value of true
    c. If 7 is found it may later be changed to false if a non-7 is in the array
A

ANSWER: C