Uploaded by paraformaldead

Review Chapter 2 + Solution

advertisement
Review Chapter 2
1. Assume that int a = 1 and double d = 1.0, and that each expression is independent.
Show the order of execution of operators in each statement as shown in the below example.
What is (are) the value(s) of the variable(s) and what are the results of the following statements?
Example:

a = 46 % 9 + 4 * 4 - 2;
a = 1 + 4 * 4 – 2;
a = 1 + 16 – 2;
a = 17 – 2;
a = 15;
a;
15 is assigned to the variable a and a is the return value.
a = 45 + 43 % 5 * (23 * 3 % 2);
a = 45 + 43 % 5 * (69 % 2);
a = 45 + 43 % 5 * (1);
a = 45 + 43 % 5 * 1;
a = 45 + 3 * 1;
a = 45 + 3;
a = 48;
48 is assigned to the variable a 48 is return to the system.

a %= 3 / a + 3;
a %= 3 + 3;
a %= 6;
a = 1 % 6;
a = 1;
1 is assigned to the variable a.

d = 4 + d * d + 4;
d = 4 + 1 * 1 + 4;
d = 4 + 1 + 4;
d = 5 +4;
d = 9;
9 is assigned to the variable d.

d += 1.5 * 3 + (++a);
a becomes 2
d += 1.5 * 3 + (2);
d += 1.5 * 3 + 2;
d += 4.5 + 2;
d += 6.5;
d = 1.0 + 6.5;
d = 7.5;
7.5 is assigned to d.
2. Show the result of the following expressions:
78 % -4
2
-34 % 5
-4
-34 % -5
-4
3. If today is Tuesday, what will be the day in 100 days?
(2 +100) % 7 -> 4, the day is Thursday
4. What is the result of 25 / 4? How would you rewrite the expression if you wished the result to be
a floating-point number?
25.0 / 4 or
25/4.0 or
25.0/4.0 or
(double)25 / 4
5. Identify the line(s) and the reason of the error (s) and fix it in the following code:
1 public class Test {
2
public void main(String[] args) {
3
4
5
6
7
8
9
10 }
int i;
int k = 100.0;
int j = i + 1;
System.out.println("j is " + j + " and
k is " + k);
}
Line 2: missed static keyword
Line 3: i is not inititalized.
Line 4 error: incompatible types: possible lossy conversion from double to int
int k = 100.0;
Line 7 and 8 wrong use of a String divided on two-line statement.
6. Show the output of the following program:
public class Test {
public static void main(String[] args) {
char x = 'a';
char y = 'c';
System.out.println(++x);
System.out.println(y++);
System.out.println(x - y);
}
}
Output : b
c
-2
7. Which of these statements are true?
(a) Any expression can be used as a statement. False
(b) The expression x++ can be used as a statement. True
(c) The statement x = x + 5 is also an expression. True
(d) The statement x = y = x = 0 is illegal. False
8. What is the output of the following code segment?
int i = '2' + '3'; // (int)'2' is 50 and (int)'3' is 51
System.out.println("i is " + i); // i is 101
int j = 2 + 'a'; // (int)'a' is 97
System.out.println("j is " + j); // j is 99
System.out.println(j + " is the Unicode for character " + (char)j);
System.out.println("Chapter " + '2');
display
i is 101
j is 99
99 is the Unicode for character c
Chapter 2
9. What is the output of the following code segment?
System.out.println("1" + 1);
System.out.println('1' + 1);
System.out.println("1" + 1 + 1);
System.out.println("1" + ('\u0001' + 1));
System.out.println("1" + (1 + 1));
System.out.println('1' + 1 + 1);
11
50
111
12
12
51
10. What is the output of the following code segment?
char c = 'a';
int i = (int)c;
System.out.println(i);
float f = 1000.34f;
i = (int)f;
System.out.println(i);
double d = 1000.34;
i = (int)d;
System.out.println(i);
i = 97;
c = (char)i;
System.out.println(c);
97
1000
1000
a
Download