Multiple Choice Answers

1. (TCO 4) When organizing a program into three files (main, class implementation, and class header), which is not a .cpp file?
Main
None are .cpp
Class header
Class implementation

2. (TCO 4) What is the result of 5 % 7?
0
2
5
7
3. (TCO 4) Which looping statement is best if the number of iterations is known?
If/Else
For
While
Do/While

4. (TCO 4) What is the value stored in variable c, after the execution of the following statement?
int a=5, b=2, c;
c = ++a / b + 5;
7.5
8
7
0

5. (TCO 4) What is the value stored in variable c, after the execution of the following statement?
int a=5, b=2, c;
c = a++ / b + 5;
7.5
8
7
0

6.  (TCO 4) Which of the following is not a legal value to initialize a pointer variable?
Zero
Null
0
The address of a variable of the same type

7. (TCO 4) Which type of error does the following code fragment cause?
const int MAX = 500;
int main (void)
{
int foo [MAX];
for (int i = 0; i <= MAX; i++)
{
foo [i] = i * 2;
}
Compiler, because of out-of-bounds array subscript
Run-time, because of out-of-bounds array subscript
Compiler, because of invalid array initialization
None of the above. It does not create any type of error.

8. (TCO 4) Which statement correctly declares an array of 10 LunchBoxes. The LunchBox data type is already defined and is a struct that contains a ‘snack’ field, and a ‘drink’ field.
snack snacks[10]; drink drinks[10];
LunchBox  snacks[10];
x LunchBox[10]
snacks LunchBox[10];

9. (TCO 4) What is the data type of bullpen[3].era?
struct pitcher
{
string name;
double era;
};
pitcher p1;
pitcher bullpen[10];
string
double
pitcher
const pointer to pitcher

10. (TCO 4) Given the following code fragment, what is the data type of roster[9] .name?
struct student
{
string name;
double gpa;
};
student thisStudent;
student roster[50];
string
const pointer to student
student
double

11. (TCO 4) Which of the following statements call the following function correctly?
int MultiplyValues (int, int);
int a = MultiplyValues (int x, int y);
int a = MultiplyValues (10, 20);
double d = MultiplyValues (int 10, int 20);
All of the above

12. (TCO 4) The function call below is designed to return, in variable result, the product of x and y. All the variables are of type int. What is the correct prototype for such a function?
myFunction(result, x, y);
void myFunction(int &, int, int) ;
void myFunction(int, int,  int) ;
void myFunction(const int *, const int, const int) ;
void myFunction(int *, const int *, const int *) ;

13. (TCO 4) Which of the following is an invalid declaration to overload the following function?
double foo(int);
double foo(double);
int foo(int);
double foo(double *):
double foo(int *);

14. (TCO 4) Identify the correct prototype for a function that receives an array of doubles, computes the sum of all the elements of the array, and returns the sum to the calling function.
void computeSum(double array[], int size, double * sum);
void computeSum(double array[], int size, double & sum);
double computeSum(double array[], int size);
All of the above

15. (TCO 4) Assume you have a class, called Simple, that has two data members, ‘a’ and ‘b’, both of type int. What can be said about the function prototype given below? (Choose the best answer.)
Simple(int=0, int=0);
It is the prototype for the default constructor.
It is the prototype for the 2-argument constructor.
It is both the 0-argument, and the 2-argument, constructor.
It is incorrect.

16. (TCO 4) Which of the following is called automatically each time an object is created?
Compiler
Builder
Constructor
Destructor

17. (TCO 4) Given the following class definition and lines of code, what would be displayed if Line 6 were replaced by the following? cout << d2.getFeet() << endl;
class Distance
{
private:
int feet;
double inches;
public:
Distance( );
Distance(int initFt, double initIn);
void setFeet(int feetIn);
void setInches(double inchesIn);
int getFeet() const;
double getInches( ) const;
};
int main( )
{
Distance d1;  //Line 1
const int MAX = 100; //Line 2
Distance list[MAX];  //Line 3
Distance d2(1, 2.3); //Line 4
Distance * pDist;  //Line 5
d1.feet = 5; //Line 6
// etc. – assume the remaining code is correct
}
0
1
2.3
5

18. (TCO 4) What is the value of i after the following code fragment executes?
int   i = 2;
int   k = 5 ;
i *= k + 1;
7
11
12
14

19. (TCO 4) A Distance class has two private members, ‘feet’, of type int, and ‘inches’, of type double. Which prototype correctly declares the copy constructor for such class?
Distance Distance(const Distance &);
Distance(const Distance &);
int Distance(int, double);
Distance(feet, inches);

20. (TCO 4) A derived class is typically an example of
a “has a” relationship.
an “is a” relationship.
a “uses a” relationship.
a “is used” relationship.

21. (TCO 4) Given a class called Employee and the following definitions and statements,
void myFunction (Employee * eptr);
Employee emp;
which of the following statements correctly calls the function passing in the address of the data array?
myFunction(emp);
myFunction(&emp);
myFunction(*emp);
myFunction(**emp);

22. (TCO 4) What is the output of the following code snippet?
int *p;
int *q;
p = new int;
*p = 43;
q = p;
*q = 52;
p = new int;
*p = 78;
q = new int;
*q = *p;
cout << *p << ” ” << *q << end1
4352
5278
7878
7852

23. (TCO 4) Assume you have to write a class that makes use of dynamic memory allocation (the class needs to allocate and deallocate memory). According to best practices, which function should you overwrite for proper memory management?
Assignment operator function
Copy constructor
Assignment operator and copy constructor
None of the above

24. (TCO 4) What is wrong with the following C++ statements?
int * iptr;
double d = 123.321;
iptr = &d;
cout << *iptr;
The cout statement does not contain an endl.
The space following the ampersand should not be there.
The iptr variable cannot be given an address of a double.
All of the above

25. (TCO 4) What is the value of data after the following code executes?
unsigned char data = 0xA5;
data = data & 0xF0;
0xA0
0xF0
0xF5
0x5A