Here are some sample questions:
What is the output of this program? Notice that the variable
names are not particularly meaningful; this means you will have
to go through the program step by step, just as if you were
the computer. (We omitted the
system("PAUSE") so that you don’t have to write that
output.)
The answer is at the bottom of the page.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main( )
{
int q = 10;
int s = 23;
if (s-q < 15 && q/s > 0)
{
cout << "Condition is True" << endl;
}
else
{
cout << "Condition is False" << endl;
}
return 0;
}
Consider the following problem:
The volume of a cylinder is equal to its height times π times its radius squared.
Write a program that prompts for the height and radius of a cylinder in centimeters and displays the volume in cubic centimeters. The height and radius may have decimal points, and must both be greater than zero. If height and radius are not both greater than zero, print an error message. Use 3.1415926536 for the value of π. Your output must be properly labelled.
Here is a program that will not do the job, because it has a lot of errors in it. Copy and paste this non-working program into a file and fix the errors. You can test to see if it works properly by entering a height of 5.5 and a radius of 3; the answer will be 155.509.
Hint: Go through the program and look for any obvious errors, and correct them before you try compiling. Don’t just compile and then try fixing errors one by one.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main( )
{
int height = 0;
double radius = 0.0;
PI = double const 3.1415926536;
cout << "Enter the height in centimeters: ";
cin >> height;
cout << "Enter the radius in centimeters: ";
cin >> radius
if (height && radius > 0)
{
volume = pi * length * radius * radius;
cout << "The volume is " << volume << endl;
}
else
{
cout << Both radius and height must be greater than zero.
<< endl;
system("PAUSE");
return 0;
}
See the corrections
See the final working version.
The answer to the first program’s output is Condition is False.
Remember, when you divide integers by integers, the result is an integer,
so q/s is 0.