CMSC 201 SAMPLE TEST 2 Name_______________
Total Points: 100 Point values in margin.
(20) 1. SHORT ANSWER.
a) Name one simple data type.
b) Name one structured data type.
c) What is a C++ string?
d) What is the base address of an array?
For parts e) thru m) write one or more C++ statements for each.
Assume the following preprocessor directive and declaration.
#include <string.h>
int x;
e) Declare an array called temps to hold up to 20 float values.
f) Assign 98.6 to the third element in the temps array.
g) If the first element of temps array is greater than 0.0
then add 54.5 to it.
h) Assume that all 20 elements of the temps array have
already been assigned values.
Write statements to find the smallest element of the
temps array. (Use a for loop.)
i) Declare an array of up to 30 characters called title.
j) Declare an array of up to 20 characters and give it
value "June" in its declaration.
k) Assign to x the length of the string called title.
l) Assign to x the value 15 if the string in array
title is different from the string in array month.
m) Copy the string from the array called month to the
array called title.
(10) 2. Use a nest of loops to send this exact output to the screen.
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J
A B C D E F G H I J K
(5) 3. A program contains the following function prototype
int windchill (int, int);
and the calling block contains these declarations:
int result, wind, temperature;
Tell whether each statement in the calling block is
VALID or NOT VALID.
a) if (windchill(wind, temperature) < -10)
cout << "feels very cold";
b) cout << windchill (wind, temperature) << endl;
c) cin >> windchill(wind, temperature);
d) windchill(wind, temperature) = 20;
e) amount = windchill(25, 50);
(10) 4. Give the output for the following program.
#include <iostream.h>
void quiz( int &, int );
int main(void)
{
int a, b, c;
a = 5;
b = 6;
c = 7;
quiz(b, c);
cout << "a=" << a << " b=" << b << " c=" << c << endl;
return 0;
}
void quiz( int& x, int y )
{
int a = 4;
cout << "a=" << a << " x=" << x << " y=" << y << endl;
y = y + 4;
x = x + y;
cout << "a=" << a << " x=" << x << " y=" << y << endl;
}
(5) 5. SHORT ANSWER.
a) In the program above, can the call quiz(b, c) be replaced
by the call quiz(6, 7) ?
b) In the program above, name the value parameters.
c) In the program above, name the reference parameters.
d) In the program above, name the actual parameters.
e) When you use pass-by-reference for a parameter,
what is sent to the function?
f) If a function contains a statement that changes a value
parameter, is the actual parameter is changed?
(5) 6. Fill in each blank with the most appropriate one of these comments
/* in */ /* out */ /* inout */
to correctly describe the direction of data flow for parameter
gamma for each function definition.
a) void FuncOne ( ___________ int& gamma )
{
gamma = 1;
}
b) void FuncTwo ( ___________ int gamma )
{
cout << 2 + gamma;
}
c) void FuncThree ( __________ int& gamma )
{
gamma = 3 + gamma;
}
(10) 7. A) Write a void function called getAnInteger( ) with two
integer value parameters lowestValid and highestValid,
and one integer reference parameter number.
Your function repeatedly prompts the user to enter at the
keyboard an integer between lowestValid and highestValid.
When the user does enter a number in this range, your
function returns with that number in the reference parameter.
Use the heading below.
void getAnInteger(/* in */ int lowestValid,
/* in */ int highestValid,
/* out */ int& number)
//-----------------------------------------------------------------
// Precondition: lowestValid and highestValid have been assigned.
// Postcondition: number has been assigned a value
// from the keyboard such that
// lowestValid <= number, and number <= highestValid.
// User was repeatedly asked to enter number until successful.
//----------------------------------------------------------------
b) Write a driver for your function.
(10) 8. Write a function called minimum( ) with return type float,
and two value parameters of type float. Your function
should return the smaller of its two value parameters.
For example, minimum(8.6, 7.31) has value 7.31.
(10) 9. a) What is the purpose or effect of using const in
the following prototype?
int countLetter(const char message[ ], char ch);
b) Write a function with the prototype above.
Your function returns the number of times that the
character ch appears in the string message.
For example, countLetter("tomorrow", 'r') has value 2.
(10) 10. Write a function with the following heading to search an unordered
array of integers containing length elements.
The function returns 1 if item is in the array.
The function returns 0 otherwise.
int search(const int list[ ], // list to be searched
int item, // item to search for
int length) // length of list
(5) 11. Write C++ statements for each task.
a) Define a data type AddressType to be an array of at most 40
characters.
b) Declare a variable myAddress to be of type AddressType.
c) Declare employeeAddresses to be a 100-element array variable
whose elements are all of type AddressType.
d) Assign the string value "7201 Rossville Blvd." to
employeeAddresses[6].
Last updated: Nov. 8, 1999