<< Back

# Exam 1 Review Worksheet

## Goal

• Practice problems to solve in preparation for the midterm.

## Project Specifications

1. Complete the following worksheet 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. We will start or review the worksheet in part during class. During the class meeting, arrange with a classmate to review your worksheet before turning it in.
4. Review of your work by another student is required for full credit. Some ways to review with another student include:
• Take a picture and send it to another student
• Hold up your paper to the camera during a Zoom call
• Describe problems and answers verbally during a phone or Zoom session
5. Add the following at the top of the first page of your paper:

Reviewed by: reviewing student's name

6. Discuss the issues and correct any problems found during review.
7. Turn in your handwritten worksheet by taking a picture of each page, pressing the Submit Assignment button above, and uploading the pictures. It is your responsibility to check the quality of each picture.

## Exam Review Worksheet

Problems: A   B   C   D   E   F   G   H

### A. Data Types

1. On your paper, create three columns with these headings as shown (underlined) along with the list of numbers:
```Numbers    Type/error    Description of type/error
3
-3
3.0
3.14
3 14/100
300,000
3e7
'3'
"3.14"
"300,000'
```
2. Assuming each of the "numbers" in the list are examples of a single C++ code value being assigned to a variable:
1. In the second column, write the type of the number (such as `int`) or the word error if the number would produce an error in code.
2. In the third column, describe the type of the number (such as `integer number`) or the coding error (such as `numbers cannot have commas`).

Hint: To verify the numbers use a C++ compiler.

^ top

### B. Variables and Assignment

On your paper, write the problem number (1a, 1b, ..., 2, 3) and a solution to the following problems:

1. Declare a single variable and assign the variable a value for storing each of the following types of data:
1. Dollars and cents
2. The count of the number of people in a room
3. A single letter
4. Whether or not a door is open or closed.
5. A sequence of characters making up a person's name.
2. Write the value of a and b on your paper after the following code snippet executes
```int a = 10;
int b = 20;
a = b;
```
a is ________ b is ________
3. Write the value of a and b on your paper after the following code snippet executes
```int a = 7;
int b = 5;
a = b;
b = 3;
```
a is ________ b is ________

^ top

### C. Math expressions

On your paper, write the problem number (1, 2, ...) and a solution for the following problems:

1. First operation performed in `1 + 2 * 3 / 4 % 5` is ________.
2. The value of x after the following code executes is ________.
```double x = 9 / 4;
```
3. The value of `x` after the following code executes is ________.
```double x = 1 + 2 * 3 / 4 % 5;
```
4. Write the following math expression as C++ code: Hint: Verify your computations by trying them in C++.

^ top

### D. Strings and Substrings

On your paper, write the question numbers (1, 2, ...) and the C++ code to extract the last word (without punctuation) from each of the following strings.

1. `string str = "Do. Or do not. There is no try."; // George Lucas (Yoda)`;
2. `string str = "Whether You Believe You Can Do a Thing or Not, You Are Right."; // Henry Ford`;
3. `string str = "It always seems impossible until it's done."; // Nelson Mandela`;
4. `string str = "If you're going through hell, keep going."; // Winston Churchill`;
5. `string str = "No one can make you feel inferior without your consent."; // Eleanor Roosevelt`;

Hint: Verify your substring by writing code to display the substring in C++.

^ top

### E. String Input

On your paper, write the question numbers (1, 2, ...) and the answer for each of the following input statements.

1. What is read into string `myString` for input "`One-hit wonder`".
`cin >> myString;`
2. What is read into string `myString` for input "`One-hit wonder`".
`cin >> x >> myString;`
3. The programmer expected `myString2` to contain "`Boys`" but instead `myString2` is blank. Why?
```cin >> myString1;
getline(cin, myString2);
User input:
Girls
Boys
```

^ top

### F. Test Conditions

On your paper, write the question numbers (1, 2, ...) and the value (`true` or `false`) for the following test conditions, given `x = 3`, `y = 7`.

1. `(x != y)`
2. `(x == y)`
3. `(x < y)`
4. `(x >= y)`
5. `(x <= 42`)

^ top

### G. Tracing `if`-statements

On your paper, write the problem numbers (1, 2, ...) and a solution to the following problems:

1. The value of x after the following code executes is ________.
```int x = 7;
if (x == 3) {
x = 1;
}
```
2. The value of x after the following code executes is ________.
```int x = 3;
int y = 4;
if (x < y)
{
x = y;
}
```
3. The value of x after the following code executes is ________.
```int x = 5;
if (x > 3)
{
x = x - 2;
}
else
{
x = x + 2;
}
```
4. The value of x after the following code executes is ________.
```int x = 1;
if (x > 0)
{
x = x + 5;
}
else if (x > 1)
{
x = x + 2;
}
else
{
x = x + 7;
}
```

^ top

### H. Logical Operators

On your paper, write the problem numbers (1, 2, ...) and the code for the following test conditions using logical operators:

1. A restaurant gives a discount for children under 10. They also give the discount for adults over 55. Write the test condition for a single `if`-statement:
```if ( test condition goes here ) {
cout << "apply discount" << endl;
}
```
2. Both must be true for a person to ride: (1) At least 5 years old, (2) Taller than 36 inches. Write the test condition for a single `if`-statement:
```if ( test condition goes here ) {
cout << "can ride" << endl;
}
```
3. We want to test the variable `x` for a value between 50 and 100, including 50 and 100. Write the test condition for a single `if`-statement:
```if ( test condition goes here ) {
// Output "50, 51, ..., 99, 100"
}
```

^ top

<< Back