CSCI 203 Exam 2 Review Questions




The questions for exam 2 will be taken from/similar to/based on the following, and on labs and projects you've completed so far, and on the material from Chapters 1-10.4 of Mercer (see especially the self-check questions).
 
1. Identify the following:

2. Given the following declarations, what values do the expressions evaluate to?

int a(2), b(3), c(5);
double x(3.2), y(2.0);

!((a < c) && (x > y))

b != a || x == y

a <= c && c <= b

3. Write a method computeInterest() for the bankAccount class that increases the balance data member by 5% (i.e., balance is multiplied 1.05) and returns the new balance. Show what you would add to the bankAccount.h and bankAccount.cc files.

4. Write a C++ program that computes a final price of a purchase. The program does the following: It reads in a value representing the amount of a purchase. If the purchase is under $5, there is no discount; if the purchase is greater than or equal to $5 and under $10 dollars, there is a 5% discount (the purchase amount is multiplied by .95 to get the final price); if the purchase amount is $10 or more, there is a 10% discount (the purchase price is multiplied by .9). The program then prints out the final price.

5. Rewrite the following code to use a while loop in place of the for loop. The behavior of your solution should be the same as that of the original.

int nSquared = 0;

for (int i = 0; i < n; i = i+1)
{
    nSquared = nSquared + 2*i + 1;
}

6. Write the C++ code that does the following: it prompts the user to enter the name of a file, reads a file name from the user, and tries to open that file in an input filestream. If the file name is bad (i.e., the file can't be opened), the user is re-prompted for a good file name, and the process repeats until a file is successfully opened. You may assume that all necessary #include directives are already provided, and that you only need to provide the lines of code to do what is specified. Be sure to declare all objects that you use, however. Remember that the string class method c_str() method converts a string object into a C-style string.

7. Write a C++ program to read a set of string objects from a file named words, then prints the strings (one per line) in the reverse of the order in which they appear in the file. You can assume that there are no more than 20 strings in the file, though you don't know the exact number a priori.

8. What factors must you consider in designing a repetition; selection?

9. Write a function findUnderTen that takes as arguments a vector of integers ans the number numVals of objects in that vector. The function returns the position of the first integer in the vector that is less than 10. If there is no such value in the vector, numVals is returned.

10. Write the declaration of a vector of 25 double objects, each initialized to 1.412.

11. Write a C++ function getBoundedUL() that takes a string argument prompt and two double arguments low and high. It prints the prompt, then reads in a double value. If the value lies between low and high, it is returned. Otherwise, the user is prompted again, and new value is read. This continues until a value between low and high is entered, in which case it is returned.

12. Write the pre- and postconditions for the getBoundedUL() function in the preceding problem.

13. Write a C++ program that reads a sequence of positive int values from input, keeping track of the largest value seen so far. When a value less than or equal to 0 is entered, input terminates and the largest value is printed with an explanatory message. Hint: you do not need to use a vector to solve this problem. In fact, it just complicates things...

14. What is the output of the following program (note that the definition of f2() has changed from the first review sheet)? Show the environments of the function calls.

#include <iostream>

int f1(int j)
{
    int k = 5;
    j = k * j;
    cout << "f1: " << j << endl;
    return j;
}

int f2(int & k)
{
    k = 2 * f1(k);
    cout << "f2: " << k << endl;
    return k;
}

int main()
{
    int j = 1, k = 2;

    cout << "j = " << j << "; k = " << k << endl;
    k = f2(j);
    cout << "j = " << j << "; k = " << k << endl;

    return 0;
}