Exam 2 Practice(eCampus_part3)

1. The following program results in a compiler error. Why?

int FindMin(int a, int b) {
    int min;
    if (a < b) {
        min = a;
    } else {
        min = b;
    }
    return min;
}

int main() {
    int x;
    int y;
    cin >> x;
    cin >> y;
    min = FindMin(x, y);
    cout << "The smaller number is: " << min << endl;
}

The variable min is declared in FindMin(), but is used in main().

2. Which of the following declarations is an accessor?

void GetName() const;

(when there are private members in a class, we need functions to get the value from the class, which is called an accessor. The function stored or changed value to that variable is called mutator)

3. Which XXX will call a destructor?

int main() {
XXX
Cout << “Exiting main” << endl;
Return 0;
}

LinkedList list;

List.append(1);

note on zybook

4.Which XXX best defines the function’s integer vector parameter scores, if scores will have just a few elements, and the function will not change scores?

Void Quiz1 (XXX) {...}

Const vector <int> scores

5. Given a function with one vector parameter scores. How should the parameter be defined if scores may be very large, and the function will modify the parameter? Not constant, and pass by reference

6. Which of the following statements is true about inline member functions of a class?

An inline function is typically a short function definition with compact code

7.Which of the following statements is true about public member functions of a class? A class user can call the functions to operate on the object

8. In the following code, where are variable a, b, c, and d stored?

#include <iostream>
Using namespace std;
int a = 10;
int Add(int x, int y) {
int b;
b = x + y;
return b;
}
int main () {
int c;
c = 20;
int d;
d = Add(a, c);
Cout << d;
return 0;
}

stack

9. Which is true regarding how functions work?

A function’s local variables are discarded upon a function’s return; each new call creates new local variables in memory

10. Which of the following statements is true about a class’ private helper function? A private helper function helps public functions carry out tasks

11. Which of the following statements is true about private data members of a class?

A member function can access the private data members, but class users cannot.

12. Which XXX calculates the area using the CalcSquare() function? The formula to calculate the area of a circle is pi * r 2 .

double CalcSquare(double x) {
 return x*x;
}
double CalcArea(double r) {
 const double PI_VAL = 3.14159265;
 XXX;
}
int main() {
 cout << CalcArea(5.0);
 return 0;
}

return PI_VAL * CalcSquare(r);

13. The automatic process of finding and freeing unreachable allocated memory locations is called a _____. garbage collection

14. Which of the following statements is true about a class' member function definition? A function definition provides a class name, return type, arguments, and the function's statements

15.Which of the following statements is true about a memory leak?

A memory leak occurs when a program allocates memory but loses the ability to access the allocated memory.

Last updated

Was this helpful?