Sunteți pe pagina 1din 5

CMPSC 201

C++ Programming for Engineers


Fall 2008

Exam I: Introductory Concepts, C++ Basics and Arithmetic, Selection


September 23, 2008
Name: ___________SOLUTION____________________________

Last 4 digits of ID: ____ ____ ____ ____

Honor Code Statement


I certify that I have not discussed the contents of the exam with any other student and I will not discuss the contents
of this exam with any student in this class before it is returned to me with a grade. I have not cheated on this exam in
any way, nor do I know of anyone else who has cheated.
Signature ________________________________________________________________________________

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.

No notes, books, or calculators are permitted.

See directions at the beginning of each part of the exam.

Be sure to sign the honor code statement when you are finished.

Part

Description

Points

Free Response, Page 1

29

Free Response, Page 2

29

Free Response, Page 3

12

Free Response Total

70

II

True/False

10

III

Programming Application Problem

20

TOTAL

100

Score

CMPSC 201 Exam 1, Fall 2008

Page 1

Last 4 Digits of PSU ID #: ___ ___ ___ ___

Part I: Free Response

[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.]

Processor speeds will double every 12-18 months.


8. List the five phases of software development, in order from start to finish.

[4 pts.]

Analysis, Design, Implementation, Testing, Maintenance


9. After the following block of code has run, what is the value of each variable? Write values in the box. Show supporting
work for partial credit.
[8 pts.]
double a = 3.8;
a becomes 3.8
double z;
int n = 2;
n becomes 2
a: 3.8
z: 6.8
int y;
n = (a/n)*2;
n = (3.8/2)*2 = 3.8, but integer, so it's truncated to 3
n: 3
y: 6
y = n + 3;
y=3+3=6
z = (y+1)/2 + a;
z = (6+1)/2 + 3.8 = 7/2 + 3.8 = 3 + 3.8 = 6.8 (types count!)

CMPSC 201 Exam 1, Fall 2008

Page 2

Last 4 Digits of PSU ID #: ___ ___ ___ ___

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.

cout >> "Hello world!";


>> should be <<

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

b. pow(y, 3) = 10.03 = 1000.0

c. sqrt(x) (error, need positive input to sqrt)

d. fabs(x+2) = |-7.4 + 2 | = |-5.4| = 5.4

e. fabs(x)+2 = |-7.4| + 2 = 7.4 + 2 = 9.4

f. pow(ceil(x), floor(z)) = (-7)2 = 49

CMPSC 201 Exam 1, Fall 2008

Page 3

Last 4 Digits of PSU ID #: ___ ___ ___ ___

14. Write the output generated by each code block. "No output" is a valid response.
a.

char option = 'd';


if(option == 'a')
{
cout << "add record";
}
if(option == 'd')
{
cout << "delete record";
}

c.

delete record
b.

int grade = 45;


if(grade >= 70)
{
cout << "passing" << endl;
}
if(grade < 70)
{
cout << "dubious" << endl;
}
if(grade < 60)
{
cout << "failing" << endl;
}
dubious
failing

[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.

char input = 'q';


switch(input)
{
case 'A':
cout << "one";
break;
case 'D':
cout << "two";
break;
case 'Q':
cout << "three";
break;
default:
cout << "four";
}

four
(Remember, C++ is case-sensitive)

Part II: True/False

[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.

C++ is not case sensitive.


Statements and declarations must end with semicolons.
The result of an integer division is a rounded result.
123 is a valid identifier in C++.
___1___ is a valid identifier in C++.
double is a valid identifier in C++.

1.

true false

2.

true

3.

true false

false

4.

true false

For #7 to #9, a1 is true and a2 is false. Evaluate the Boolean expressions.


7. a1 && a2 xxxx
8. !(a1 || a2) xx
9. !a1 && a2 xxx

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

10. (loc < 2 || loc > 2) xxx


xx

false

10. true false

CMPSC 201 Exam 1, Fall 2008

Page 4

Last 4 Digits of PSU ID #: ___ ___ ___ ___

Part III: Programming Application Problem

[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;

Enter the number of the current month: 11


Enter river depth observed on the 11/1 in feet: 100.25
Flooding is likely!

const double DRY_SEASON_MONTHLY_RAINFALL = 3;


const double RAINY_SEASON_MONTHLY_RAINFALL = 6;
const double RIVER_OVERFLOW_THRESHOLD = 100*12+5;
int main()
{
int month;
double start_depth;
double predicted_end_depth;

// avg. inches of rain per month in dry season


// avg. inches of rain per month in wet season
// how many inches deep river can be at this
//
location before it overflows

// number of month in question


// depth of river on 1st day of month, in feet
// predicted ending depth of river, in inches

cout << "Enter the number of the current month: ";


cin >> month;
cout << "Enter depth of the river observed on the first"
<< " day of this month (in feet): ";
cin >> start_depth;

// get month

predicted_end_depth = start_depth*12;

// start by converting feet to inches


// (12 inches = 1 foot)

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;
}

// get start depth

// 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;
}

S-ar putea să vă placă și