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

C++ 30 Questions. Please do NOT bother to answer if you cannot answer all 30 que

ID: 3574183 • Letter: C

Question

C++ 30 Questions.

Please do NOT bother to answer if you cannot answer all 30 questions!! Thanks!!

1. The >> operator can be used to write a line to a file.

2. An array is a collection of data elements of one or more data types.

A. True

B. False

3. The __________ function will return true if the file you wish to use is ready for input/output operations.

7. 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;

}

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 //include iostream

using namespace std;

int main()

{

    cout << DoSomething(4, 3);

    return 0;

}

9. By default, all variables and functions are defined in the global namespace.

10. Although the using keyword can be used outside of a function to help resolve every identifier in the file, this is not recommended. Why?

11. What is the purpose of a constructor?

12. Consider the following main function. What can you guess about the Line class?

#include "line.h"

int main( )
{
   Line line(10.0);

}

13. 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;

14. 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] << " ";
      }

15. 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] << " ";
  }
}

16. Which search algorithm can be described as follows:

Given a collection you try every element in the collection until you have found the element or until you reach the end of the collection.

B.

17. You have the following struct defined in the private area of your class:

struct database {
int id_number;
int age;
float salary;
};

database employee;

How would you assign a value to each of the data members in a function belonging to the same class?

18. Consider the following function named mystery:

void mystery()

{

myclass foo;

int original_num = 1;

std::cout << original_num << std::endl;

foo.passbyValue(original_num);

std::cout << original_num << std::endl;

foo.passbyRef(original_num);

std::cout << original_num << std::endl;

}

How would you call (activate) this function?

19. What is the value of s4 as a result of the following statements:

string s = "yellow";

s4 = s.substr(1);

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.

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)

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?

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):

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

Approach A

int BinarySearch(vector 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 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);
}

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

31. Consider the following:

struct database {
int id_number;
int age;
float salary;
};

How would you set the age variable equal to 22?

32. What is wrong with this function?

void swapShellsFirstInArray(int &basket[5])
{
int temp = basket[0];
basket[0] = basket[1];
basket[1] = temp;
}

33. I have declared the following function:

Here is my main function:

What is wrong?

34. The std::binary_search function returns the index number of the val if it is found. Otherwise, it returns -1.

A. True

Explanation / Answer

Answer :

1. The >> operator can be used to write a line to a file.

A.True

B.False

Answer :

B.False

.............

2. An array is a collection of data elements of one or more data types.

A.True

B.False

Answer :

B.False

Explanation:

False,becauseAn array is a collection of data elements of same type.

................

3. The __________ function will return true if the file you wish to use is ready for input/output operations.

Answer :

A.ready()

..........

7. 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;

}

Answer :

B.return value must be int data type, not string

D.data types are required for x and y

.............

11. What is the purpose of a constructor?

Answer :

B.Constructors can be very useful for setting initial values for certain member variables.

.....

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

A.iostream

B.ofstream

C.inputstream

D.ifstream

Answer :

D.ifstream

.............

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

A.True

B.False

Answer :

A.True

.......................