Page 137 - Introduction to Programming with Java: A Problem Solving Approach
P. 137

                b) 9x􏰁(4.5􏰂y) 2x
11. [after §3.16] Assume this: int a = 9;
double b = 0.5;
int c = 0;
Evaluate each of the following expressions by hand. Show your work, using a separate line for each evaluation step. Check your work by writing and executing a program that evaluates these expressions and outputs the results.
a) a + 3 / a
b) 25 / ((a - 4) * b)
c) a / b * a
d) a % 2 - 2 % a
12. [after §3.19] Type Casting:
Assume the following declarations:
int integer;
double preciseReal;
float sloppyReal;
long bigInteger;
Rewrite those of the following statements which would generate a compile-time error using an appropriate cast that makes the error go away. Do not provide a cast for any statement which the compiler automatically
promotes.
Apago PDF Enhancer
a) integer = preciseReal;
b) bigInteger = sloppyReal;
c) preciseReal = integer;
d) sloppyReal = bigInteger;
e) integer = sloppyReal;
f) bigInteger = preciseReal;
g) sloppyReal = integer;
h) preciseReal = bigInteger;
i) integer = bigInteger;
j) sloppyReal = preciseReal; k) preciseReal = sloppyReal; l) bigInteger = integer;
13. [after §3.20] Assuming that tab stops are 4 columns apart, what output does the following statement generate?
System.out.println("\"pathName:\"\n\tD:\\myJava\\Hello.java");
14. [after §3.21] Reference types begin with an uppercase letter. (T / F)
15. [after §3.22] Assume that you have a string variable named myName. Provide a code fragment that prints
myName’s third character.
16. [after §3.22] What does this code fragment print?
String s = "hedge";
s += "hog";
System.out.println(s.equals("hedgehog"));
System.out.println((s.length()-6) + " " + s.charAt(0) + "\'s");
Exercises 103
 



























































   135   136   137   138   139