Java Reference
In-Depth Information
Section 5.5: Assertions and Program Logic
27. Identify the various assertions in the following code as being always true, never true, or sometimes true and sometimes
false at various points in program execution. The comments indicate the points of interest:
public static int mystery(Scanner console, int x) {
int y = console.nextInt();
int count = 0;
// Point A
while (y < x) {
// Point B
if (y == 0) {
count++;
// Point C
}
y = console.nextInt();
// Point D
}
// Point E
return count;
}
Categorize each assertion at each point with ALWAYS, NEVER, or SOMETIMES:
y < x
y == 0
count > 0
Point A
Point B
Point C
Point D
Point E
28. Identify the various assertions in the following code as being always true, never true, or sometimes true and some-
times false at various points in program execution. The comments indicate the points of interest:
public static int mystery(int n) {
Random r = new Random();
int a = r.nextInt(3) + 1;
int b = 2;
// Point A
while (n > b) {
Search WWH ::




Custom Search