I know we are not supposed to post homework/test questions but a friend that was going to help me flaked. There were 50 questions total and I do not know the answer to the remaining 14. Any help would be appreciated.
6. Consider this for loop to walk through all elements of the array of integers called b. Complete the code for the loop so that the number 0 is placed in each element of the array.
int b[ 5 ];
for ( n=0 ; n<5 ; n++ )
{
//place a 0 in each element of the array
}
A. 0 = b[n];
B. int b[] = {0};
C. b[n] = 0;
D. b[0] = n;
7. Explain two things that are wrong with this code and how to fix them:
int sum(x, y)
{
char result;
result = x + y;
return result;
}
A. The prototype should be:
void sum(x, y);
B. return value must be int data type, not char
C. data types are required for x and y
D. You cannot use the + operator with a char data type.
8. Consider the following code. What is the output?
foo.h:
1
2
3
4
5
int DoSomething(int nX, int nY)
{
return nX + nY;
}
goo.h:
1
2
3
4
5
int DoSomething(int nX, int nY)
{
return nX - nY;
}
main.cpp:
1
2
3
4
5
6
7
8
9
10 #include "foo.h"
#include "goo.h"
#include <iostream> //include iostream
using namespace std;
int main()
{
cout << DoSomething(4, 3);
return 0;
}
A. 1
B. Error occurs due to naming collision
C. Error occurs due to missing class definition
D. 7
12. Consider the following main function. What can you guess about the Line class?
#include "line.h"
int main( )
{
Line line(10.0);
}
A. It has a constructor that has a parameter
B. It has a constructor that has one or more parameters
C. It has a member variable with the same name as the class that can store the value 10.0
D. None of the above
25. Assume that myList is a previously declared an initialized array. Assume that the variable length holds the total number of elements in the array. What does the following code block do?
for (int i = 0; i < length; i++) {
cout << myList[i] << " ";
}
A. Displays the data stored before the first element of the array called myList up to the data element stored in the second to last element in the array, separated by a space.
B. Displays each index number of the array called myList, followed by a space, up to the last index number in the array's length.
C. Displays the total number of elements in the array called myList followed by a space.
D. Displays each data element stored in the array called myList, separated by a space.
29. Which of the following would be a correct way to call (invoke) this method?
void printArray(int array[]) {
for (int i = 0; i < length; i++) {
cout << array[i] << " ";
}
}
A. int result[];
printArray(int result[]);
B. int result[]={3, 1, 2, 6, 4, 2};
printArray(result);
C. int result[]={3, 1, 2, 6, 4, 2};
printArray({3, 1, 2, 6, 4, 2});
D. int[]{3, 1, 2, 6, 4, 2};
printArray(int[]);
34. You have the following struct defined in the private area of your class:
struct database {
int id_number;
int age;
float salary;
};
employee database;
How would you assign a value to each of the data members in a function belonging to the same class?
A. employee::age = 22;
employee::id_number = 1;
employee::salary = 12000.21;
B. employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
C. employee->age = 22;
employee->id_number = 1;
employee->salary = 12000.21;
D. age = 22;
id_number = 1;
salary = 12000.21;
37. Consider the following incomplete main function.
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
//TODO: complete this block of code
}
else cout << "Unable to open file";
return 0;
}
Which statements might be used to complete the if block?
A. while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
B. myfile >> "This is a line.\n";
myfile >> "This is another line.\n";
myfile.close();
C. myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
D. while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
myfile.close();
}
40. Consider the following program:
int operate (int a, int b)
{
return (a*b);
}
float operate (float a, float b)
{
return (a/b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}
Which line(s) of code could be used to call this function (select all that apply):
float operate (float a, float b)
A. float n=5.0,m=2.0;
cout << operate (n,m);
B. int n=5,m=2;
cout << operate (n,n);
C. int n=5,m=2;
cout << operate (n,m);
D. float n=5.0,m=2.0;
cout << operate (n,n);
42. Consider the following prototype:
void duplicate (int& a, int& b, int& c)
What do the ampersands (&) signify?
A. The ampersands signify that their corresponding arguments are to be passed by reference instead of by value.
B. The ampersands signify that their corresponding arguments are to be passed by value instead of by reference.
C. The ampersands signify that their corresponding arguments are to be passed as a constant (read-only) value.
D. None of these. Ampersands are not allowed in a function prototype.
43. What is missing from this function?
int divide (int a, int b=2)
{
int r;
r=a/b;
}
A. Nothing is missing
B. return r;
C. cout << r;
D. &r;
44. Consider the following:
int BinarySearch(int A[], int key, int low, int high)
{
while (low <= high)
{
int m = (low + high) / 2;
if (key < A[m])
{
high = m - 1;
}
else if (key > A[m])
{
low = m + 1;
}
else
{
return m;
}
}
return -1;
}
What is the purpose of iteration in the above code?
A. To set the middle index point so the search area can be divided in two.
B. To test if the original array has a length greater than zero before attempting any other statements.
C. To search between the low and high index numbers as long as there are index numbers between low and high to search.
D. To walk through each element of the array from the first element until the end of the array is reached.
48. Consider the following code. Assume that the program includes iostream and uses namespace std.
Why would you get identifier not found errors?
int main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}
void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}
A. Because the function calls do not match the function definitions.
B. Because variable a is not defined in the body of the even and odd functions.
C. Because the variable i is not initialized to a valid integer value before it is used.
D. Because the functions even and odd are not declared prior to the function calls. Their prototypes are needed above main:
void odd (int a);
void even (int a);
50. You have an array of structs that is defined as follows.
struct pairT {
std::string key;
int val;
};
Which of the following could be used to add a new struct to the array called entries in position 0?