Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I know about half of the answers to these multiple choice questions. The others

ID: 3558569 • Letter: I

Question

I know about half of the answers to these multiple choice questions. The others I don't. Please provide explanations, especially for the last four! Thanks! Points go to most correct with best explanations:)

---------------------------------------------------------------------------

20) 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?

         }

---------------------------------------------------------------------------------

21) Which of the following classes handles reading from a file?

------------------------------------------------------------------------------------------------

22) Passing by reference is also an effective way to allow a function to return more than one value.

True
False

-------------------------------------------------------------------------

23) 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 << " ";
cout << operate (n,m);
cout << " ";
return 0;
}

Which line(s) of code could be used to call this function (select all that apply):

float operate (float a, float b)

cout << operate (n,n);

cout << operate (n,m);

cout << operate (n,n);

cout << operate (n,m);

---------------------------------------------------------------------

24)   Consider the following prototype:

void duplicate (int& a, int& b, int& c)

What do the ampersands (&) signify?

------------------------------------------------------------------------------------------

25) What is missing from this function?

int divide (int a, int b=2)
{
int r;
r=a/b;
}

--------------------------------------------------------------------------

26)   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?

-------------------------------------------------------------------------------

27) 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? (select all that apply- can be more than one answer!)

if (key == sortedArray[mid])

else if (key > sortedArray[mid])

if (first <= last)

-----------------------------------------------------------------------------------------------------------------

28) Consider the following two approaches to the binary search algorithm using the STL vector:

Approach A

int BinarySearch(vector<int> A, int key)  
    {
    int l = 0;

    int r = A.size()-1;

    while (l <= r)  
        {

            int m = (l + r) / 2;
            if (key < A[m])
            {
                r = m - 1;
            }
            else if (key > A[m])
            {
                l = m + 1;
            }
            else
            {

                return count;
            }
        }

        return -1;
    }

Approach B

int search::BinarySearch(vector<int> sortedArray, int first, int last, int key)
    {
       if (first <= last) {
           int mid = (first + last) / 2;
           if (key == sortedArray[mid])
               return mid;
           else if (key < sortedArray[mid])
               return BinarySearch(sortedArray, first, mid-1, key);
           else
               return BinarySearch(sortedArray, mid+1, last, key);
       }
       return -1;
    }

Which approach uses recursion? Explain why Approach B has more parameters than Approach A.

-----------------------------------------------------------------------------------------------------------------

29) 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. ";
else even (a);
}

void even (int a)
{
if ((a%2)==0) cout << "Number is even. ";
else odd (a);
}

void odd (int a);
void even (int a);

-------------------------------------------------------------------------------------------------

30)   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?

p.key = "Bob" && p.val = 12345;
entries.0 =p;

p.key = "Bob";
p.val = 12345;
entries[0] =p;


entries[0] = {key = "Bob"; val = 12345};

p = "Bob";
p = 12345;
entries[0] =p;

A. myfile >> "This is a line. ";
myfile >> "This is another line. ";
myfile.close(); B. while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
      myfile.close();

         }

C. myfile << "This is a line. ";
myfile << "This is another line. ";
myfile.close(); D. while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
myfile.close();

Explanation / Answer

20) D.   while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();

21) D.   iostream

22) True

23) All Options are correct

24) A.   The ampersands signify that their corresponding arguments are to be passed by reference instead of by value.

25) B.   return r;

26) D.   To test if the original array has a length greater than zero before attempting any other statements.

27) A.   when the value is found in the middle of the range (when this statement is true)
if (key == sortedArray[mid])

28) B.   Approach B uses recursion. Approach B includes the additional first and last parameters because they are required in the process of recursion to reduce the search area, either by increasing the value of first or decreasing the value of last when a recursive call is made.

29) C.   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);

30) B.   pairT p;
p.key = "Bob";
p.val = 12345;
entries[0] =p;