Question: Consider a Java function F with precondition P and postcondition Q. Which of the following is equivalent to F is correct? A. Q is true

Consider a Java function F with precondition P and postcondition Q. Which of the following is equivalent to "F is correct"?

A.

Q is true

B.

If P is true, then Q is true.

C.

P is true

D.

If P is false, then Q is false.

Consider the following function with a loop:

// Pre: n > 0 is a natural number // Post: returns the value of n^2 int square(int n) { r = 0; k = 0; while (k < n) { r = r + 2 * k + 1; k = k + 1; } return r; }

Is k < n a valid loop invariant? Why or why not? Choose one from below.

A.

k < n is NOT a valid loop invariant because it is always false.

B.

k < n is NOT a valid loop invariant because it is not true before entering the loop

C.

k < n is NOT a valid loop invariant because it is not true at the ends of all iterations

D.

k < n is a valid loop invariant because it is true as long as we're in the loop

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!