Algorithm specification test questions

1

This algorithm is designed to validate user input until they enter a value between 0 and 50 inclusive.

Line 1 FOR counter FROM 0 TO 4 DOLine 2 RECEIVE score [counter] FROM (INTEGER) KEYBOARDLine 3 WHILE score [counter] ˂0 AND score [counter] ˃ 50 DOLine 4 SEND (“The score you entered was not valid; please enter a score between 0 and 50”)Line 5 RECEIVE score [counter] FROM (INTEGER) KEYBOARDLine 6 END WHILELine 7 END FOR

Identify the logic error shown in this algorithm.

2

This algorithm is designed to perform a linear search.

Line 1 SET item_found TO FALSELine 2 SET counter TO 0Line 3 RECEIVE desired_item FROM (STRING) KEYBOARDLine 4 REPEATLine 5 IF desired_item = nation[counter] THENLine 6 SET item_found TO TRUE Line 7 END IFLine 8 SET counter TO counter + 1Line 9 UNTIL counter = 10 OR item_found = TRUE

The loop used in this example is:

3

This algorithm is designed to find the maximum value held within an array called testscore which will hold ten values.

Line 1 SET maximum TO testscore [0]Line 2 FOR counter FROM 1 TO 9 DOLine 3 IF testscore[counter] ˃ maximum THENLine 4 SET maximum TO testscore[counter]Line 5 END IFLine 6 END FOR

Explain why the fixed loop is set to run from 1 TO 9

4

This algorithm is designed to find the minimum value held within an array called testscore. This array will hold ten values.

Line 1 SET minimum TO testscore [0]Line 2 FOR counter FROM 1 TO 9 DOLine 3 IF testscore[counter] ˃ minimum THENLine 4 SET minimum TO testscore[counter]Line 5 END IFLine 6 END FOR

Explain the role of the variable ‘counter’ in this example.

5

The algorithm shown below has been designed to count the number of times a number is found within an array.

Line 1 SET occurrence TO 0Line 2 RECEIVE desired_value FROM KEYBOARDLine 3 FOR counter FROM 0 TO 9 DOLine 4 IF age [counter] = desired_valueLine 5 Line 6 END IFLine 7 END FOR

Line 5 is missing. Which of the following should be shown on line 5?

6

Why is the use of meaningful identifiers important when creating an algorithm?

7

This algorithm should output the final value of occurrence. Line 6 is not in the correct place. Where should line 6 be placed?

Line 1 SET occurrence TO 0Line 2 RECEIVE desired_value FROM KEYBOARDLine 3 FOR counter FROM 0 TO 9 DOLine 4 IF age [counter] = desired_valueLine 5 SET occurrence TO occurrence + 1Line 6 SEND occurrence TO DISPLAYLine 7 END IFLine 8 END FOR

8

Select the correct definition for the term algorithm.

9

This algorithm is designed to carry out a linear search. Complete the missing part of line 9 by selecting the correct answer as shown below.

Line 1 SET item_found TO FALSELine 2 SET counter TO 0Line 3 RECEIVE desired_item FROM (STRING) KEYBOARDLine 4 REPEATLine 5 IF desired_item = nation[counter] THENLine 6 SET item_found TO TRUELine 7 END IFLine 8 SET counter TO counter + 1Line 9 UNTIL counter = 10 OR

10

If the user enters the score 71 on line 2, why would this value not be held within the score array by the time the second iteration of the fixed loop takes place?

Line 1 FOR counter FROM 0 TO 4 DOLine 2 RECEIVE score [counter] FROM (INTEGER) KEYBOARDLine 3 WHILE score [counter] ˂0 OR score [counter] ˃ 50 DOLine 4 SEND (“The score you entered was not valid; please enter a score between 0 and 50”)Line 5 RECEIVE score [counter] FROM (INTEGER) KEYBOARDLine 6 END WHILELine 7 END FOR