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

1. Explain TWO things that are wrong with this code and how to fix them: #includ

ID: 3575417 • Letter: 1

Question

1. Explain TWO things that are wrong with this code and how to fix them:

#include <string>

int sum(x, y)

{

std::string result;

     result = x + y;

     return result;

}

Choose Two:

2. You have a main method with the following statements. Each statement creates an object of the Platypus class, but one sends an argument in parentheses and the other does not. In which circumstances would this be allowed?

Platypus p1("digger");

Platypus p1;

3. Consider the following prototype:

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

What do the ampersands (&) signify? Choose Two or more

4. 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?

Choose Two or more

To walk through each element of the array from the first element until the end of the array is reached.

5. 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?

Choose Two or more

B. When the midpoint value is greater than the target (when this statement is true)

C. when the value is found in the middle of the range (when this statement is true):

D. when there are no elements in the specified range of indices (when this statement is false):

6. Consider the following function prototypes:

void passbyValue(int valnum);

void passbyRef(int &refnum);

Which has a reference parameter?

Choose one or two (Multiple Choice)

7.What is missing from this function?

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

Choose One or Two or more (Multiple Choice)

A. You cannot use the + operator with a char data type.

Explanation / Answer

Question 1:

Answer is :
B.   return value must be int data type, not string
D.   data types are required for x and y

Why?
   #include <string>
int sum(int x,int y)
{
std::int result;
result = x + y;
return result;
}


Question 2:


Answer

A.   When the Platypus class has an overloaded constructor. One constructor of this class would have no parameters. When an object is created without an argument, this constructor is called. The class also has another constructor that has a string parameter. When an object is created with a String argument, this constructor is called.

Question 3:


Answer is only one
A. The ampersands signify that their corresponding arguments are to be passed by reference instead of by value.

Why?
B. None of these. Ampersands are not allowed in a function prototype.
they can be used
C. The ampersands signify that their corresponding arguments are to be passed as a constant (read-only) value.
no they are read and write values

D. The ampersands signify that their corresponding arguments are to be passed by value instead of by reference.
they are passed by reference


  
Question 4:


Answer is
B.   To search between the low and high index numbers as long as there are index numbers between low and high to search.
C.   To set the index point in the middle so the right hand side of the array can be searched before the left hand side is searched.

Question 5:


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

D. when there are no elements in the specified range of indices (when this statement is false):
if (first <= last)

Question 6:


Answer is
A. void passbyRef(int &refnum);

Question 7:


Answer is
A. return r;