Midterm Exam
Problem 1 (12 points). Given the following mini Java program.
Construct the control flow graph and show that the given program
terminates.
int x, y, z;
x = read();
y = read();
if (x > y) {
z = x;
x = y;
y = z;
}
z = 1;
while (x != y) {
x = x + 2;
y = y + 1;
z = z * (y - x);
}
write(z);
Problem 2 (12 points). Given the following Mini Java program.
Construct the control flow graph and prove the assertion Z. In order
to do so, proceed as follows.
Provide a loop invariant. 6 points
Construct an annotation of program points with assertions. 3
points
Prove that your annotation is locally consistent. 3 points
int x, y;
x = 0;
y = 1;
while (y =< 630) {
y = y * 5;
x = x + 5;
}
write(x);
assert (Z ≡ x == 25);
}
Problem 3 (6 points). Given the following Mini Java program. Does
the program always terminate?
Yes, it terminates always. Give at least 5 inputs for which the
program terminates. 2
No, it never terminates. Give at least 5 inputs for which the
program does not terminate. 2
No, it terminates sometimes. Give at least 5 inputs for which
the program does not terminate and at least 5 inputs for which
the program terminates.
int m = 0;
int l = 1;
int t = 0;
int k = 2;
int n = read();
while (l != n) {
t = l + m;
m = l;
l = t;
k = k + 1;
}