Computer Science 162 – Spring 2000
02/25/2K Midterm Exam #1 Test Form A
Name:
Instructions:
Answer ALL questions on the answer sheet provided.
Hand in TEST, Answer Sheet, and Help Sheet, each with your name on it.
Remember to put the letter of your test form on your answer sheet.
Completion (3
points each)
1. Functions in C++ can be ________; this means that two different functions can have the same name. They can be distinguished based on the number and types of parameters, and the class the function belongs to.
2. A(n) ________ function in C++ runs when an object is declared, creating and initializing the data (data members) for the object. These have the same name as the class and no return type.
3. The data members and member functions in the ________ section of a class definition can be accessed by member functions, but NOT by client functions, such as main. That is, these are hidden or inaccessible to users of the object.
4. Some
output manipulators, such as setprecision, fixed, and left, are ________, that is they remain in effect until they
are changed; others, such as setw are not.
5. A(n) ________ is a data structure that is a general container for objects. Objects enter and exit at the same end (the top) – the last one in is the first one out (and vice versa).
Short Answer
(points as shown)
(6 points)
6. If you are declaring a C++ member function that has a set of parameters, which parameters can have defaults specified in the definition?
Valid/Invalid (4
points each)
For each, tell
whether valid or invalid. If invalid, tell why!
Use the
following class definition for the questions 7-10.
class Thing {
public:
Thing();
Thing(Thing orig);
int GetValue();
void SetValue(int val);
private:
int Value;
};
7.
. // client
code in main using Thing class defined above
Thing X;
Thing Y(X);
8.
. // client
code in main using Thing class defined above
Thing X;
X.SetValue(4);
9.
. // client
code in main using Thing class defined above
Thing X;
X.Value = 3;
Doing and
Understanding: (points as shown)
(12 points)
10. Indicate
the output from the following fragment of code
void mixit
(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main () {
int x = 5;
int y = 7;
mixit(x,y);
cout << x << " "
<< y << endl;
}
(12 points)
11. Given the
attached Account class definition, Indicate the output from the following
fragment of code:
Account x(“1234”);
bool in = x.Deposit(100);
while (x.Withdraw(30)) {
cout << x << endl;
}
(13 points)
12. For the
code fragment below using the string library, indicate the output:
string myWords = “I can’t believe the news
today”;
int len = myWords.length();
int half = len / 2;
string begin = myWords.substr(0,half);
string end = myWords.substr(half,len –
half);
cout << “len: “ << len << “
flopped: “ << end << “ “ << begin << endl;
Writing Code
(points as shown)
Given the
attached Card, Deck, and Player class definitions:
(5 points)
13. Write a
declaration for an array of 5 cards.
(10 points)
14. In a game
a Deck variable named deck1 has been created, shuffled, and some play has gone
on. Write statements in main that will check if any cards remain in the deck,
and if so draws a card, putting it in the variable currCard.
(15 points)
15. Assuming
that a class called Employee has been defined as below, and you are in the
middle of writing a main function and that you have already 1) declared an
array of 20 employees called employs;
2) asked the user for employee name, and salary for all of the employees, and 3) updated the
employee objects in the array with this information. Now, your job is to:
·
give all
employees a 10 percent raise
class
Employee {
public:
Employee (); // default employee prototype
Employee (string name, double
salary); // fully specified employee
prototype
string GetName(); // report employee’s name
double GetSalary(); // report employee’s
salary
void SetName(string nam); // set or change employee’s name
void SetSalary (double area); // set or change employee’s land area
void GiveRaise(double pct); // give an employee a raise of the given
pct
private:
string emplName;
double emplSalary;
};