Java Reference
In-Depth Information
8. Suppose you have an int variable called number . What Java expression produces the second-to-last digit of the
number (the 10s place)? What expression produces the third-to-last digit of the number (the 100s place)?
9. Consider the following code:
int first = 8;
int second = 19;
first = first + second;
second = first - second;
first = first - second;
What are the values of first and second at the end of this code? How would you describe the net effect of the
code statements in this exercise?
10. Rewrite the code from the previous exercise to be shorter, by declaring the variables together and by using the special
assignment operators (e.g., += , - = , *= , and /= ) as appropriate.
11. What are the values of a , b , and c after the following code statements? (It may help you to write down their values
after each line.)
int a = 5;
int b = 10;
int c = b;
a++;
b--;
c += a;
12. What is the output from the following code?
int max;
int min = 10;
max = 17 - 4 / 10;
max = max + 6;
min = max - min;
System.out.println(max * 2);
System.out.println(max + min);
System.out.println(max);
System.out.println(min);
13. Suppose you have a real number variable x . Write a Java expression that computes the following value y while using
the * operator only four times:
12.3 x 4
9.1 x 3
19.3 x 2
y
=
-
+
-
4.6 x
+
34.2
Section 2.3: The for Loop
14. Assume that you have a variable called count that will take on the values 1 , 2 , 3 , 4 , and so on. You are going to
formulate expressions in terms of count that will yield different sequences. For example, to get the sequence 2 , 4 ,
6 , 8 , 10 , 12 , ... , you would use the expression (2 * count) . Fill in the following table, indicating an expres-
sion that will generate each sequence.
 
Search WWH ::




Custom Search