Makeup Exam – Spring 2014
A) (10 points) Show what is printed
by the following program:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
int chapter, page= 250, count = 0;
string word = "junk";
chapter = page/100;
if (page >= 100)
{
cout << "We are past the introductory
chapter!\n";
word = "computer";
page++;
}
else
{
cout << "Still at the beginning!\n";
page--;
}
if (word < "computing")
cout << "Word not exactly in dictionary\n";
cout << "page " << '#' << page
<< endl;
cout << "\tin chapter " << '#' <<
chapter << endl;
cout << "\t\tup to word " << word <<
endl;
return 0;
}
B) (30 points) Answer questions about short C++ code:
a)
Suppose that the input
is 1. What is the value
of alpha after the following C++ code executes?
cin>>
alpha;
switch(alpha)
{
case 1:
case 2:
alpha++;
case 4:
alpha++;
break;
case 5:
alpha = 3 * alpha;
case 6:
alpha = alpha + 6;
default:
alpha--;
}
b)
What is printed
by the following piece of code?
int count=270;
while (count
< 273)
{
count++;
cout << count << "Good Luck!" << endl;
}
cout
<< "Done!?!";
c)
What
is the value of counter after
the following statements executes?
counter = 20;
while (counter < 62)
{
cout << counter;
counter++;
}
d) Suppose that we have the following declarations:
int
alpha;
double
beta;
bool
flag; //boolean is assigned either true or false
int
x = 5, y = 20;
What is the value assigned
to alpha or beta or flag with each of the following expressions?
i)
alpha = 21 + 14 / 3 + 1;
ii)
flag = (4 + x == 9 && 2 > y || y < 30);
e)
Show the contents
of "out.txt"
after the following code is run. Here is a
picture of the input file:
int main()
{
int
a, b, c;
ifstream
inFile;
ofstream
outFile;
inFile.open("data.txt");
outFile.open("out.txt");
inFile
>> a >> b >> c;
outFile<<
a << ' '<< b << ' ' << b – a << ' ' << a *
b * c;
outFile.close();
inFile.close();
}
C)
( 40 points)Short Answer Questions:
For this section you may assume the following
declarations:
int num;
double x , y, z, a, b,
c;
char letter;
You may also assume
that all the variables have values. When asked to write code in this section,
do not write full programs.
a) Change
the following switch statement
to an if..else statement:
switch (letter)
{
case ‘a’:
case ‘z’:
cout << “last or first”;
break;
case ‘k’:
case ‘l’:
case ‘m’:
case ‘n’:
case ‘o’:
case ‘p’:
cout << “middle”;
break;
default:
cout << “All rest!”;
}
b) Write
the C++ code that assigns the variable x
the value of the expression
c) Use a loop: Write the C++ code that prints out the numbers from 1 to 100 and their squares (the number raised to the 2 power) and cubes (the number raised to the 3rd power). Your output should be:
1 1 1
2 4 8
3 9 27
4 16 64
etc.
d) Write the C++ code that prints out yes if the the following two conditions are BOTH true:
i) variable num is divisible by either 10 or 4
ii) the letter is a after ‘M’ in the alphabet
Otherwise no will be printed.
e) Write the C++ code that allows
the user to enter 100 numbers. Your program prints out the sum of the 25
numbers. (YOU MUST USE A LOOP!)
D)
(20 points) Write a COMPLETE C++ program (including
comments!) that asks the user to input the number of total hours it took for the computer to process the result of a
complicated program. The program then
prints the duration of the job in a nicer form; i.e. it prints out the number
of total weeks, then (for the part left over) days and (for the
part left) hours that the job took.
For example, if the user types in 362 for the hours, the output will be:
2 weeks(s) and 1 day(s) and 2 hours(s)
Remember that there are 24
hours in a day and 7 days in a week. You may assume that there are no leap
years!
Extra Credit: The (s) above is in parenthesis. Have it singular or plural according to the
number. So above would print:
2
weeks
1
day
2
hours