Quizzes are given on a more-or-less random basis. Missed quizzes may not be made up. Quiz questions and answers will be posted here periodically.
| # | Quiz | Date |
|---|---|---|
|
9
10
11
|
Consider
the following incomplete function implementation: ///////////////////////////////////////////////////////////
ReplaceAll // Substitutes the string toSub for each occurrence of the
string // toReplace in the array A[]. // //
. . . // Pre:
A[] contains Usage string values. //
. . . // Post:
Each occurrence of toReplace in A[] has been replaced with //
the string toSub. // Returns:
the number of replacements that were performed //
. . . // int
ReplaceAll(string A[], int Usage, const string& toReplace,
const string& toSub) {
int numReplacements = 0;
int Idx;
for (Idx = 0;
; Idx++) {
// Line 1
if (A[Idx] ==
) {
// Line 2
;
// Line 3
numReplacements++;
}
}
return numReplacements;
How should the blank in Line 1 be filled? Idx < Usage
How should the blank in Line 2 be filled? toReplace
How should the blank in Line 3 be filled? A[Idx] = toSub
|
Jun 21 |
|
8 |
Suppose we have the following array declaration:
const int Dimension = 7; int A[Dimension];
and that A has been initialized like this:
Index: 0 1 2 3 4 5 6 Value: 13 9 27 33 8 19 22
Suppose we make the following call to the function below: Mystery(A, 7);
void Mystery(int List[], int Size) { int Left = 0, Right = Size - 1; int Temp; while (Left < Right) { Temp = List[Left]; List[Left] = List[Right]; List[Right] = Temp;
Left++; Right--; } return; }
Show the contents of the array A after the function call has completed.
When we execute the body of the while loop the first time: Index: 0 1 2 3 4 5 6 Value: 13 9 27 33 8 19 22 Left Right
The first three statements in the body of the while loop just swap the values at indices Left and Right, so when we reach the body of the while loop the second time: Index: 0 1 2 3 4 5 6 Value: 22 9 27 33 8 19 13 Left Right
When we reach the body of the while loop the third time: Index: 0 1 2 3 4 5 6 Value: 22 19 27 33 8 9 13 Left Right
When we reach the body of the while loop the fourth time: Index: 0 1 2 3 4 5 6 Value: 22 19 8 33 27 9 13 Left Right
The loop terminates now... the effect of the function is to reverse the order of the entries in the array. |
Jun 20 |
|
6
7 |
Consider the function:
//////////////////////////////////////// validPrice // Takes a price, in dollars and cents, and //
determines if it is greater than or equal to zero. // // Parameters: // Dollars
dollar field of price // Cents
cents field of price // // Returns: true if the price is >= 0, //
false otherwise // // Pre:
Dollars and Cents have been initialized. // // Post:
None // // Called by: ?? // Calls:
None // bool validPrice(int Dollars, int Cents) { if
(Dollars < 0) return false; if
(Dollars == 0 && Cents == 0) return false; return
(true); }
Given what the function above does, how should the parameter Dollars have been passed?
by value by reference by constant reference
Add code below to show where and how the function above could be used to prevent processing an item with a "bad" price:
. . . // priming read (initializes Dollars and Cents, among //
other things) while
( In ) {
// perform calculations for this item and for summary
. . .
// write output for this item
. . .
// repeat priming read to read next item
. . . }
|
Jun 18 |
|
4
5 |
Consider the incomplete function:
////////////////////////////////////////////
printTime // Takes a time value, in minutes, and prints it //
to the output stream in
the format HH:MM. // //
Parameters: // Out output stream to which time will //
be written //
timeInMinutes integer
number of minutes to write // //
Returns: None // //
Pre:
Output stream has been opened. // timeInMinutes has been set to a //
nonnegative value. // // Post: The time, formatted as HH:MM has been //
written to the output stream. // //
Called by: main() //
Calls: None // void
printTime(ofstream Out, int timeInMinutes) {
const int MINPERHOUR = 60;
. . .
// other decl. here??
Hours =
timeInMinutes / MINPERHOUR;
Minutes = timeInMinutes % MINPERHOUR;
Out << Hours << ':'
<< setfill('0') << setw(2)
<< Minutes <<
setfill(' ');
return; }
How should each of the following parameters be passed? Out by value by reference timeInMinutes by value by reference
Where should the variable Hours be declared? (Circle one.)
-
in the function that calls printTime() - in the body of printTime() - globally
|
Jun 14 |
|
3 |
Consider executing the following code fragment:
1. If the user entered the value 30, what would be printed?
2. What value could the user enter to cause the value 'b' to be printed?
|
June 6 |
|
2 |
Consider executing the following code fragment:
1. If the user entered the value 5, what value would be printed for x?
2. If the user entered the value 42, what value would be printed for x?
|
June 5 |
|
1 |
Give an example of: 1. a valid C++ identifier.
2. an invalid C++ identifier.
|
May 29 |
Last modified: Thursday, June 21, 2001
Please send comments and suggestions to William McQuain at mcquain@cs.vt.edu.