Ch.7 Flashcards

1
Q

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) for(i = 0;i < SIZE;i ++)array1[i] = array2[i];
C) for(i = 0;i < SIZE;i ++) array1[] = array2[];

D) array1 = array2;

A

for(i = 0;i < SIZE;i ++)array1[i] = array2[i];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
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 start searching

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following function declarations correctly expect an array as the first argument?

A
void f1(int array[100], int size);
void f1(float array[], int size);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Given an array of integers of size 5, how does the computer know where the 3rd indexed variable is located?

A

It adds space for 3 integers to the base address of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why should you use a named constant for the size of an array?

A) makes changes to the program easier
B) helps reduce logic errors
C) readability of code
D) all of the above

A

all of the above

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of the following array declarations are legal?

A) int array[] = {0,0,0};
B) int size;
cin >> size;
int array[size];
C) const int size = 9;
int array [size];
D) int array[10];
E) all but B

A

all but B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is wrong with the following code fragment?

const int SIZE = 5;
float scores[SIZE];
for(int i = 0; i <= SIZE;i ++)
{
cout << “Enter a score\n”;
cin >> scores[i];

}

A

Array indexes must be less than the size of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following statements are true?

A) Array elements may be user-defined types (structs or classes).
B) If a function is expecting a variable of the base type of the array, then you can pass an element of the array to the function.
C) all of the above
D) none of the above

A

all of the above

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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[], const int size);
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[], int size) const;

A

void f1(const int array[], int size);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is wrong with the following code?
float scores[10], total;
A) nothing
B) Cannot declare regular and array variables together.
C) The 10 should be replaced with a variable name, whose value is input from the user.
D) Arrays must be integers.

A

nothing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given an array named scores with 25 elements, what is the correct way to access the 25th element?

A

scores[24]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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 << array[index1][index2] << “ “;
cout << endl;
}

A

0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

void f1(char a[6][8], int sizeOfFirst);

17
Q

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

A

char array[5] = {‘a’,’b’,’c’,’d’,’e’};

18
Q

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) float array[3,5];
C) int array[3][5];

D) float array[3][5];

A

float array[3][5]; or double array[[3][5]

19
Q

Which of the following will read values from the keyboard into the array?
(Assume the size of the array is SIZE).
A) cin >> array[];

B) cin >> array;
C) cin >> array[SIZE];

D) for(i = 0;i < SIZE;i ++)
cin >> array[i];

A

for(i = 0;i < SIZE;i ++)
cin >> array[i];

20
Q

What are the valid indexes for the array shown below?
int myArray[25];

A

0-24

21
Q

How many indexed variables does the following array have?
int myArray[12] = {1,2,3,6,5,4,7,1,2};

A

9

22
Q

If your index used to access the indexed variables of the array has the value of a non-existent index, this is called ________.

A

out of bounds

23
Q

An ________ is used to process a collection of data all of which is the same type

A

array

24
Q

Write the code to declare a two dimension array of integers with 10 rows and 20 columns

A

int array[10][20]

25
Q

The computer remembers the address of which indexed variable(s) in an array?

A

the first index variable

26
Q

Write the code to declare an array of 10 doubles named list.

A

double list[10];

27
Q

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

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

28
Q

In the expression
cout << score[i] << endl;
i is called the ________

A

index or subscript

29
Q

In the expression
double score[10];
double is called the ________ of the array.

A

base type

30
Q

A computer’s memory consists of numbered locations

A

bytes

31
Q

If you use the const modifier in a function declaration, you do not include it in the function definition.

A

False

32
Q

The indexed variables (members) of an array must be integers

A

False

33
Q

Arrays can be returned from a function

A

False

34
Q

The locations of the various indexed variables in an array can be spread out all over the memory.

A

False

35
Q

The following function declaration guarantees the values in the array argument are not changed.

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

A

False

36
Q

The following array declaration is legal
double scores[] = {0.1,0.2,0.3};

A

False

37
Q

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