Professional Documents
Culture Documents
apple
2.
apple
orange
3.
orange
4.
orange
pear
poor indentation
5.
if (num2 == MAX)
{
System.out.println ("apple");
System.out.println ("orange");
}
System.out.println ("pear");
apple
orange
pear
6.
apple
7.
pear
Exercise Worksheet
8.
if (2*num1 != num2)
System.out.println ("apple");
else
{
System.out.println ("orange");
System.out.println ("pear");
}
apple
9.
if (LIMIT%num1 + 4 ==
{
System.out.println
System.out.println
}
else
{
System.out.println
System.out.println
}
pear
banana
num1 + (MAX-num2))
("apple");
("orange");
("pear");
("banana");
10.
apple
orange
11.
orange
12.
orange
pear
13.
apple
Exercise Worksheet
14.
pear
poor indentation
15.
if (num3 == 87)
{
if (num2 != MAX)
System.out.println ("apple");
}
else
System.out.println ("orange");
System.out.println ("pear");
pear
16.
orange
17.
if (LIMIT%MAX == 3)
System.out.println ("apple");
else
if (num2 == MAX)
System.out.println ("orange");
else
System.out.println ("pear");
orange
18.
apple
orange
banana
kiwi
coconut
Exercise Worksheet
else
System.out.println ("lime");
System.out.println ("coconut");
19.
orange
20.
orange
21.
apple
orange
22.
apple
orange
23.
orange
24.
apple
orange
25.
apple
orange
26.
apple
orange
27.
Exercise Worksheet
For exercises 28 to 41, write code segments that will perform the specified action.
Assume that all variables have already been declared and given values.
28.
29.
Increment the integer variable total if total is zero and decrement total
otherwise.
if (total == 0)
total++;
else
total--;
30.
31.
32.
Print "Victory" only if result is greater than or equal to 500 and penalty
is equal to zero (use nested ifs).
if (result >= 500)
if (penalty == 0)
System.out.println ("Victory");
Exercise Worksheet
33.
Print "Victory" only if result is greater than or equal to 500 and penalty
is equal to zero (use logical operators).
if (result >= 500 && penalty == 0)
System.out.println ("Victory");
34.
Assign the smallest of two integer values num1 and num2 to the variable
smallest. (use an if-else statement)
if (num1 < num2)
smallest = num1;
else
smallest = num2;
35.
Assign the smallest of two integer values num1 and num2 to the variable
smallest. (use the conditional operator)
smallest = (num1 < num2) ? num1 : num2;
36.
Assign the smallest of three integer values num1, num2, and num3 to the
variable smallest. (do not use logical operators)
if (num1 < num2)
if (num1 < num3)
smallest = num1;
else
smallest = num3;
else
if (num2 < num3)
smallest = num2;
else
smallest = num3;
37.
Assign the smallest of three integer values num1, num2, and num3 to the
variable smallest. (use logical operators)
if (num1 < num2 && num1 < num3)
smallest = num1;
else
if (num2 < num3)
smallest = num2;
else
smallest = num3;
Exercise Worksheet
38.
39.
Of the two characters stored in the variables ch1 and ch2, print the one which
comes later in the Unicode character set.
if (ch1 < ch2)
System.out.println (ch1);
else
System.out.println (ch2);
40.
41.
Print "Equal" if two floating point values stored in val1 and val2 are exactly
equal, "Essentially Equal" if they are within 0.0001 of each other, or
"Not Equal" otherwise.
if (val1 == val2)
System.out.println ("Equal");
else
if (Math.abs(val1-val2) < 0.0001)
System.out.println ("Essentially Equal");
else
System.out.println ("Not Equal");