AP Quiz Completion Proof

Same as the previous blog. I did my best, but I did not reach my intended results. As always, I’m still learning and there’s much room for improvement.



Questions

1.4 (2/3)


Q1 (Correct): In the following code segment, score and penalty are initially positive integers. The code segment is intended to reduce the value of score by penalty. However, if doing so would cause score to be negative, score should be assigned the value 0. For example, if score is 20 and penalty is 5, the code segment should set score to 15. If score is 20 and penalty is 30, score should be set to 0. The code segment does not work as intended.

Line 1: IF(score - penalty < 0)

Line 2: {

Line 3: score ← score - penalty

Line 4: }

Line 5: ELSE

Line 6: {

Line 7: score ← 0

Line 8: }

Which of the following changes can be made so that the code segment works as intended?

Answer: Interchaning lines 3 and 7.

Interchanging lines 3 and 7 ensures that if reducing score by penalty results in a negative value, score is set to 0. Otherwise, score is reduced by penalty.


Q2 (Incorrect): The following procedure is intended to return true if the list of numbers myList contains only positive numbers and is intended to return false otherwise. The procedure does not work as intended.

PROCEDURE allPositive(myList)

{

index ← 1

len ← LENGTH(myList)

REPEAT len TIMES

{

IF(myList[index] > 0)

{

RETURN(true)

}

index ← index + 1

}

RETURN(false)

}

My Answer : [1, 2, 3]

The procedure begins to traverse the list and immediately encounters the positive value 1. At this point, the procedure returns true as intended for this list.

Correct Answer: [-1, 0, 1]

The procedure traverses this list and eventually encounters the positive value 1. At this point, the procedure returns true when it should return false because the list does not contain only positive values.


Q3 (Correct): A homework assignment consists of 10 questions. The assignment is graded as follows.

Let numCorrect represent the number of correct answers for a particular student. The following code segment is intended to display the appropriate grade based on numCorrect. The code segment does not work as intended in all cases.

For which of the following values of numCorrect does the code segment NOT display the intended grade?

Answer(s): 8: When numCorrect is 8, the condition numCorrect > 7 evaluates to true and the condition numCorrect ≥ 9 evaluates to false. Therefore “check minus” is displayed instead of the intended “check plus”.

6: When numCorrect is 6, the condition numCorrect > 7 evaluates to false. Therefore “check” is displayed instead of the intended “check minus”.



Click here to access additional resources for Collaboration, Program Function/Purpose, and Program Design/Development.