midterm ch 7 arrays Flashcards
- The indexed variables (members) of an array must be integers. t/f
FALSE
- The locations of the various indexed variables in an array can be spread out all over the memory. T/F
FALSE
- The following array declaration is legal: double scores[]={0.1,0.2,0.3};
TRUE
- Arrays can be passed to functions. T/F
TRUE
- Arrays can be returned from a function.
FALSE
- 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.
TRUE
- 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.
TRUE
- The following function declaration guarantees the values in the array argument are not changed:
void function1(int array[], int numElements);
FALSE
- The following function will work with any size integer array.
void function1(int array[], int numElements);
TRUE
- If you use the const modifier in a function declaration, you do not include it in the function definition.
FALSE
- Write the code to declare a two dimension array of integers with 10 rows and 20 columns.
ANSWER: int array[10][20];
- Write the code to declare an array of 10 doubles named list;
ANSWER: double list[10];
- The modifier that guarantees that an array argument will not be changed is called ______.
ANSWER: const
- How many indexed variables does the following array have?
int myArray[]={1,2,3,6,5,4,7,1,2};
ANSWER: 9
- How many indexed variables does the following array have?
int myArray[12]={1,2,3,6,5,4,7,1,2};
ANSWER: 12
- 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.
ANSWER: void funct1(float myArray[], int numElements);
- If you put a value in the square brackets of a one-dimension array parameter, this value is _________ by the compiler.
ANSWER: ignored
- If your index used to access the indexed variables of the array has the value of a non-existent index, this is called _________
ANSWER: Index out of range, Index out of bounds, or illegal.
- The computer remembers the address of which indexed variable(s) in an array? ______
ANSWER: the first
- A computer’s memory consists of numbered locations called __________.
ANSWER: bytes
- In the expression
double score[10];
double is called the ___________ of the array
ANSWER: base type
- In the expression
cout «_space;score[i] «_space;endl;
i is called the
ANSWER: index or subscript
- An _______ is used to process a collection of data all of which is the same type
ANSWER: array
- The individual variables that comprise an array are called __________
ANSWER: indexed variables, subscripted variables, or elements.
- Indexes are numbered starting at _________
ANSWER: 0
- 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
ANSWER: B
- 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.
ANSWER: D
- 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]
ANSWER: B
- 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
ANSWER: D
- 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
ANSWER: B
hat is wrong with the following code fragment?
const int SIZE =5;
float scores[SIZE];
for(int i=0; i<=SIZE;i++)
{
cout «_space;“Enter a score\n”;
cin»_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»_space; scores[0];
ANSWER: C
- 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
ANSWER: E
- 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’
ANSWER: A
- 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
ANSWER: C
- Give the following declarations, which of the following is a legal call to this function?
int myFunction(int myValue);
int myArray[1000];
a. cout «_space;myFunction(myArray);
b. cout «_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
ANSWER: G
- 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);
ANSWER: D
- The following function definition has an error in it. What line is this error on?
- void f1(const double array[], int size)
- {
- int i=0;
- while(i< size)
- {
- array[i] += 2;
- cout «array[i];
- i++;
- }
- }
a. 0
b. 2
c. 5
d. 6
e. 2
ANSWER: C
- 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);
ANSWER: A
- 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);
ANSWER: D
- 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);
ANSWER: C
- 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
ANSWER: B
- 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.
ANSWER: B
- 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
ANSWER:B
- Which of the following array declarations are legal?
a. int array[10];
b. int size;
cin»_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
ANSWER: G
- 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);
ANSWER: B
- 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
ANSWER: C
- 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];
ANSWER: C
- 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 «_space;array[index1][index2] «_space;” “;
cout «_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
ANSWER: A
- 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
ANSWER: E
- 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
ANSWER: F
- 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[];
ANSWER: C
- Which of the following will read values from the keyboard into the array? (Assume the size of the array is SIZE).
a. cin»_space; array;
b. cin»_space; array[];
c. cin»_space; array[SIZE];
d. for(i=0;i<SIZE;i++)
cin»_space; array[i];
ANSWER: D
- 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
ANSWER: A
- What is the output of this code?
int arr[] = { 1, 2, 3};
for (int &element : arr)
element+=10;
for (int element : arr)
cout «_space;element «_space;endl;
a. 1 2 3
b. 11 12 13
ANSWER: B
- What is the output of this code?
int arr[] = { 1, 2, 3};
for (int element : arr)
element+=10;
for (int element : arr)
cout «_space;element «_space;endl;
a. 1 2 3
b. 11 12 13
ANSWER: A
- 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 «_space;“Found 7!” «_space;endl;
else
cout «_space;“7 not found.” «_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
ANSWER: C