Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Topical Question with Answer

Paper-II
“Identify errors in given algorithms and suggest ways of
removing these errors”

Compiled by:
Nouman
Shamim

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Nouman Shamim

Syllabus Reference

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat

Computer Science 2210


(May/June 2015 onwards)
May/June 2015 P21

2 Read this section of program code that should input 10 positive numbers and then output the
smallest number input.

1 Small = 0

2 Counter = 0

3 REPEAT

4 INPUT Num

5 IF Num < Small THEN Num = Small

6 Counter = Counter + 1

7 PRINT Small

8 UNTIL Counter < 10

There are four errors in this code.

Locate these errors and suggest a corrected piece of code for each error.

1 .......................................................................................................................................................

2 .......................................................................................................................................................

3 .......................................................................................................................................................

4 .......................................................................................................................................................
1 1 mark for each error identified + suggested
correction Line 1 or Small = 0: this should read
Small = 999
line 5 or IF…: this should read IF Num < Small THEN Small = Num
line 8 or UNTIL: this should read UNTIL Counter = 10 or
UNTIL Counter > = 10 or
UNTIL Counter > 9
line 7 or PRINT…: PRINT Small should come after the end of the repeat
loop or
line 8 or UNTIL: this should come before line 7
[4]
Principal Examiner Report
Question 2

Most candidates located at least one error and suggested a suitable piece of corrected code.
The error on line 8 was often identified, with better candidates providing a working correction.
farhan.rahmat@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat

May/June 2015 P22

2 Read this section of program code that should input 30 positive numbers and then output the
largest number input.

1 Large = 9999

2 Counter = 0

3 WHILE Counter > 30

4 DO

5 INPUT Num

6 IF Num < Large THEN Large = Num

7 Counter = Counter - 1

8 ENDWHILE

9 PRINT Large

There are four errors in this code.

Locate these errors and suggest a corrected piece of code for each error.

1 .......................................................................................................................................................

..........................................................................................................................................................

2 .......................................................................................................................................................

..........................................................................................................................................................

3 .......................................................................................................................................................

..........................................................................................................................................................

4 .......................................................................................................................................................

......................................................................................................................................................[4]

2 1 mark for each error identified + suggested correction


Line 1 or Large =9999: this should read Large = 0
Line 3 or WHILE: this should read WHILE Counter < 30
line 6 or IF: this should read IF Num > Large THEN Large = Num
line 7 or Counter =…: this should read Counter = Counter + 1 [4]

Principal Examiner Report


Question 2
Most candidates located at least one error and suggested a suitable piece of corrected code.
The error on line seven was the one identified and corrected by nearly all candidates. The
error on line 3 was often identified, with better candidates providing a working correction.
farhan.rahmat@gmail.com
Topical Questions of O Level Computer Science 2210 by NOUMAN SHAMIMt

Oct/Nov 2015 P22

2 Read this section of program code that should input 50 numbers and then output the average.

1 Total = 0

2 For Counter = 1 TO 50

3 INPUT Num

4 Total = Total + 1

5 Counter = Counter + 1

6 Average = Total/Counter

7 NEXT Counter

8 PRINT Average

There are four errors in this code.

Locate these errors and suggest code corrections to remove each error.

1 .......................................................................................................................................................

..........................................................................................................................................................

2 .......................................................................................................................................................

..........................................................................................................................................................

3 .......................................................................................................................................................

..........................................................................................................................................................

4 .......................................................................................................................................................

......................................................................................................................................................[4]

2 One mark for each error identified + suggested correction


line 4 or (Total =) Total + 1: this should read (Total =) Total + Num
line 5 or Counter = Counter + 1: delete this line
line 6 or (Average = )Total / Counter: swap lines 6 and 7
line 6 or (Average = )Total / Counter : this should read (Average =) Total / 50

Principal Examiner Report


Question 2

Many candidates located at least one error and suggested a suitable piece of corrected
code. The errors on lines 4 and 5 were frequently identified, with stronger responses
providing a working correction. The question asked the candidates to identify and correct
each error; a few candidates either identified the error or corrected the error, but both actions
were required to gain each mark. farhan.rahmat@gmail.com
Topical Questions of O Level Computer Science 2210 by Nouman Shamim

Oct/Nov 2015 P23


2 Read this section of program code that should input 50 numbers and then output the average of
the positive numbers only.

1 Total = 0

2 PosCount = 0

3 FOR Counter = 1 TO 50

4 INPUT Num

5 IF Num < 0 THEN Total = Total + Num

6 IF Num > 0 THEN Counter = Counter + 1

7 Average = Total/PosCount

8 NEXT Counter

9 PRINT Num

There are four errors in this code.

Locate these errors and suggest code corrections to remove each error.

1 .......................................................................................................................................................

2 .......................................................................................................................................................

3 .......................................................................................................................................................

4 .......................................................................................................................................................

......................................................................................................................................................[4]

2 One mark for each error identified + suggested correction


line 5 or IF Num < 0: this should read IF Num > 0 (THEN Total = Total + Num)

line 6 or (IF Num > 0 ) THEN Counter = Counter + 1:


this should read (IF Num > 0 THEN)Poscount = Poscount + 1

line 7 Average = Total/Poscount: this should come after the end of the repeat loop

line 9 or PRINT Num: this should read PRINT Average [4]

Principal Examiner Report


Question 2
Most candidates located at least one error and suggested a suitable piece of corrected code.
The errors on lines 5 and 9 were frequently identified, with stronger responses providing a
working correction. The question asked the candidates to identify and correct each error; a
few candidates either identified the error or corrected the error, but both actions were
required to gain each mark. farhan.rahmat@gmail.com
Topical Questions of O Level Computer Science 2210 by Nouman Shamim

May/June 2016 P21

2 Read this section of program code that inputs 10 positive numbers and then outputs the smallest
number input.

1 Small = 1000
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num < Small THEN Small = Num
6 Counter = Counter + 1
7 UNTIL Counter = 10
8 PRINT Small

(i) Identify three changes you would need to make to find the largest number input instead
of the smallest number.

1 .........................................................................................................................................

...........................................................................................................................................

2 .........................................................................................................................................

...........................................................................................................................................

3 .........................................................................................................................................

.......................................................................................................................................[3]

(ii) Rewrite the program code with your changes.

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

.......................................................................................................................................[3]

farhan.rahmat@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat

2 (i) 1 mark for each change

Change variable name in every instance as needs to be meaningful e.g. Large


Set this variable to a low value
line 5: change comparison from < to > [3]

(ii) 3 marks maximum, 1 mark for each change correctly included.

1 Large = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num > Large THEN Large = Num
6 Counter = Counter + 1
7 UNTIL Counter = 10
8 PRINT Large [3]

Principal Examiner Report


Question 2
(i) Most candidates correctly suggested that the variable name Small should be changed to
Large and the test should be changed from less than to greater than. Many candidates
correctly identified that the variable should be set to zero at the start of the program.
(ii) Most candidates correctly changed the variable name, and the test. A common error was
not to change the assignment at the start of the program.

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat

May/June 2016 P22

2 Read this section of program code that inputs 10 positive numbers and then outputs the total.

1 Total = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 Total = Total + Num
6 PRINT Total
7 Counter = Counter + 1
8 UNTIL Counter = 10

This code works, but it is inefficient.

(i) Suggest three improvements that could be made.

1.................................................................................................................................................

...................................................................................................................................................

2.................................................................................................................................................

...................................................................................................................................................

3.................................................................................................................................................

...............................................................................................................................................[3]

(i) Rewrite the program code with your improvements.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[3]

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat

2 (i) 1 mark for each improvement

use FOR … NEXT instead of REPEAT … UNTIL


Move PRINT to after the end of the loop
Add error checking to check that the value input is positive

(ii) 3 marks maximum, 1 mark for each improvement correctly included.

Sample answer below


1 Total = 0
2 FOR Counter = 1 To 10
3 REPEAT
4 INPUT Num
5 UNTIL Num >0
6 Total = Total + Num
7 NEXT Counter
8 PRINT Total [3]

Principal Examiner Report


Question 2

(i) Most candidates correctly suggested that the PRINT Total statement should be after the
end of the loop. Many candidates correctly identified that the input needed to be checked to
see if it was positive or suggested that the loop should be a FOR … NEXT loop.

(ii) Most candidates correctly moved the PRINT Total statement. Some candidates correctly
converted the REPEAT … UNTIL loop to a FOR … NEXT loop. Some candidates attempted
the validation check; a common error was to total too few positive numbers.

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat

Oct/Nov 2016 P22

2 Read this section of program code that inputs positive numbers, discards any negative numbers
and then outputs the average. An input of zero ends the process.

1 Total = 0
2 Counter = 100
3 REPEAT
4 REPEAT
5 INPUT Num
6 UNTIL Num < 0
7 Total = Total + 1
8 Counter = Counter + Num
9 UNTIL Num = 0
10 Average = Total / (Counter - 1)
11 Print Average

There are four errors in this code.

Locate these errors and suggest a correction to remove each error.

Error 1 ..............................................................................................................................................

Correction .........................................................................................................................................

Error 2 ..............................................................................................................................................

Correction .........................................................................................................................................

Error 3 ..............................................................................................................................................

Correction .........................................................................................................................................

Error 4 ..............................................................................................................................................

Correction .........................................................................................................................................

..................................................................................................................................................... [8]

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat
2 1 mark for identifying each error, 1 mark for the corresponding change

– line 2 or Counter = 100


– Counter = 0

– line 6 or UNTIL Num < 0


– UNTIL Num >= 0

– line 7 or Total = Total + 1


– Total = Total + Num

– line 8 or Counter = Counter + Num


– Counter = Counter + 1 [8]

Principal Examiner Report


Question 2

Most candidates correctly identified at least one error. Few candidates showed
understanding of the program code by suggesting that the UNTIL Num < 0 should be
replaced by UNTIL Num >= 0 to allow the process to end correctly. Many candidates
correctly identified that Counter and Total were updated incorrectly and could provide the
necessary corrections.

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat
Oct/Nov 2016 P23

2 Read this section of program code that:

• inputs 10 numbers
• checks whether each number is within a specified range
• totals the numbers within the range and outside the range

1 InRange = 0
2 OutRange = 1000
3 FOR Count = 1 TO 10
4 INPUT Num
5 IF Num > 10 AND Num < 20 THEN InRange = InRange + 1
6 ELSE OutRange = OutRange - 1
7 Count = Count + 1
8 NEXT X
9 PRINT InRange, OutRange

(a) There are four errors in this code.

Locate these errors and suggest a correction to remove each error.

Error 1........................................................................................................................................

Correction ..................................................................................................................................

...................................................................................................................................................

Error 2........................................................................................................................................

Correction ..................................................................................................................................

...................................................................................................................................................

Error 3........................................................................................................................................

Correction ..................................................................................................................................

...................................................................................................................................................

Error 4........................................................................................................................................

Correction ..................................................................................................................................

...............................................................................................................................................[4]

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat
(b) Decide, with reasons, whether the numbers 10 and 20 are within or outside the range.

Within
range (✓) Outside
Number range (✓) Reason

10
……………………………………………………………………..

……………………………………………………………………..

20
……………………………………………………………………..

……………………………………………………………………..
[4]
2 (a) 1 mark for each change
– Line 2: OutRange = 0
– Line 6: should be OutRange = OutRange + 1
– Line 7: not needed
– Line 8: NEXT X should be NEXT Count / Line 3: FOR Count = 1 TO 10 should be
FOR X = 1 TO 10 [4]

(b)

Number Within Outside Reason


range () range ()

10 Range greater than 10, so 10 not included


…………………………………………………………

20 Range less than 20, so 20 not included


…………………………………………………………

[4]
Principal Examiner Report
Question 2
(a) The full range of marks was seen for this question on errors within program code, with
candidates’ correct responses divided amongst all the correct possibilities. Common errors
included giving an incorrect initial value to OutRange, such as 1, or applying an incorrect
variable to NEXT X, such as NEXT Number.
(b) Most candidates correctly identified that both 10 and 20 were outside the range. Better
candidates gave good reasons for this being the case. Common mistakes were for
candidates to not fully explain their reason, such as by simply stating erroneous or invalid
data, which was not enough for the reason mark.

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat
May/June 2017 P21

2 This section of program code asks for 50 numbers to be entered. The total and average of the
numbers are calculated.

1 Total = 0
2 Counter = 50
3 PRINT ′When prompted, enter 50 numbers, one at a time′
4 REPEAT
5 PRINT ′Enter a number′
6 INPUT Number
7 Total + Number = Total
8 Number = Number + 1
9 UNTIL Counter = 50
10 Average = Number * Counter
11 PRINT ′The average of the numbers you entered is ′, Average

There are four errors in this code.

State the line number for each error and write the correct code for that line.

Error 1 Line number .............................

Correct code.............................................................................................................................

Error 2 Line number .............................

Correct code.............................................................................................................................

Error 3 Line number .............................

Correct code.............................................................................................................................

Error 4 Line number .............................

Correct code.............................................................................................................................
[4]
1 mark for each error identified and suggested correction (the corrected code must be written in
full)
Line 2 Correct code Counter = 0 (1)

Line 7 Correct code Total = Total + Number // Number + Total (1)

Line 8 Correct code Counter = Counter + 1 // 1 + Counter (1)

Line 10 Correct code Average = Total / Counter //


Average = Total / 50 (1)

Principal Examiner Report


Question 2
Candidates were able to spot a range of typical errors in program code. Common additional
errors included candidates altering = signs to back arrows, when the question states that it is
written in program code. Also, a small number of candidates offered descriptive answers
when they were asked to provide corrections for the errors in code.

farhan.rahmat@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat
May/June 2017 P22
4 An algorithm has been written in pseudocode to input 100 numbers and print out the sum.
A REPEAT … UNTIL loop has been used.

Count ← 0
Sum ← 0
REPEAT
INPUT Number
Sum ← Sum + Number
Count ← Count + 1
UNTIL Count > 100
PRINT Sum

(a) Find the error in the pseudocode and suggest a correction.

Error........................................................................................ ...................................................

Correction ............................................................................................................................. ....


...................................................................................................................................................
[2]

(b) Rewrite the correct algorithm using a more suitable loop structure.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[3]

farhan.rahmat@gmail.com
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat

Question Answer Marks

4(a) Error - Count 0 2


Correction - Count 1
or
Error - UNTIL Count > 100
Correction - UNTIL Count >= 100 or UNTIL Count = 100
or
UNTIL Count > 99

4(b) - use of FOR with correct start and end values . 3


- . use of NEXT
- . removal of increment for Count
Sample algorithm
Sum 0
FOR Count 1 TO 100
INPUT Number
Sum Sum + Number
NEXT // NEXT Count
PRINT Sum

Principal Examiner Report


Question 4
(a) Generally well answered.
(b) Candidates found this part more challenging with weaker candidates not removing the
statement to increment the variable Count.

nouman.shamim@gmail.co
m
Topical Questions of O Level Computer Science 2210 by Farhan Rahmat
Oct/Nov 2017 P22
Oct/Nov 2017 P23

2 This section of program code asks for 80 numbers between 100 and 1000 to be entered. It checks
that the numbers are in the correct range, and stores them in an array. It counts how many of the
numbers are larger than 500 and then outputs the result when the program is finished.

1 Count = 0
2 FOR Index = 1 TO 80
3 INPUT 'Enter a number between 100 and 1000', Number
4 WHILE Number = 99 AND Number = 1001
5 INPUT 'This is incorrect, please try again', Number
6 ENDWHILE
7 Num[80] = Number
8 IF Number > 500 THEN Count = Count + 1
9 UNTIL Index = 80
10 PRINT Index
11 PRINT ' numbers were larger than 500'

There are four lines of code that contain errors.

State the line number for each error and write the correct code for that line.

Error 1 Line Number .............................

Correct Code ....................................................................................................................................

Error 2 Line Number .............................

Correct Code ....................................................................................................................................

Error 3 Line Number .............................

Correct Code ....................................................................................................................................

Error 4 Line Number .............................


Correct Code ....................................................................................................................................
[4]

Nouman.shamim@gmail.com
Question Answer Marks

2 1 mark for each error identified plus suggested correction (the corrected 4
lines must be written in full)

Line 4 correct line WHILE Number <= 99 OR Number > 1000

Line 7 correct line Num[Index] = Number

Line 9 correct line NEXT (Index)

Line 10 correct line PRINT Count

Principal Examiner Report


Question 2
The full range of marks was seen for this question. A common mistake seen as a correction
for the error in line 4 involved the AND in the checking range just being replaced with an OR.
This would not work in this case; an OR together with correcting the actual range was
needed. For line 7, some candidates did not spot that a variable was needed to ensure the
number was inserted into the correct array element. For line 9, some candidates did not spot
that the line given was not the correct end statement for the FOR loop in the program.
Candidates generally understood how line 10 could be corrected.

Nouman.shamim@gmail.com
Topical Questions of O Level Computer Science 2210 by Nouman Shamim
May/June 2018 P21

May/June 2018 P22

Oct/Nov 2018 P22

Oct/Nov 2018 P23

4 This is a section of program code.

1 Total = 100.00
2 PRINT ′Enter the height of each member of your class, one at a
time, when prompted′
3 FOR Count = 1 TO 30
4 PRINT ′Enter a height in metres′
5 INPUT Height
6 Total = Total + Height
7 PRINT Total / 30
8 Count = Count + 1
9 NEXT Count

(a) There are three errors in this code.

State the line numbers that contain the errors and describe how to correct each error.

Error 1 .......................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

Error 2 .......................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

Error 3 .......................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................
[3]

(b) State the purpose of this program.

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[1]

Nouman.shamimt@gmail.com
Topical Questions of O Level Computer Science 2210 by Nouman Shamim

Principal Examiner Report


Question 4
(a) Many candidates correctly identified all three errors. The error in line 1 was identified by
more candidates than the other two errors.
(b) Most candidates gave the correct answer.

Nouman.shamimt@gmail.com
Topical Questions of O Level Computer Science 2210

May/June 2019 P21

May/June 2019 P22

Oct/Nov 2019 P22

Oct/Nov 2019 P23

Nouman4@gmail.com
Topical Questions of O Level Computer Science 2210 by nouman
shamimt

Computer Studies 7010


(Oct/Nov 2006 till Oct/Nov 2014)
Oct/Nov 2006
9 A computer program is required which inputs 10 numbers, multiplies them together and
finally outputs the answer (the product). The following algorithm has been written to do this.

1 count = 0
2 product = 0
3 while count <= 10 do
4 input number
5 product = product * number
6 count = count + 1
7 print product
8 endwhile

(a) There are three errors in the algorithm. Locate and describe these errors. [3]
(b) A while … do loop has been used in the algorithm. State another type of loop that
could have been used. [1]

9 (a) error 1: product = 0 on line 2


should use product = 1
error 2: loop control, count <= 10 on line 3
should use count < 10 or alternatively alter count value on line 1 to count = 1
error 3: print value of product inside loop on line 7
output should come after the endwhile statement [3]
(b) Accept either of the following loop controls:

Repeat for count = 1 to 10


OR
until count = 10 next count
(accept repeat
until count > 11
if line 1 changed to count = 1) [1]

May/June 2010. P11

12 A golf course charges $10 for each game of two people. Each additional person incurs
afurther charge of $2 per game. If they book two or more games in advance, they get a
10%discount on the total charge.
The following program has been written in pseudocode to calculate the charges for a
game.
1 extracost = 0
2 input numberpeople, numbergames
3 charge = 10 * numbergames
4 extrapeople = numberpeople – 2
5 if numberpeople< 2 then extracost = 2 * extrapeople * numbergames
6 charge = extracost
7 if numbergames> 1 then charge = charge * 0.1
8 print charge

Nouman4@gmail.com
Topical Questions of O Level Computer Science 2210 by nouman
shamimt
There are three errors in the program. Locate these errors and suggest a correct piece
ofcoding. [6]

12 1 mark for each error identified + 1 mark for each suggested correction
– error
line 5: numberpeople < 2 is incorrect
correction:
numberpeople > 2

– error
line 6: the formula/charge = extracost is incorrect
correction:
charge = extracost + charge

– error
line 7: discount calculation/charge = charge * 0.1 is incorrect,
correction:
charge = charge * 0.9 [6]

Oct/Nov 2010. P11


9 The following algorithm inputs 20 numbers and outputs how many numbers were positive
(> 0) and how many numbers were negative (< 0).

1 negative = 1
2 positive = 1
3 for count = 1 to 20 do
4 input number
5 if number < 0 then negative = negative + 1
6 if number > 0 then positive = positive + 1
7 count = count + 1
8 print negative, positive
9 next count

There are three different errors in this algorithm.


Locate each error and give the reason why you think it is an error. [6]

9 1 mark for each error and 1 mark for reason why it is an error
– line 1/negative=1 and/or line 2/positive=1
– negative and/or positive should be set to zero
– line 7/count=count+1
– don’t need a count within a for …. to next loop
– replace loop with a repeat...until loop
– line 8/print negative, positive or line 9/next count
– outputs should come after the next count statement [6]

Nouman4@gmail.com
Topical Questions of O Level Computer Science 2210 by nouman
shamimt
May/June 2011 P12
7 (a) Read the following section of code that inputstwenty (20) numbers and then outputs
the largestnumber input.
1h=0
2c=0
3 REPEAT
4 READ x
5 IF x > h THEN x = h
6c=c+1
7 PRINT h
8 UNTIL c < 20
There are THREE errors in this code.
Locate these errors and suggest a corrected pieceof code. [3]
7 (a) 1 mark for each error identified + suggested correction
line 5: this should read if x > h then h = x
line 7: print h should come after the end of the repeat loop
line 8: this should read until c = 20 or until c >= 20 or until c > 19 [3]

Oct/Nov 2013. P12

8 A piece of pseudocode was written to input 1000 positive numbers and then output
thehighest and lowest numbers.
10 highest = 0
20 lowest = 0
30 for count = 1 to 100
40 input number
50 if number > highest then number = highest
60 if number < lowest then number = lowest
70 count = count + 1
80 next count
90 print highest, lowest

There are errors in the code.


Locate these errors and suggest a correction. [8]

(8) 1 mark for error + 1 mark for suggested correction to error (max of FOUR errors)

Nouman4@gmail.com
Topical Questions of O Level Computer Science 2210 by nouman
shamimt
May/June 2014 P12

5 The following algorithm should:


• input ten numbers
• output the largest number input
• output the average value of the input data
10 largest = 0
20 sum = 0
30 for x = 1 to 10
40 input x
50 if x > largest then x = largest
60 output largest
70 sum = sum + x
80 next x
90 average = sum * 10
100 output average
There are four errors in this algorithm.
Locate these errors and suggest a correction.

5 1 mark for identification of error and suggested correction (description or corrected


pseudocode)
Error: line 40: input x; using same input value as loop variable will cause problems or
line 30: for x = 1 to 10
correction: change loop variable e.g. for count = 1 to 10 or change input variable e.g.
input number
error: line 50: formula is reversed
correction: …. then largest = x (or largest = number)
error: line 60: output shouldn’t be inside the loop
correction: 100 output average, largest
error: line 90: incorrect formula
correction: average = sum/10 [4]

Oct/Nov 2014. P12

6 The following section of a pseudocode algorithm should:


• input 500 numbers
• generate a ratio called k
• output each value of k
• output how many numbers were larger than 10
10 total = 1
20 FOR x = 1 TO 500
30 IF number < 10 THEN total = total + 1
40 k = x / number
50 x=x+1
60 OUTPUT k
70 NEXT x
80 OUTPUT x
(a) There are five errors in the above code.
Locate these errors and suggest a correction.

Nouman4@gmail.com
Topical Questions of O Level Computer Science 2210 by nouman
shamimt
error 1 .......................................................................................................................................
correction ..................................................................................................................................
...................................................................................................................................................
error 2 .......................................................................................................................................
correction ..................................................................................................................................
...................................................................................................................................................
error 3 .......................................................................................................................................
correction ..................................................................................................................................
...................................................................................................................................................
error 4 .......................................................................................................................................
correction ..................................................................................................................................
...................................................................................................................................................
error 5 .......................................................................................................................................
correction ..................................................................................................................................
............................................................................................................................................... [5]
6 (a) 1 mark for each error and suggested correction (accept description or example of
corrected pseudocode).
error: line 10: total = 1
correction: totals should be set to zero; total = 0
error: line 30: … number < 10 …
correction: check should be made if number > 10; … number > 10 …
error: no input inside loop
correction: input number
error: line 50: x = x + 1
correction: for … to loops don’t need a counter; remove line 50 altogether
error: line 80: output x
correction: output should be total value; output total [5]

(b) The corrected algorithm was converted to a computer program and run. However,
after several numbers were input, the program stopped and an error message was
generated, showing that there was a further error at line 40 (k = x / number).
State what could cause this error to occur.
...................................................................................................................................................
...................................................................................................................................................
Suggest a change to line 40 to overcome this problem.
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................... [2]

(b) division by zero error (or similar description of error produced when dividing by 0)
add an error trap after input of number
e.g. 40 if number = 0 then k = 0 else k = x/number [2]

Nouman4@gmail.com
Topical Questions of O Level Computer Science 2210 by nouman
shamimt
Oct/Nov 2014. P13

6 The following pseudocode algorithm should:


• input up to 20 numbers
• stop if the sum of the input numbers exceeds 50
• output the final sum
10 count = 0
20 REPEAT
30 INPUT n
40 n + sum = sum
50 IF sum = 50 THEN count = 20
60 count = count + 1
70 UNTIL count = 20
80 OUTPUT n
There are five errors in this algorithm.
Locate these errors and suggest a correction.
error 1 .......................................................................................................................................
correction ..................................................................................................................................
...................................................................................................................................................
error 2 .......................................................................................................................................
correction ..................................................................................................................................
...................................................................................................................................................
error 3 .......................................................................................................................................
correction ..................................................................................................................................
...................................................................................................................................................
error 4 .......................................................................................................................................
correction ..................................................................................................................................
...................................................................................................................................................
error 5 .......................................................................................................................................
correction ..................................................................................................................................
............................................................................................................................................... [5]

Nouman4@gmail.com
Topical Questions of O Level Computer Science 2210 by nouman
shamimt
Practice Questions:
Questions 1 to 3 contain sections of pseudocode which contain errors. Locate the errors
and suggest the correct coding. Questions 4 to 10 are problems which require an algorithm
to be written in pseudocode – there is “no right answer” here; as long as the pseudocode
works then the solution is acceptable.

(1) The following section of pseudocode inputs 1000 numbers and then outputs how many
were negative, how many were positive and how many were zero.
Locate the 3 errors and suggest a corrected piece of code.

1 negative  1: positive  1
2 for x  0 to 1000
3 input number
4 if number < 0 then negative  negative + 1
5 if number > 0 then positive  positive + 1
6 endif
7 endif
8 next
9 print negative, positive

(2) The following section of pseudocode inputs rainfall (in cm) for 500 days and outputs the
average rainfall and the highest rainfall input.
Locate the 3 errors and suggest a corrected piece of code.

1 highest  1000
2 days  1
3 while days > 0
4 input rainfall
5 if rainfall > highest then highest  rainfall
6 endif
7 total  total + rainfall
8 days  days + 1
9 average  total/500
10 endwhile
11 print average, highest

(3) The following section of pseudocode inputs a number, n, multiplies together 1 x 2 x 3 x


……. x n, calculates input number/sum and outputs result of the calculation.
Locate the 3 errors and suggest a corrected piece of code.

1 input n
2 for mult 1 to n
3 sum  0
4 sum  sum * mult
5 result  n/sum
6 next
7 print result

Nouman4@gmail.com

You might also like