assertionQuestions - 2009 November 23 Assertions...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
2009 November 23 Assertions & Verification Example Test Questions 1. A Give a general template for refining an operation into a sequence and state what questions a designer must answer to verify the sequence is a correct refinement. B Give a general template for refining an operation into a choice and state what questions a designer must answer to verify the choice is a correct refinement. C Give a general template for redefining an operation into a loop and state what questions a designer must answer to verify the loop is a correct refinement. 2. Explain what is meant by strong and weak assertions. 3. There are 5 types of assertions that can be used in program design. Describe each type of assertion and how that assertion type is used in the design of programs. 4. Give the best precondition, postcondition and loop invariant for the following algorithm to do a binary search on an array A[1 .. N]. Your work should use as much mathematical notation as possible to capture the relevant information. low 1 high N Result 0 while Result = 0 and low high do mid (low + high)/2 if A[mid] = k then Result mid elseif A[mid] < k then low mid + 1 else high mid - 1 fi end while 5. What would be the best precondition, postcondition and loop invariant for the following algorithm to find a span of dash characters in an array. charPointer := 1 loop exit when textLine(charPointer) not equal dashChar charPointer := charPointer + 1 end loop 6. What would be the best precondition, postcondition and loop invariant for the following correct algorithm to find the maximum integer and its index in the array A[1..N]. max := 0 ; maxIndex := 0 j := 1 while j <= N do if max < A[j] then max = A[j] ; maxIndex := j end if
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2009 November 23 Assertion & Verification Example Test Questions Page 2 of 8 j := j + 1 end while 7. Prove the following algorithm to sum the odd integers in the range 1 to N inclusive is correct. Clearly show the correspondence with the questions a designer must ask when verifying a loop is the correct refinement of an operation. Precondition: N > 0 Postcondition: sum = { j :1.. N | odd ( j ) j } " Loop invariant: sum = { j :1.. p | odd ( j ) j } " sum := 0 p := 0 while p < N p := p + 1 if odd(p) then sum := sum + p fi end while 8. What is a loop invariant? When is it used? How is it used? Why is it used? Where is it used? 9. Given a loop, pre & post conditions and loop invariant prove the loop is correct or is incorrect. 10. Given pre & post conditions and a loop invariant create the corresponding program text in Eiffel. 11. What would be the best loop invariant and variant for the following algorithm. require x = a * a from invariant ??? variant ??? until x = 0 loop y := y + 1 ; x := x - 1 end ensure y = a*a + old y Using your loop invariant and variant, verify that the algorithm given above achieves the post- condition.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern