Java Software Solutions, 8e, Global Edition Exercise Solutions, Ch. 2
Chapter 2 Exercise Solutions
EX 2.1. What is the difference between the literals 4, 4.0, ‘4’, and “4”?
EX 2.2. Explain the following programming statement in terms of objects and
theservices they provide.
EX 2.3. What output is produced by the following code fragment? Explain.
EX 2.4. Is the following program statement wrong? If so, how can it be fixed?
EX 2.5. What output is produced by the following statement? Explain.
EX 2.6. What is the output produced by the following statement? Explain.
EX 2.7. What value is contained in the integer variable count after the following
statements are executed?
EX 2.8. What value is contained in the floating point variable area after the following
statements are executed?
EX 2.9. What value is contained in the integer variable sum after the following
statements are executed?
EX 2.10. Write three different program statements that decrement the value of an integer
variable highest.
EX 2.11. Given the following declarations, what result is stored in each of the
listedassignment statements?
a. iResult = num1 / num4;
b. fResult = num1 / num4;
Java Software Solutions, 8e, Global Edition Exercise Solutions, Ch. 2
c. iResult = num3 / num4;
d. fResult = num3 / num4;
e. fResult = val1 / num4;
f. fResult = val1 / val2;
g. iResult = num1 / num2;
h. fResult = (double) num1 / num2;
i. fResult = num1 / (double) num2;
j. fResult = (double) (num1 / num2);
k. iResult = (int) (val1 / num4);
m. fResult = (int) ((double) num1 / num2);
n. iResult = num3 % num4;
o. iResult = num 2 % num3;
p. iResult = num3 % num2;
q. iResult = num2 % num4;
EX 2.12. For each of the following expressions, indicate the order in which the operators will be
evaluated by writing a number beneath each operator.
a. a b c d
b. a b + c d
Java Software Solutions, 8e, Global Edition Exercise Solutions, Ch. 2
2.13. Explain the role played by the World Wide Web in the translation and execution of
some Java programs.
EX 2.14. Compare and contrast a traditional coordinate system and the coordinate system used
by Java graphical components.
EX 2.15. How many bits are needed to store a color picture that is 400 pixels wide and 250
pixels high? Assume color is represented using the RGB technique described in
c.
a + b / c / d
3 1 2
d.
a + b / c * d
e.
a / b * c * d
1 2 3
f.
a % b / c * d
1 2 3
g.
a % b % c % d
1 2 3
h.
a (b c) d
2 1 3
i.
(a (b c)) d
2 1 3
j.
a ((b c) d)
3 1 2
k.
a % (b % c) * d * e
2 1 3 4
l.
a + (b c) * d e
3 1 2 4
m.
(a + b) * c + d * e
1 2 4 3
n.
(a + b) * (c / d) % e
1 3 2 4
3 1 2
EX 2.16. Assuming you have a Graphics object called page, write a statement that will draw a
line from point (20, 30) to point (50, 60).
EX 2.17. Assuming you have a Graphics object called page, write a statement that will draw a
rectangle with height 70 and width 35, such that its upper left corner is at point (10, 15).
EX 2.18. Assuming you have a Graphics object called page, write a statement that will draw a
circle centered on point (50, 50) with a radius of 20 pixels.
EX 2.19. The following lines of code draw the eyes of the snowman in the Snowman applet. The
eyes seem centered on the face when drawn, but the first parameters of each call are
not equally offset from the midpoint. Explain.