0% found this document useful (0 votes)
14 views

Error Detection & Code Improvements

The document contains sample computer science exam questions and answers for O Levels. It has multiple sections with program code snippets that have errors and the task is to identify the errors and suggest corrections. It also contains questions about modifying code to produce different outputs and improving code efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Error Detection & Code Improvements

The document contains sample computer science exam questions and answers for O Levels. It has multiple sections with program code snippets that have errors and the task is to identify the errors and suggest corrections. It also contains questions about modifying code to produce different outputs and improving code efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

O LEVELS 2210

COMPUTER SCIENCE TOPICAL PAST


Compiled By: Engr. Shahzadah Ashraf Bande’Shah PAPERS
Edited By: Muhammad Jawwad BOOK 2.1

[All Variants Included + Marking


Schemes]

STUDENTS NAME:

_____________________________________________

For More Resources:

SCHOOL NAME: https://www.bandashah.com

_____________________________________________

Join Our Facebook Page:


https://www.facebook.com/groups/
DATE ISSUED: olevelcomputers/

_____________________________________________
Follow On Facebook :
https://www.facebook.com/shahza
CONTACT NUMBER: dah.ashraf

_____________________________________________

shahzadah.ashraf +92333 2076121


@gmail.com
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 .......................................................................................................................................................

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

2210/21/M/J/15
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]

2210/22/M/J/15 [Turn over


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]

2210/21/M/J/16
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]

(ii) Rewrite the program code with your improvements.

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

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

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

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

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

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

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

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

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

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

2210/22/M/J/16
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]

2210/21/M/J/17 [Turn over


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]

2210/22/M/J/17
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]

2210/22/O/N/15
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]

2210/23/O/N/15
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]

2210/22/O/N/16
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]

2210/23/O/N/16
(b) Decide, with reasons, whether the numbers 10 and 20 are within or outside the range.

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

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

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

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

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

[4]

2210/23/O/N/16 [Turn over


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]

2210/23/O/N/17 [Turn over


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]

2210/23/O/N/18
Page 5 Mark Scheme Syllabus Paper
Cambridge O Level – May/June 2015 2210 21

2 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]

Page 4 Mark Scheme Syllabus Paper


Cambridge O Level – May/June 2015 2210 22

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]

Page 4 Mark Scheme Syllabus Paper


Cambridge O Level – May/June 2016 2210 21

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]
Page 4 Mark Scheme Syllabus Paper
Cambridge O Level – May/June 2016 2210 22

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 [3]

(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]

Cambridge O Level – Mark Scheme


PUBLISHED May/June 2017
2210/21

Question Answer Marks

2 1 mark for each error identified and suggested correction (the corrected 4
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)
Cambridge O Level – Mark Scheme
2210/22 PUBLISHED May/June 2017

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

Page 4 Mark Scheme Syllabus Paper


Cambridge O Level – October/November 2015 2210 22

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
[4]

Page 4 Mark Scheme Syllabus Paper


Cambridge O Level – October/November 2015 2210 23

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]


Page 4 Mark Scheme Syllabus Paper
Cambridge O Level – October/November 2016 2210 22

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]

Page 3 Mark Scheme Syllabus Paper


Cambridge O Level – October/November 2016 2210 23
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 ( 9) range ( 9)

10 9 Range greater than 10, so 10 not included


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

20 9 Range less than 20, so 20 not included


… … … …

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

[4]

2210/23 Cambridge O Level – Mark Scheme October/November


PUBLISHED 2017

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


2210/23 Cambridge O Level – Mark Scheme October/November 2018
PUBLISHED

Question Answer Marks

4(a) 1 mark for each error identified plus suggested correction 3

Line 1 or Total = 100.00: correction Total = 0(.00)

Line 8 or Count = Count + 1: correction This line should be removed (not required in a FOR loop) // use of
REPEAT…UNTIL or WHILE…DO…ENDWHILE

Line 7 or PRINT Total /30: correction This line should be outside the loop (or it will print each iteration)

4(b) 1 mark for correct purpose: 1


Find/output average height

You might also like