1. What is wrong with the following code? float scores[10], total; a. The code c
ID: 3548839 • Letter: 1
Question
1. What is wrong with the following code? float scores[10], total;
a. The code cannot declare regular and array variables together.
b. The arrays must be integers.
c. The 10 should be replaced with a variable name, whose value is input from the user.
d. None of the choices apply.
2. 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]
3. 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 choices apply.
4. 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 ";
cin >> 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 >> scores[0];
5. 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'
6. Arrays are always passed to a function using which of the following?
a. Pass by value
b. Pass by reference
c. Pass by array
d. You cannot pass arrays to a function
7. Which of the following function declarations correctly expect an array as the first argument?
a. void f1(int array, int size);
b. void f1(int& array, int size);
c. void f1(int array[100], int size);
d. void f1(int array, int size); and void f1(int& array, int size);
8. 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);
9. The following function definition has an error in it. What line is this error on?
0. void f1(const double array[], int size)
1. {
2. int i=0;
3. while(i< size)
4. {
5. array[i] += 2;
6. cout <<array[i];
7. i++;
8. }
9. }
a. 0
b. 2
c. 5
d. 6
10. 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);
11. 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);
12. 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
13. Which of the following array declarations are legal?
a. int array[10]; int siz;
b. cin >> size; int array[size];
c. int array []={0,0,0};
d. int array[10]; int siz; and int array []={0,0,0};
14. 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);
15. If you need a function that will handle multi-dimensional arrays, you must specify which of the following sizes inside the square brackets?
a. All of the sizes
b. All of the sizes, except the last dimension
c. All of the sizes, except the first dimension
d. None of the choices apply.
16. 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
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
17. 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];
18. A two dimension array can also be thought of as a(n):
a. table.
b. array of arrays.
c. file.
d. table and array of arrays.
19. 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[]
20. 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];
21. The indexed variables (members) of an array must be integers.
a. True
b. False
22. The locations of the various indexed variables in an array can be spread out all over the memory.
a. True
b. False
23. The following array declaration is legal: double scores[]={0.1,0.2,0.3};
a. True
b. False
24. Arrays can be passed to functions.
a. True
b. False
25. Arrays can be returned from a function.
a. True
b. False
26. 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
b. False
27. 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
b. False
28. The following function declaration guarantees the values in the array argument are not changed: void function1(int array[], int numElements);
a. True
b. False
29. The following function will work with any size integer array: void function1(int array[], int numElements);
a. True
b. False
30. If you use the const modifier in a function declaration, you do not include it in the function definition.
a. True
b. False
0
1
2
3
1
2
3
4
2
3
4
5
3
4
5
6
Explanation / Answer
1:
The code float scores[10], total; has no errors.
It is possible to regular and array variables together. Arrays can be integer arrays or float arrays or of any type.
Hence, the correct option is d.
2:
An array named scores with 25 elements can be declared as float scores[25] or int scores[25].
Array indexing starts at 0. The syntax to access array elements is arrayName[index].
24th element can be accesses with the index value 24. So the correct way to access the 25th
Hence, the correct option is b.
3:
Following are the advantages when a named constant is used for specifying the size of an array.
Hence, the correct option is d.
4:
Suppose the size of the array is n. Array indexing starts at 0. In other words, the first element of the array will be at index 0. The last element of the array will be at index n-1. In other words, array indexes must be less than the size of the array. The wrong in the code is at line 3 for(int i=0; i<=SIZE;i++). The condition in the for statement must be i<SIZE.
Hence, the correct option is c.
5:
At the time an array is declared, memory is allocated and the array elements will have garbage values. If some elements of the array are assigned values, then the remaining elements will have garbage values.
So when an integer array of size 10 is declared and only 5 values are assigned with values, then the remaining 5 indexed variables will have garbage values.
Hence, the correct option is b.
6:
When an array is passed to the function, the address of the first element of the array is passed. So by default an array is passed using call by reference mechanism.
An array cannot be passed using call by value mechanism.
Hence, the correct option is b.
7:
The function void f1(int array[100], int size); correctly expect an array as the first argument.
For example:
#include <iostream>
using namespace std;
void f1(int array[10], int size)
{
int i,s=0;
for (i=0;i<size;i++)
s=s+array[i];
cout<<"sum : "s;
}
int main()
{
int array[10];size;
cout<<"Ënter the size : ";
cin>>size
f1(array,size);
return 0;
}
Hence, the correct option is c.
8:
void f1(const int array[], int size); corrects guarantee that the function will not change any values in the array argument. The key word const before the array will make the array as constant and changes cannot be done.
Hence, the correct option is d.
9:
In the function declaration, void f1(const double array[], int size) makes the array as constant as the key word const is used before the array. So changes cannot be done. In line 5, array[i] += 2;, the value of the array element is being changed. So this is not possible as the array becomes constant. Therefore, line 5 has the error.
Hence, the correct option is c.
10:
The correct prototype of a function to search an array for some value and return either the index where the value was found, or -1 if not found, int search(const int array[], int target, int numElements);
To search for an element in an array, name of the array, element to be searched, number of elements in the array must be passed. The return type of the function must be integer as index will be of integer type. If the element is not found, then -1 is returned which is also of type integer.
Hence, the correct option is d.
11:
To search for an element in an array, name of the array, element to be searched, number of elements in the array must be passed. The return type of the function must be integer as index will be of integer type. So the correct way to call the search function is result=search(array, target, numberOfElements);
Hence, the correct option is c.
12:
The given code will search for the target element in the entire array.
Hence, the correct option is c.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.