Question
Question 1: Consider the following incomplete main function. int main () { ofstream myfile (example.txt); if (myfile.is_open()) { //TODO: complete this block of code }
Question 1:
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. "; myfile << "This is another line. "; myfile.close(); |
C. | while ( myfile.good() ) { getline (myfile,line); cout << line << endl; myfile.close(); } |
D. | myfile >> "This is a line. "; myfile >> "This is another line. "; myfile.close();
|
Question 2 (Could choose more than one answer):
Consider the following recursive binary search function:
int search::rBinarySearch(int sortedArray[], int first, int last, int key) { if (first <= last) { int mid = (first + last) / 2; // compute mid point. if (key == sortedArray[mid]) return mid; // found it. else if (key < sortedArray[mid]) // Calls itself for the lower part of the array return rBinarySearch(sortedArray, first, mid-1, key); else // Calls itself for the upper part of the array return rBinarySearch(sortedArray, mid+1, last, key); } return -1; }
Which of the following might be described as the base case(s) for this method to end the recursion?
A. when there are no elements in the specified range of indices (when this statement is false): if (first <= last) |
B. When the midpoint value is less than the target |
C. When the midpoint value is greater than the target (when this statement is true) else if (key > sortedArray[mid]) |
D. when the value is found in the middle of the range (when this statement is true): if (key == sortedArray[mid])
|
Question3:
What is wrong with this function?
void swapShellsFirstInArray(int &basket[5]) { int temp = basket[0]; basket[0] = basket[1]; basket[1] = temp; }
A. You cannot pass an array by reference |
B.The array size is too large in the parameter list |
C. The code will only work with a vector, not an array |
D. The return type should not be void
|
Question 4:
I have declared the following function:
int findfirst(int a[][7]) { return a[0][0]; }
Here is my main function:
int main() { int myarray[1][1] = {1}; int first = findfirst(myarray); }
What is wrong?
A. The first variable in main stores the return value of findfirst, which is an integer array, not an integer. |
B. The array is too small for the findfirst function. It must have 7 columns. |
C. The array is empty when the findfirst function is called. |
D. The array in main is not declared properly. It should be named a, not myarray. |
E. The array is too small for the findfirst function. It must have 7 rows.. |
Question 5: (Could choose more than one answer)
Explain two things that are wrong with this code and how to fix them:
#include
int sum(x, y)
{
std::string result;
result = x + y;
return result;
}
A. The prototype should be: void sum(x, y); |
B. return value must be int data type, not string |
C. You cannot use the + operator with a char data type. |
D. data types are required for x and y |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started