Sample Questions
Completion
________ are a feature of C++ that allows data structure libraries to be more general, so that you can define a vector of Cards or of Accounts or of Songs, etc. Essentially the type of object contained in a vector or list becomes a kind of parameter that can be set as you need it for your current task.
Multiple Choice
If you need to add an element at the beginning of a data structure, which is the most efficient data structure to add to?
Short Answer
Why is it expensive to increase the size of a vector?
6. What are two advantages of using vectors instead of linked lists?
Valid / Invalid
// with string class included
string one = ("real important stuff");
vector<Account> mine(10);
mine[4].Deposit(250);
Use the following class definition for the questions x-x+10.
class Thing {
public:
Thing();
Thing(Thing orig);
int GetVal();
void SetVal(int val);
private:
int Value;
};
Thing x;
x.SetVal(3);
vector<Thing> thngList(10,x);
Doing and Understanding:
(12 points)
10. Indicate the output from the following fragment of code
string long = "These are the times that try mens souls";
int place = long.find("tim");
int len = long.length();
if ((place > 0) && (place < len)) {
for (int cnt = place; place < len; ;place++) {
cout << long[cnt] << "|";
}
}
(12 points)
10. Indicate the output from the following fragment of code
string one = "asdf";
string two = "qwer";
one += two;
cout << one << | << two;
Assume
list<Card> myCards;
Has been filled with the Ace of Hearts, 10 of Clubs, Jack of Spades, 5 of Hearts, and Queen of diamonds (in that order)
And that the following statements have been executed
list<Card> copycards(myCards);
myCards.pop_front();
myCards.pop_back();
b) What cards are now in copycards? (show in order)
Writing Code
(points as shown)
Assuming that a class called Employee has been defined as below, and you are writing a main function. 1) Declare an empty vector of employees called current; 2) Create 10 employees, by asking the user for a name and salary, and adding the employee to the vector of employees.
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 salary
void GiveRaise(double pct); // give an employee a raise of the given pct
private:
string emplName;
double emplSalary;
};