Answers to Exam Preparation Exercises (revised as of 11/20)

Chapter 2


a. XYZ Invalid -- must end with 1, 2, or 3
b. 123 Invalid -- must start with X, Y, or Z
c. X1 Valid
d. 23Y Invalid -- must start with X,Y, or Z
e. XY12 Valid
f. Y2Y Invalid -- must end with 1, 2, or 3
g. ZY2 Valid
h. XY23X1 Valid

3. a.invalid b.valid c.invalid d.valid e.valid

6. a. 27 b. 13 c. 5 d. 0 e. 3 f. 8 g. 3

8. False. Reserved words are just that -- reserved for use by C++. They may not be used as variable names.

9. False. C++ is a case-sensitive language. The word main must be entirely in lowercase letters.


a. a = 5b =2
b. Sum : 7
c. Sum : 7
d. 2 feet


Chapter 3

2. a. 3 b. 4 c. 37 d. 22 e. 23 f. 5

3.y = -b + (b2 - 4ac)1/2

4. a. 0.235294 b. 2.25 c. 44.2 d. 5 e. 0 f. 21 g. 8 h. 5 i. 1 j. 3

6. a. 9.1 b. 7.0 c. 24 d. 16.0 e. 5.0 f. 3.0

7. Below, _ denotes a blank.

_ _ 413_is the value of n
_ _ _21.8_is the value of y

8. Below, _ denotes a blank.

x is 14.38
x is _ _ _14.38
x is14.38
x is_14.383

10. Any two of the following: comments, meaningful identifiers, formatted lines with spaces, identation.


Chapter 4

1. The main advantage of having a program input its data rather
than writing all of the values as constants is that the program does
not have to be changed when a new set of data needs to be run through
it. If all values were declared as const data, those values would
have to be modified and the program recompiled every time it was run.
When the program inputs the values, it can run on many different data
sets without modification of the source code.

3. a. A newline character is entered from the keyboard by
pressing the Return or Enter key.
b. A program can generate a newline character in its output by
either using the endl manipulator or by explicitly outputting the
single character denoted by the symbols \n.

4. The operator skips leading whitespace characters (if any),
whereas the get function does not. The get function inputs the
next character waiting in the input stream, even if it is a
whitespace character.

5. True (with the understanding that an integer input value is
converted into floating point form before it is stored into the float variable)

7. a contains 14,b contains 19, c contains 67, d contains 73

9. a. I contains 11, x contains 12.35, ch1 contains 'A'
b. I contains 1, x contains 12.35, ch1 contains '1'

10. An input prompt is a message printed out directly before an
input statement, telling the user what kind of input is expected by
the input statement. Echo printing refers to printing out the values
of variables directly after they are read, in order to verify that
the variables contain the proper values.

13. Programs that are designed using a methodology such as
top-down design or object-oriented design are readable,
understandable, and easy to debug and modify.


Chapter 5

1. a. T b. T c. T d. F

3. MadamNowI'mAdam

4. a. 10
b. The value of x is 3
c. 3
   z (Despite the indentation, the final output statement
     is not part of the else-clause or even part of the If statement).

5. a. Eligible to server.
   b. Too short and too light to server.

7. a. x <= z
   b. x 0 && y 0 && z 0
   c. x!= y && x!= z
   d. x = = y && x = = z

8. a. T   b. F   c. F   d. T   e. T

11. To fix the dangling else problem, insert braces as follows:
if (typeA || typeB)
    if (typeA && typeB)
        cout << "Type AB";
    cout << "Type O";

Set 1    'A' 'A' 'A'    All initials are the same.
Set 2    'A' 'A' 'B'    First two are the same.
Set 3    'B' 'A' 'A'    Last two are the same.
Set 4    'A' 'B' 'A'    First and last are the same.
Set 5    'A' 'B' 'C'    All initials are different.

14. In the left-to-right evaluation, only the first two relational
expressions are evaluated. the first yields TRUE, so evaluation
proceeds. The second yields FALSE, so evaluation stops. The final
result is FALSE.


Chapter 6

1. Loops are segments of code that are repeated from 0 to many
times until some stopping condition is met. Branches are segments of
code that may be executed 0 or 1 times, depending on evaluation of
the logical expression in the If statement.

2. The loop prints the integers 2 through 11, one number per line.

5. In the output below, the first two digits on each line are
values of j, and the third digit is the value of i

2 1 4
2 1 3
2 1 2
2 1 1

6. a. 4 6 8 10 12 14 16 18 20 and so on
b. The two flaws are the initial value of n and the while
condition. n is initially set to 2, but the 2 is not printed before n
is incremented to 4. This flaw can be fixed by incrementing n after
the output statement instead of before it. The loop never stops
because n never equals 15; n always contains an even number. This may
be corrected by changing the While condition to n&lt;15. The
corrected code is:

while(n < 15)
cout <<< ' ';
n = n + 2;

7. a. The output is BCDE, followed by an echo of the newline
character. The 'A' that is input by the priming read is discarded
because the statements in the loop body are in reverse order.

b. Corrected code:

while(inChar!= '\n')
cout << inChar;

8. One (and only one) priming read is needed. It precedes the
outer While loop:


This input statement primes not only the outer loop but also the
inner loop.

11. a. After exit from the loop, sum contains 18 and number
contains 0.
b. No, the given data set does not fully test the program.
Another set needs to be tested in which the value of i reaches its
limit, another in which the first data item is the sentinel value 0,
and yet another in which all data values before the sentinel are less
than zero.

12. Given the program code

sum = 0;
sumSquares = 0;
count = 0;
cin number;
sum = sum + number;
sumSquares = sumSquares + number * number;
cin number;

Every input operation prior to the current one succeeded AND count
contains the number of input values preceding the urrent one AND sum
contains the sum of the input values preceding the current one
AND sumSquares contains the sum of the squares of the input values
preceding the current one

14.   1 3 6 10 15

Chapter 9

Switch expression -- the expression in a switch statementwhose value determines which switch label is selected

Pretest loop -- A loop in which the loop test occurs before the body of the loop

Posttest loop -- A loop in which the loop test occurs after the body of the loop


3. True



switch (n)


case 3: alpha++;


case 7: beta++;


case 10: gamma++;

break; //optional



7. false. The loop condition in both cases is delta <= alpha.

8. True

10. Nothing is printed. While and For are pretest loops. Here, the loop condoition is immediately FALSE and the body is skipped.


4 3 2 1 4

3 2 1 3

2 1 2

1 1


12. In the following output, note that there will be a blank line printed following the last line of stars.

********* *********

********   ********

*******     *******

******       ******

*****         *****

****           ****

***             ***

**               **

*                 *


14. e

Chapter 7

1. Function call -- The mechanism that transfers control to the body of a function

Parameter list -- A mechanism by which functions communicate with each other; the list of variables and/or expressions following the name of a function that determines how the function and the calling code communicate with each other

Parameterless function -- A function that has no parameters; a function whose parameter list is empty

Formal parameter -- A variable declared in a function heading

Actual parameter -- A variable or expression listed in a call to a function

Local variable -- A variable declared within a block and not accessible outside of that block


void Test(int , int , int); // Function prototype

// Function Definition

int main() // Function Heading


int a; //Local Variable

in b;

int c;


Test( a, c, b); //Function Call

Test(b, a, c);



// Function definition

void Test(int d, int e, int f //Formal Parameters) // Function Heading


int g;

int h; //Local Variable



3. First call to test


1. d a

2. e c

3. f b

Second call to test


1. d b

2. e a

3. f c


5. True

6. True


9. widgets and clunkers refer to the same memory location, so after the assginment statement both widgets and clunkers have the value 77.

12. In function Tet, the variables equal 5 20

In the main function after the first call, the variables equal 5 14

In function Test, the variables equal 5 20

In the main function after the second call, the variables equal 15 5



1 cout << "Exercise ";

2 DoThis(number1,number2);

5 cout << number1 << ' '<<number2 << endl;

3 cin >> value3 >> value1;

4 value2=value1+10;


14. In the main function just bfore execution of the return statement, number1 contains 15,

number2 contains 25, and value 3 does not exist (it is local to the DoThis function)


Chapter 8

2. True
3. Local Variable -- A variable declared within a block and not
accessible outside of that block

Global Variable -- A variable declared outside of all functions

Lifetime -- The period of time during program execution when an
identifier has memory allocated to it

Scope -- The region of program code where it is legal to reference
an identifier

Side Effects -- Any effects of one function on another that are
not part of the explicitly designed interface between them ( for
example, use of global variables, or modification in a function of a
parameter that should be a value rather than a reference parameter)

Name precedence -- The precedence that a local identifier has over
a nonlocal identifier with the same name when the local identifier is
refernced within its block

x = 3 after the call to DoReference.
x = 16 after the call to DoValue.
x = 17 after the call to DoLocal.
x = 7 after the call to DoGlobal

6. Within the function, n is decremented down to zero. Because n
is passed be reference rather then by value, the caller's second
parameter contains zero upon return from the function.

8. Function Definition:
float Power(float base, int exponent)
float answer = 1.0;
int i = 1;
while(i <= exponent)
return answer;

Function Call:
m = power (k,1);

9. a. -0.5
b. 0.5

10. a. A return statement with an expression after the word return
is used in value-returning functions, not void functions.
b. This value-returning function is missing a return statement to
return the function value.


Chapter 11

2. False

3. a. float floatArray[24];

b. int intArray[500];

c. double doubleArray[50];

d. char charArray[10];

4. const int CLASS_SIZE = 30;

float quizAvg[CLASS_SIZE];

6. a. for (cIndex = BLUE; cIndex <= BLACK; cIndex = Colors(cIndex+1))

b. for (rIndex = 0; rIndex < MAX_LENGTH; rIndex++)

c. int counter = 0;
for (rIndex = 0; rIndex < MAX_LENGTH; rIndex++)
if(rainbow[rIndex] == GREEN)

d. cout << count[BLUE] << endl;

e. int sum=0;
for (cIndex = BLUE; cIndex <= BLACK; cIndex = Colors(cIndex+1))
sum = sum + count[cIndex];

8. The For statement incorrectly causes index to range from 1
through 4 instead of 0 through 3. Output of the (nonexistent) array
element arr[4] accesses the memory location just beyond the end of
the array. This memory location evidently contained the value 24835.

10. sample: [0] [1] [2] [3] [4] [5] [6] [7]
1 1 1 1 -1 -1 -1 -1

11. sample: [0] [1] [2] [3] [4] [5] [6] [7]
0 101 2 103 4 105 6 107

Chapter 13


Qs 2.    a. 10     b. 3     c. 3      d. 10     e. 30     f. 30      g. row       h. row

Qs 3  a.




              enum ResultType {WON, LOST, TIED};

Qs 6. a.  valid

Qs 7. a. Boolean table[5][6];

Qs 9  a. float arrayA[10][7][21];