Вы находитесь на странице: 1из 2

CS 401CS 401 Spring 1999

Practice Questions for Final Exam


Here a few example questions to try which should help you with the exam.
Remember that these do NOT cover all of what will be on the exam, so you should
still study everything in the study guide. As a review, be sure to look over the
practice questions from the first two exams as well.
________________________________________________________________________
Assume I am a computer novice, and that I want to know a good way to search for
an item in an array. Explain in detail how the Binary Search algorithm works,
any assumptions made on the data, and why it is good.
Answer: See Section 10.8 in Cohoon text and notes
_________________________________________________________________________
What is the difference between the private, public and protected parts of a
class specification? Be specific.
Answer: See Sections 8.2.1, 9.3.1, and 13.4 in Cohoon text
_______________________________________________________________________
Consider the employee class implemented in "employee.h" and "employee.cpp" (See
Handout 17).
Add a MEMBER FUNCTION called equals that returns true if the current employee
and a parameter employee have the same name and false otherwise. Write both
the prototype (as it would appear within the class specifications) and the
function definition.
Answer:
bool equals (const employee& rhs) const; // prototype

bool employee::equals (const employee& rhs) const


{
return (name == rhs.name); // remember that == is predefined
// for the string class
}
Write the operator == as an external function so that it accomplishes the same
task as the member function above. Remember that it is NOT a member function,
however.

Answer:
bool operator == (const employee& lhs, const employee& rhs) // prototype

bool operator == (const employee& lhr, const employee& rhs)


{
return (lhs.equals(rhs));
}

Give all output to the program below as indicated by the cout statements.

#include <iostream.h>

class foo
{
public:
float x;
double y;
};

void main()
{
int i = 75;
int * ip1, * ip2;
ip1 = &i;
ip2 = ip1;

cout << i << " " << *ip1 << " " << *ip2 << endl;

*ip2 = 85;

cout << i << " " << *ip1 << " " << *ip2 << endl;

foo f;
foo * fp = &f;
f.x = 6.8; f.y = 9.9;
cout << f.x << " " << (fp -> x) << endl;
(fp -> x) = (fp -> x) + 2.0;
cout << f.x << " " << (fp -> x) << endl;

cout << sizeof(f) << " " << sizeof(fp) << endl;
}

Answer:
75 75 75
85 85 85
6.8 6.8
8.8 8.8
12 4

Indicate whether each of the following statements is true or false. For false
statements, indicate why they are false.

a) Insertion Sort and Selection Sort in the worst case both require ~n2
comparisons.
Answer: True

b) Two-dimensional arrays in C++ are stored/accessed in column-major order.


Answer: False -- they are stored in row-major order

c) When character arrays are used as primitive strings in C++, a location must
be reserved for the '\0' character to mark the "end" of the string.
Answer: True

d) Objects of a base class can access member functions and data defined in a
derived class.
Answer: False -- it is the other way around (as long as the members are public
or protected)

Вам также может понравиться