# Exam 1 CS 143 ```Sample Exam 1
CS 143
(60 points closed book)
1.
Compute and indicate the value of each legal expression. Indicate whether the value is an
integer or a floating-point value. If the expression is not legal, simply write ILLEGAL.
(4)
a.
913 % 9 * 17
b.
279 % 17 + 13.0
c.
7.7 % 2
d.
72 * 3 / 2 * 6
2.
Indicate in outline form the major steps in an sentinel-controlled loop. (4)
3.
Write a complete C++ program (without comments) that computes the volume of a sphere, the
volume of a sphere is 4R / 3 , where R is the radius. The program should prompt for and
read in the radius. The program should print the volume, appropriately labeled. Define PI
to be a constant equal to 3.14159. Remember that the program MUST BE A COMPLETE WORKING
PROGRAM BUT NOT DOCUMENTED. (5)
3
4.
Given:
bool EqualTo;
int num1, num2;
Write one C++ statement to assign to EqualTo true if num1 is equal to num2, false
otherwise. You should assume that num1 and num2 have values. (3)
5.
Given the following program:
#include &lt;iostream.h&gt;
const int LIMIT = 9;
// SHOW YOUR TRACE OVER HERE!!
int main()
{
int sum, i, number;
bool finished;
sum = 0;
i = 1;
finished = false;
while (i &lt;= LIMIT &amp;&amp; !finished)
{
cin &gt;&gt; number;
if (number &gt; 0)
sum = sum + number;
else if (number &lt; 0)
finished = true;
i++;
}
cout &lt;&lt; &quot;Sum is &quot; &lt;&lt; sum &lt;&lt; endl &lt;&lt; &quot;Number is &quot; &lt;&lt; number
&lt;&lt; endl &lt;&lt; &quot;i is &quot; &lt;&lt; i &lt;&lt; endl;
return 0;
}
And given the following input data values
9
7
13
5
0
2
6
-3
-5
2
3
4
0
Trace the program above, making sure to show the values of the variables during execution
of the program and what is output by the program!!
(8)
Assume that x is an int variable for problems 6-7.
6.
What value is assigned to x by the following nested-if statement assuming that x has an
initial value of 4? (2)
if (x &gt;= 3)
x = x + 1;
else if (x &gt;= 6)
x = x + 7;
// x will have a value of _____________________
7.
What value is assigned to x by the following if statements assuming that x has an initial
value of 4? (2)
if (x &gt;= 3)
x = x + 1;
if (x &gt;= 6)
x = x + 7;
// x will have a value of _____________________
8.
What causes the error message &quot;else with no matching if&quot; when this code fragment is
compiled? In other words, indicate the correction to be made to get this section of code
to compile correctly. You may assume that this if statement is in a program that would
otherwise compile. (2)
if (mileage &lt; 24.0)
{
cout &lt;&lt; &quot;Gas&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;guzzler&quot; &lt;&lt; endl;
};
else
cout &lt;&lt; &quot;Fuel efficient&quot; &lt;&lt; endl;
9.
Write ONE nested if statement to display a message indicating the educational level of a
student based on his/her number of years of schooling. (0, none; 1 through 6, elementary
school; 7 through 8, middle school; 9 through 12, high school; &gt; 12, college). Print a
message to indicate bad data as well. You are given: (6)
int num_years; // this represents the number of years of schooling;
...
cin &gt;&gt; num_years;
// WRITE OUT MESSAGE INDICATING THE EDUCATIONAL LEVEL (use ONE nested if)
10.
Write a C++ program segment that reads a list of integers until either a –1 value is found
or the end of file condition is true.
After leaving the loop, print either “-1 found” or “-1
Declare the variables you need to use, and just write the
program segment NOT an entire program. (6)
11.
Complete the following C++ program to find and print the smallest positive odd integer in
a collection of HowMany positive integers where the value of HowMany will be the first
data item read. (You may assume that there are NO negative integers in the list.)
(You may assume that the smallest odd integer will never be 2147483647, which represents
the largest odd integer stored in 32 bits, which has a name called INT_MAX)
Use batch processing and NOT interactive processing.
If the following were sample input:
6
23
2
11
4
22
19
(12)
|
|
|
The corresponding output would be:
|
|
(Write an error message if there
were no odd integers)
SMALLEST ODD IS 11
#include &lt;iostream.h&gt;
#include &lt;limits.h&gt;
int main()
{
int val, smallest_odd, count, HowMany;
cin &gt;&gt; _________________ ;
smallest_odd = INT_MAX; // largest integer for 32-bit integer
count = 1;
// initialize counter
while (________________________________________)
{
cin &gt;&gt; val;
// counter-controlled loop
if (______________________________) // test to see if val is odd
{
if (val &lt; smallest_odd)
}
_________________________________;
// end if
__________________________;
} // end while
// change smallest_odd
// update the counter: count
if (___________________________________)
cout &lt;&lt; &quot;NO ODD #'S FOUND.&quot; &lt;&lt; endl;
else
cout &lt;&lt; “SMALLEST ODD IS &quot; &lt;&lt; smallest_odd &lt;&lt;
return 0;
endl &lt;&lt; endl;
}
12.
5
34
41
32
-1
59
12
13
c.
Given the following input:
(6 pts)
|
|
|
a.
What is the value of summing the input if end of file processing
is used?
_________________________
|
|
|
|
b.
What is the value of summing the input if a sentinel of -1 is used?
_____________
What is the value of summing the input if a count-controlled loop is used and the first
number in the file indicates how many numbers to sum?
_______________
```