Name:__SOLUTION__________________ Alpha ____________ Instructor: ________
IC210
Sample Written 6Week Exam
ANSWERS SHOWN IN RED
This is a multi section exam.
You may NOT communicate about this exam with anyone using any medium until
your instructor tells you that you can.
This exam is closed book and no electronic device can be used.
Problem 
Points Avail 
Score 
1. 
15 

2. 
12 

3. 
6 

4. 
13 

5. 
12 

6. 
8 

7. 
4 

8. 
10 

9. 
10 

10. 
10 

Total 
100 

1. (15 Points) True or False: the following variable names are legal, and if False, why?
(a) T / F thisIsARidiculouslyLongVariableName
(b) T / F 2nd_company – can’t start
with a number
(c) T / F Navy#1  # can’t be used in a variable name
(d) T / F USNA
(e) T / F double – reserved word
2. (12 Points) The following variable declarations apply to parts (a) thru (c).
int i,
j, k;
double x, y, z;
char a, b, c;
string s, t;
For each of (a) thru (c) below, write a single cin statement that would read what is underlined and store the required values in the prescribed variables.
(a) Read discount = $22.25 such that x contains 22.25 and s contains “discount”.
cin >> s >> a >> b
>> x;
(b) Read m081234@usna.edu such that j contains 81234 and t contains usna.edu.
cin >> a >> j >> b
>> t;
(c) Read 36.9% such that x contains 36.9, y contains 18.4 and z contains 83.6.
18.4%
83.6%
cin >> x >> a >> y
>> b >> z >> c;
3. (6 Points) The 8bit sequence (a byte) 10101011 has what interpretation as an
(a) int (Show work or risk loosing all credit)
10101011 = 1 x 2^{7} + 1x2^{5} + 1x2^{3} + 1x2^{1}
+ 1x2^{0}
= 128 + 32 + 8 + 2 + 1
= 171
(b) as a bool
true or 1
4. (13 Points) Given the following declarations:
int i
= 6, j = 4, k = 7;
char c = ‘H’;
double x = 3.6, y = 1.3;
string s = “app”, t = “ly”, u = “rove”;
Fill in the following table by providing the type and value of each expression.
Expression 
Type 
Value 

int 
1 

int 
6 

string 
approve 

int 
6 
cout >> y 
ostream 
Leave Blank 
1.0/3 + 1/3.0 + 1.0/3.0 + 1/3 
double 
1 

int 
5 
5. (12 Points)
(a) Assume that variable x is of type double with a value of 17.325, write the statement(s) that would print exactly the following:
The integer portion of the variable x is 17.
cout <<
“The integer portion of the variable x is ”
<< int
(x) << ‘.’ << endl;
(b) Assume the variable c is of type char. Write the statement(s) that would print “Upper Case” if the value stored in c is a capital letter and “Not Upper Case” otherwise. A copy of the ASCII Character Table can be found on the last page of the exam.
if (c >= 65
&& c <= 90)
cout
<< “Upper Case” << endl;
else
cout
<< “Not Upper Case” <<endl;
(c) What is the output from the following code fragment?
char c1, g;
c1 =
‘*’;
c1 =
g;
cout << “The value of c1
is “ << c1;
a. The value of c1 is *
b. The value of c1 is g
c. The value of c1 is c1
d. none of the above
6. (8 Points) Rewrite the following code fragment so that it prints exactly the same answer but uses a for loop rather than a while loop.
double answer =
1.0;
int
trials = 3;
while (trials <
15)
{
answer =
answer * 2.0;
trials =
trials + 3;
}
cout
<< “The answer is “ << answer << ‘.’ << endl;
double answer = 1.0;
for (int trials = 3;
trials < 15; trials += 3)
{
answer = answer
* 2.0;
}
cout << “The
answer is “ << answer << ‘.’ << endl;
7. (4 Points) Which of the following is/are infinite loop(s)?
a. for (int
k = 0; k!=10; k++)
cout
<< “Beat Army” << endl;
b. int numb = 101;
while (numb <=
100)
{
cout
<< “Beat Army” << endl;
numb ;
}
c. for (int numb =
25; numb <= 100; numb)
cout << “Beat Army” << endl;
d. none of the above
8. (10 Points) I am writing a program which computes PRT grades. The portion of the program that calculates the grades is given below.
double push_score, curl_score,
run_score, avg;
cin >> push_score;
cin >> curl_score;
cin >> run_score;
avg = push_score
+ curl_score + run_score /
3.0;
(a) Why do I get the incorrect result for avg?
Only run_score
is being divided by 3.0, vice the entire sum.
(b) How would you correct the problem?
Add parenthesis around the sum.
avg = (push_score + curl_score
+ run_score) / 3.0;
9. (10 Points) Explain why the following program does not compile, and explain what needs to be done to fix it.
#include
<iostream>
using namespace std;
int main()
{
Add Remove
int
k;
int n;
cin
>> k;
if (k < 0)
{
int n = 1*k;
}
else
{
int n = k;
}
cout
<< "The absolute value of k is " << n << endl;
return 0;
}
(a) Why won’t this program compile?
Scope problem…n is an undeclared identifier when it is used
in the cout statement. The variable n does not exist outside the if
and else blocks in which it was declared.
(b) How can the problem from part (a) be fixed?
Declare n at the beginning of main()
and remove the declarations in the blocks…see the code marked in red.
10. (10 Points) According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, a leap year is a year that is:
§ divisible by 400, or
§ divisible by 4, but not divisible by 100
For example, 2000 was a leap year (divisible by 400), as was 2004 (divisible by 4, but not by 100). However, 2100 will not be a leap year because it is divisible by 4 and also divisible by 100 (centurial year).
Provide the three missing expressions and one missing logical operator in the C++ program below so that the program informs the user whether or not the year they entered is a leap year. You can assume the user enters a valid integer input.
#include <iostream>
using namespace std;
int main()
{
int
year;
cout
<< "Enter a 4 digit year: ";
cin
>> year;
if ( year%400 == 0 )
cout
<< "Leap Year" << endl;
else if (( year%4 == 0 ) && (
year%100 != 0 ))
cout <<
"Leap Year" << endl;
else
cout
<< "Not Leap Year" << endl;
return 0;
}