<< Back

# Midterm 2 Review Supplement

1. Complete the following handwritten on paper and make sure to put your name on your paper.
2. Your work will be reviewed and graded, so make it presentable.
3. The following problems are due before the test starts.
4. Review of your work by another student is required for full credit. Add the following at the top of the first page of your paper:

Reviewed by: reviewing student's name

Discuss the issues and correct any problems found during the review.

### A. Loops and counting (5m)

1. Write the letter and heading for this problem on your paper.
2. Review loops and counting in lesson 5.1.6 .
3. Complete the following problems and answer any questions:
1. On paper, trace the following loop under the column headings showing both the value of `i` (including the initial value) and the terminal output for `i` in each iteration.
```for (int i = 2; i < 9; i = i + 2)
{
cout << i << endl;
}
```
i output

2. What does the loop do?
1. Prints all the numbers between 2 and 9.
2. Prints all the numbers between 2 and 8.
3. Prints the odd numbers from 1 to 9.
4. Prints the even numbers from 2 to 8.
3. The final value of i when the loop ends is ________.

#### Test Yourself (Optional)

1. The values of the counter variable in the following `for`-loop are ________.
```for (i = 5; i <= 10; i++)
```
1. i = 4, 5, 6, 7, 8, 9, 10, 11
2. i = 4, 5, 6, 7, 8, 9
3. i = 5, 6, 7, 8, 9, 10, 11
4. i = 5, 6, 7, 8, 9, 10
2. The values of the counter variable in the following `for`-loop are ________.
```for (i = 10; i >= 5; i--)
```
1. i = 10, 9, 8, 7, 6, 5, 4
2. i = 10, 9, 8, 7, 6
3. i = 9, 8, 7, 6, 5
4. i = 10, 9, 8, 7, 6, 5
3. True or false: the last value of a counter variable makes the test condition evaluate to `false` and causes the loop to exit.

^ top

### B. Arrays and Loops (4m)

1. Write the letter and heading for this problem on your paper.
2. Review arrays in lesson 7.2.5.
3. Complete the following problems and answer any questions:
1. Assume that "a" is an array with 10 elements. On your paper, write code to:
1. Set the value of the first element of array "a" to 42.
2. Set the value of the last element to 42.
3. Add the missing step so the `for`-loop prints all the array elements.
```for (int i = 0; i < _________ i++)
{
cout << a[i] << endl;
}
```
2. On your paper, trace the following algorithm through the entire array under the column headings shown below and then write the answer to the question in part d.
```  string numbers[ ] = {"one", "two", "three"};
for each array element
print the array element to the terminal
```
index Terminal display

3. What does the above algorithm do?
1. Displays several different numbers with an index value.
2. Displays a random number chosen from the array "numbers" and an index value.
3. Displays the values of the array to the terminal window
4. Displays a single number chosen from the array "numbers" and an index value.

#### Test Yourself (Optional)

1. An valid initialization of an array is ________.
1. `int myarray[] = { 0, 1, 2, 3, 4 }`
2. `int myarray[] = { 0, 1, 2, 3, 4, 5 };`
3. `int[] myarray = { 0, 1, 2, 3, 4 };`
4. `int myarray = { 0, 1, 2, 3, 4 };`
2. A valid line of code for displaying element index seven of an array named "myarray" is ________.
```int myarray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
```
1. `cout << myarray(7) << endl;`
2. `cout << myarray[7] << endl;`
3. `cout << myarray(8) << endl;`
4. `cout << myarray[8] << endl;`

^ top

### C. Indefinite Loops (5m)

1. Write the letter and heading for this problem on your paper.
2. Review section 5.3.3: Processing a Sequence of Inputs.
3. For the following code, on your paper trace the value of variable `sum`, including the initial value, and what is printed to the console if the user enters: `4, 5, -1, 7`
```double sum = 0;
cout << "Enter a number or 0 to exit: ";
cin >> input;
while (input >= 0) {
cout << "So far, sum = " << sum << endl;
sum = sum + input;
cout << "Enter a number or 0 to exit: ";
cin >> input;
}
cout << "Ending sum: " << sum << endl;
```
sum input output

4. After tracing, circle the final value of sum.

#### Test Yourself (Optional)

1. The first (initial) value of `sum` is ___.
2. The last value of `sum` is ___.
3. The while loop repeats this many times: ___.

^ top

### D. Calling functions (4m)

1. Write the letter and heading for this problem on your paper.
2. Review section 7.4.6: Flow of Control for a Function Call.
3. On your paper, write valid code that calls (invokes) each of the following functions. For those functions that return a value, assign the returned value to a variable of the correct data type. For example, we could call the function `int foo(int bar)` with
```int x = foo(42);
```
1. `void play()`
2. `int addAmount(int amount)`
3. `bool isCool(bool testValue)`
4. `void printCompare(int x, int y, int z)`
5. `double max(double x, double y)`

#### Check Yourself (Optional)

1. Every program starts executing at the start of the function named __________.
2. True or false: when reaching a function call, the currently executing function stops and control jumps to the start of the called function.
3. True or false: after a called function returns, control returns to the point in the code just after where it was called.

^ top

### E. Defining functions (7m)

1. Write the letter and heading for this problem on your paper.
2. Review lesson 8.1.1: Review of Functions.
# Function name Return type Number of parameters
4. On paper, write the question numbers (##), function name, return type, and number of parameters for the following functions under the column headings: (4m)
1. `void play()`
2. `void addAmount(int amount)`
3. `bool isCool(bool testValue)`
4. `void compare(int x, int y, int z)`
5. `bool isGreater(int number)`
5. Write a complete definition for a function named `add` that takes two parameters, sums the parameters and returns the sum as a type `double`.

^ top

### F. Objects and classes (5m)

1. Write the letter and heading for this problem on your paper.
2. Review lesson 10.1.1: Review of Objects and Classes
3. Design the interface and write the declaration for a class named `Rectangle`. Include the following class members and write the item numbers next to the code as comments like: // 1
1. Two private member variables named: "width" and "height".
2. A default (no parameter) constructor that sets default values for all member variables.
3. A constructor with two parameters, one for each member variable, that assigns the appropriate parameter to each member variable.
4. A member function "read" that gets a value for each member variable from the user with appropriate prompts.
5. An accessor function named "print" that displays the width, height and area of this Rectangle object to the terminal window.

Do NOT write the implementation (definition) of the class functions at this time. Only write the function prototypes.

^ top

### G. Constructors (5m)

1. Write the letter and heading for this problem on your paper.
2. Review lesson 9.2.5: Coding Constructors, 9.3.7: Summary -- Constructors
3. On paper, write the question numbers and the answers for the following questions:
1. Given a class named `WildWorld`, write a constructor prototype with no parameters.
2. Given a class named `Cat`, write a constructor prototype with two parameters: name, age.
3. Given an class named `Student`, write a constructor prototype with three parameters: name, id number, and GPA.
4. The name of the class that contains this code is _____.
```public:
RabitPatch();
private:
Dog dog;
Rabbit rabbit;
```

#### Test Yourself (Optional)

1. True or false: when a variable is not assigned a value in C++, its value is automatically set to zero (0).
2. True or false: the purpose of a constructor is to initialize all the member variables.
3. True or false: a constructor has the same name as the class.
4. True or false: the return type of a constructor is always `void`.

^ top

### H. Get and Set Functions (5m)

1. Write the letter and heading for this problem on your paper.
2. Review lesson 9.3.7: Summary -- Get and Set Functions
3. On paper, write the question numbers and the answers for the following:
1. Given a class named `Cat` with the following data members, write a get and set function definition for each data member external to the class:
```private:
string name;
int age;
```
2. Given a class named `Student` with the following data members, write a get and set function definition for each data member external to the class:
```private:
string name;
int age;
double gpa.
```
3. Given a class named `RabbitPatch` with the following data members, write a get and set function definition for each data member external to the class:
```private:
Dog dog;
Rabbit rabbit;
```

#### Test Yourself (Optional)

1. True or false: we should declare all member variables `private`. Why?
2. True or false: the usual naming convention for `get` or `set` functions is to use the word "get" or "set" followed by the name of the value being returned.
3. True or false: every member variable must have a get- and set-function.

^ top

<< Back