COSC1410
Dr. Jones
COSC 1410 Exam #2
Your Name: _________
KEY
___________
TA Name:___________________
1.
What is the value assigned to the variable in each of these cases? Explain the results.
double z, w, t;
a) z = 1/2;
b) t = 3.0/2.0;
Answer:
(4 points)
a)
0 is calculated because 1/2 is done with truncating integer divide, gives 0. The floating
point value 0.0 is stored.
b)
1.5 is calculated and stored.
2.
Assume
variables
first
and
second
are declared to be
double
and are initialized.
Write a sequence of lines of code that cause the values stored in
first
and
second
to be
exchanged if the value of
first
is not less than
second
.
Answer:
(6 points)
// double first, second;
// these have been initialized
if (!(first < second))
{
double temp = first;
first = second;
second = temp;
}
//assert: first <= second
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentPage 2
3.
Write Boolean expressions that represent the given English expressions. Assume any
variables used have been declared and initialized.
a)
x
and
y
are odd
b)
ch
is an upper case alphabetic character (between
'A'
and
'Z'
).
Answer:
(6 points)
a.
b.
4.
Write Boolean expressions that represent the given English expressions. Assume any
variables used have been declared and initialized.
a) at least one of x or y is odd
b)
at least one of x or y is nonnegative (
x is nonnegative
is
written x >= 0)
c) x is 1 or x is equal to y
Answer:
(6 points)
a)
(x%2==1)  (y%2==1)
b)
(x >= 0)  (y>=0)
c)
(1==x)  (x==y)
5.
What is the output from each of the following loops?
a) while ( 0 )
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '07
 D.Kretchmer
 ObjectOriented Programming

Click to edit the document details