## CS 1044 Quizzes - Summer I 2001

Notice:  this website is best viewed with Internet Explorer version 5.0 or later.

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 ) {

if ( validPrice(Dollars, Cents) ) {

// 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:

int A;

cin >> A;

if ( A < 10 ) {

cout << 'a' << endl;

}

else if ( A < 20 ) {

cout << 'b' << endl;

}

else {

cout << 'c' << endl;

}

1.  If the user entered the value 30, what would be printed?

The first condition (A < 10) would be false, so execution would flow to the first else clause.  The condition for the if there (A < 20) would also be false, so execution would flow to the next else clause and the value 'c' would be printed.

2.  What value could the user enter to cause the value 'b' to be printed?

The value of A must make the first condition (A < 10) false, but make the second condition (A < 20)true.  That means we must have 10 <= A < 20, so any integer value from 10 to 19 would work.

June 6

2

Consider executing the following code fragment:

int x;

cin >> x;

switch ( x ) {

case 5:  x = x + 1;

break;

case 7:  x = 5;

break;

}

cout << "x is now " << x << endl;

1.  If the user entered the value 5, what value would be printed for x?

This would cause the execution of the first case, setting the value of x to 6.  The break would then exit the switch statement and the output statement would execute.

2.  If the user entered the value 42, what value would be printed for x?

Neither case matches this value of x, so the switch would not change the value of x; hence 42 would be printed.

June 5

1

Give an example of:

1.  a valid C++ identifier.

There are obviously lots of correct answers here.  Basically you just had to remember the restrictions.  The first character must be an underscore or a letter, the remaining characters can be letters, digits or underscores (and nothing else).  And, an identifier cannot be one of the C++ keywords, such as const or double.

2.  an invalid C++ identifier.

Again there are lots of correct answers.  You just have to violate one or more of the restrictions stated above.

May 29