Java Reference
In-Depth Information
z++;
}
System.out.println(y + " " + z);
}
For each of the following calls, indicate the output that the preceding method produces:
mystery(1);
mystery(6);
mystery(19);
mystery(39);
mystery(74);
4.
Consider the following method:
public static void mystery(int x) {
int y = 0;
while (x % 2 == 0) {
y++;
x = x / 2;
}
System.out.println(x + " " + y);
}
For each of the following calls, indicate the output that the preceding method produces:
mystery(19);
mystery(42);
mystery(48);
mystery(40);
mystery(64);
5.
Consider the following code:
Random rand = new Random();
int a = rand.nextInt(100);
int b = rand.nextInt(20) + 50;
int c = rand.nextInt(20 + 50);
int d = rand.nextInt(100) - 20;
int e = rand.nextInt(10) * 4;
What range of values can each variable (
a
,
b
,
c
,
d
, and
e
) have?
6.
Write code that generates a random integer between
0
and
10
inclusive.
7.
Write code that generates a random odd integer (not divisible by
2
) between
50
and
99
inclusive.
8.
For each of the
do/while
loops that follow, state the number of times that the loop will execute its body. Remember
that “zero,” “infinity,” and “unknown” are legal answers. Also, what is the output of the code in each case?
a.
int x = 1;
do {
Search WWH ::
Custom Search