Chapter 4 Questions.
Program Verification.
Semantics of programs: the partial correctness relation
par.
Recall what
par
phi
P
psi
means: "For all stores that
satisfy phi, if P runs on such a store and if that run terminates, then the
resulting store satisfies psi."
Question 1
Which of the following triples is valid with respect to the partial
correctness relation
par?
(As in the textbook, we assume all occurring variables x, y, ... have type integer.)
x > -1
y = x + x;
x > -2
x = 5
y = 3
x = y; y = x;
x = 3
y = 5
T
while (x > 4) x = x - 1;
x = 4
T
if (x > 5) { y = x - 1;} else {y = x + 1;}
y > 3
T
x = 1; x = 2;
x = 1
x = 2
Question 2
Which of the following programs P satisfies the triple
T
P
z = max(x,y)
with respect to partial correctness:
par
T
P
z = max(x,y)
where max(x,y) is the larger number of x and y (e.g. max(-1,3) = 3)?
-
if (x > y) {
z = x + 1;
} else {
z = y + 1;
}
z = z - 1;
-
if (x > y) {
z = y + 1;
} else {
z = x + 1;
}
z = z - 1;
-
if (x > y) {
z = x;
} else {
z = y + 1;
}
z = z - 1;
-
if (x > y) {
z = x + 1;
} else {
z = y + 1;
}
z = z + 1;
-
if (x > y) {
z = y;
} else {
z = x;
}
z = z;
Question 3
The program P:
x = y;
if (x < z) { x = z; }
satisfies which of the following Hoare triples
with respect to the partial correctness relation
par?
x = y
P
x = y 
x < y
P
x = y
y < z
P
x = z
y = z
P
x < y
- None of the above.
Question 4
Consider the program:
y = x - y - 1;
x = x + 1;
y = 2*x + y;
What fact about the starting state would guarantee that, after the program has run, x = y?
- x = y.
- y = -1.
- y = 2x.
- y = y + 3.
- None of the above.
Invariants of while-statements.
Question 5
Which of the following is an invariant of the while-statement in the program
a = 0;
z = 0;
while (a != y)
{
z = z + x;
a = a + 1;
}
with respect to partial correctness
par ?
- z = x * y
- z = x * a
- z = a * y
- y = z * a
- x = y * a
Question 6
Which of the following is an invariant of the while-statement in the program
z = 0;
a = 1;
while (a != y+1) {
z = z + a;
a = a + 1;
}
with respect to partial correctness
par ?
- z = a + y.
- z = a(a + y).
- 2z = (a - 1)a.
- 2z = a + 1.
- The while loop has no invariant.
Back to chapter index.