Documente Academic
Documente Profesional
Documente Cultură
Directions:
Write your ID on every page of this exam. Write your name just on this cover page and on the first page of
questions.
Be sure to sign the honor code statement when you are finished.
Part
Description
Points
29
29
12
70
II
True/False
10
III
20
TOTAL
100
Score
Page 1
[70 pts.]
Answer each question completely, clearly, and concisely. Where short phrases will answer questions, theyll be fine.
Complete sentences written for the sake of complete sentences are NOT necessary. If a question asks you to list something, a
list is by all means the best answer. No comments are required in questions asking you to write code.
1. An error where the program does not solve the problem at hand correctly is called a(n) logic error.
[1 pt.]
2. A string is declared with the line of code below. How many characters long can the input be? 24
[2 pts.]
char inputLine[25];
Remember, the number of characters you can use is one less than the number
in [ ]. (We now know that extra character is the null character.)
3. A DVD will hold as much data as at most 6 CDs. Show calculations to support your answer below. (This question has two
possible solutions; your work must support your answer for credit.)
[4 pts.]
Solution A: CD holds 700 MB, DVD holds 4.7 GB = 4700 MB. So, 4700/700 = 6.xxx but we can't have a fraction of a CD,
so we round down to 6.
Solution B: CD holds 650 MB, DVD holds 4.7 GB = 4700 MB. So, 4700/650 = 7.xxx but we can't have a fraction of a CD,
so we round down to 7.
4. What is the difference between how errors are handled in compiled languages vs. how errors are handled in interpreted
languages?
[2 pts.]
In a compiled language, code must be error-free before the program can be run. In an interpreted language, the program can
run with errors and the interpreter deals with them on the fly, the best way it can.
5. Define the concept of abstraction and explain one example of where abstraction helps us in everyday life.
[4 pts.]
Definition: Focus on the WHAT, not the HOW. Ignore the details of a complicated process/device and worry about only
what we need to know to use the device.
Example (answers vary): To turn a television on with a remote control, one needs to know press a button on the remote and
point it at the TV. It is not necessary to know how the button press turns into a signal inside the remote, nor how the beam
travels to the TV, nor how the TV interprets it.
6. The ASCII code for w is 119. What is the ASCII code for W? Show supporting work.
[2 pts.]
There is a 32-character difference between ASCII codes for uppercase letters and their lowercase equivalents. Uppercase
come first. So 119 32 = 87.
7. What is the significance of Moore's Law?
[2 pts.]
[4 pts.]
Page 2
10. Write a block of code to compute, store, and then output the volume of a sphere. (Recall that volume V of a sphere of
radius r is given by V = (4/3)r3.) Assume all necessary #include statements have been provided. Variable declarations
have been provided to get you started.
[7 pts.]
double radius;
// radius of sphere in cm
double volume;
// volume of sphere in cm^3
volume = (4.0/3)*3.1415926*pow(radius,3);
cout << "Volume is " << volume << "cubic cm.";
// Watch that you don't say 4/3, which, by int arithmetic, is 1, not 1 1/3
11. Provided these declarations:
[5 pts.]
double num1;
// first input number
double num2;
// second input number
Write a line of code to output the difference of num1 and num2. If their values are 3.2 and 3.7, respectively, the output of
your code must be exactly as follows:
3.2 3.7 = -0.5
cout << num1 << " " << num2 << " = " << num1-num2;
12. Each of the following lines of C++ contains an error or errors. Tell what the error is and how to correct it.
a.
b.
ceil(num_leaves / 2) = height;
Misuse of assignment operator. Correct to:
height = ceil(num_leaves / 2);
c.
RETURN 0;
C++ is case sensitive. Correct to:
return 0;
[5 pts.]
13. Evaluate the following expressions. Use a decimal point to distinguish integer and floating point results. "Error" is a valid
response.
[12 pts.]
Assume these declarations precede these expressions:
double x = -7.4;
double y = 10.0;
double z = 2.5;
a. 5 / 2
=2
Page 3
14. Write the output generated by each code block. "No output" is a valid response.
a.
c.
delete record
b.
[12 pts.]
int g = 45;
cout << "g: " << g << endl;
if(g = 70)
cout << "at cutoff" << endl;
cout << "g: " << g << endl;
if(g = 1)
cout << "you get one" << endl;
cout << "g: " << g << endl;
g: 45
at cutoff
g: 70
you get one
g: 1
(Watch == vs =.)
d.
four
(Remember, C++ is case-sensitive)
[10 pts.]
Determine whether each statement of #1 to 6 is true or false. Determine whether each Boolean expression in #7 to 10
evaluates to true or false. Respond in the box at right by circling your response. One point each. No partial credit.
1.
2.
3.
4.
5.
6.
1.
true false
2.
true
3.
true false
false
4.
true false
5.
true
6.
true false
7.
true false
The declaration below applies to #10. Again, evaluate the Boolean expression.
int loc = 2;
8.
true false
9.
true false
false
Page 4
[20 pts.]
Write a full C++ program to solve the programming problem below. No header comments are required, but all other usual
comments are required. Choose the optimal programming techniques from those we've studied in class.
Suppose a particular river has been observed to overflow at a given location if its depth exceeds 100 feet, 5 inches.
Due to various environmental factors, the depth of the river is not constant, but we measure this depth at the
beginning of each month. The area goes through a "rainy" season, which lasts from December to April and during
August and September, when it is expected to receive an average of 6 inches of rainfall per month. Otherwise,
during the "dry" season (the other months of the year), we expect an average of 3 inches of rainfall per month.
Write a program that takes in the month and the observed depth of the river in feet at the beginning of that month
and uses the average rainfall for the month to output a message saying flooding is unlikely or flooding is likely at
this given location. (Assume no other variables affect our simple model.)
Sample interaction:
(input in italics)
#include <iostream>
using namespace std;
// get month
predicted_end_depth = start_depth*12;
if(month == 12 ||
(month >= 1 && month <= 4) || (month >= 8 && month <= 9))
{
predicted_end_depth += RAINY_SEASON_MONTHLY_RAINFALL;
}
else if(month >=5 && month <= 11)
{
predicted_end_depth += DRY_SEASON_MONTHLY_RAINFALL;
}
else
{
predicted_end_depth = -1;
}
// rainy season
// dry season
// "else" handles erroneous input
// invalid month
// force error message
if(predicted_end_depth < 0)
// print error message
{
cout << "Depth must be > 0 and month between 1 and 12." << endl;
}
else if(predicted_end_depth <= RIVER_OVERFLOW_THRESHOLD)
// print flooding unlikely response
{
cout << "Flooding is unlikely." << endl;
}
else
// print flooding likely response
{
cout << "Flooding is likely." << endl;
}
return 0;
}