Form 5
CXC Past Papers Programming Questions
1. Write a pseudocode algorithm to read a set of positive integers (terminated by 0) and print their average as well as the largest of the set.
2. Write a pseudocode algorithm to print a conversion table from miles to kilometers. The table ranges from 5 to 100 miles in steps of 5 (1 mile = 1.61 kilometers).
3. Write a pseudocode algorithm to read a positive integer N followed by N integers. For these N integers, the algorithm must count and print the number zero and non-zero values. 4. a) Write pseudocode to interchange the values in two variables A and B b) Write an algorithm to read the names and ages of 10 people and print the name of the oldest person. Assume that there are no persons of the same age. Data is supplied in the following form: name, age, name, age etc.
5. a) Write an algorithm to read an integer value for SCORE and print the
appropriate grade based on the following:
SCORE GRADE 80 or more A Less than 80 but 65 or more B Less than 65 but 50 or more C Less than 50 F
b) What is printed by the following algorithm when n = 5?
If (n = 1) OR (n = 2) then
H = 1
Else
F = 1
G =1
For J = 1 to n –2 do
H = F + G
F = G
G = h
Print H
End for
End if
Print F, G
Stop Total marks 8
6. What is printed by the following algorithm?
SUM = 0 N = 20 WHILE N < 30 DO SUM = SUM + N PRINT N, SUM N = N + 3 END WHILE (5 marks) Total marks 5
7. (a) Define the following:
Source Code Object Code Compiler (3 marks) (b) Draw a labeled diagram to illustrate the relationship between source code, object code and a compiler. (1 mark)
(c) Write a pseudocode algorithm to read in THREE numbers and print the highest and lowest number. (6 marks) Total marks 10
8. The following data represents some samples scores obtained by students in a test:
5, 4, 7, 10, 0, 6, 2, 0, 1, 0, 9, 8, 999
999 is the dummy value which terminates the data. Write a pseudocode algorithm to read any data in the above format, and print the number of students scoring 0 and the number of students scoring 10. (10 marks)
9. (a) Copy the following trace table in your answer booklet. X M Y Z 4 1 4 8 4 4 Complete the trace table for the following algorithm, given that the number 4 is the input value for X.
Read X For M = 1 to X do Y = X – M Z = 5 * Y – M End Print Z (4 marks)
(b) What is printed by the algorithm? (1 mark) Total marks 5
10. (a) Write an algorithm which prompts the user to enter the price of an item and which calculates and prints the new price after a discount of 12%. (5 marks)
(b) Write an algorithm to read in TWO numbers into A and B. The algorithm should store the smaller in A and the larger in B, and then print A and B. (5 marks) Total marks 10
11. Write an algorithm to read ELEVEN numbers, find their average and print it. The algorithm should also print the number of times the number 6 occurs in the data.
For example, given the input data: 8 4 6 9 6 5 6 10 7 0 16 The algorithm should print 7 as the average and 3 as the number of times 6 occurs. 12. What is printed by the following algorithm?
COUNT = 1 X = 2 WHILE COUNT < 26 DO X = X * 2 PRINT COUNT, X COUNT = COUNT + 5 END WHILE (5 marks) Total Marks 5
13. (a) Explain the difference between a high level programming language and a machine level language. (2 marks)
(b) Explain the difference between an interpreter and a compiler. (2 marks)
Write a pseudocode algorithm to read two numbers and print the lower value. (6 marks) Total Marks 10
14. The following data represents some sample data of the number of children in several families: 1 3 5 0 4 6 7 3 5 2 4 0 2 999 999 is the dummy value which terminates the data. Write a pseudocode algorithm to read in any data in the above format, and print
(a) The number of families with no children; and
(b) The largest number of children existing in any family. (10 marks) Total Marks 10 (10 marks) 15. a) Write a structured algorithm to read in TWO numbers and print the higher value. b) Write a structured algorithm to interchange the values in TWO variables A and B.
16. a) Copy and complete the trace table below for the following algorithm. X = 5 K = 10 Sum = 45 While Sum < 75 do Sum = Sum + K Print K K = K+ X End while Print Sum c) What is printed by the algorithm?
17. Write a structured algorithm to read the names and scores of fifteen students in a
class. The algorithm must print the name of the student and his/her grade which is
determined according to the grading scale below. The algorithm must also
calculate and print the average score of the class.
GRADING SCALE
SCORE GRADE
80 or More A 65 or more but less than 80 B 50 or more but less than 65 C
Less than 50 F
18 Programs are written with the general purpose of solving problems. However, it is unlikely that a program will run completely error-free on the first attempt. The process whereby a program is checked to ensure that it dose what it was designed for is important in the development of the program. There are two main types of errors that can occur: one type of error occurs when the programmer fails to properly think through the solution to the problem, and the other occurs when the programmer does not adequately know the rules of the programming language. It is good practice to perform a manual trace on the program, using appropriately selected input values, which checks each segment of the program.
State the proper technical term for EACH of the underlined phrases. (5 marks) Total marks 5
19. DIFFERENCE = 0 Input A, B If A <= B Then DIFFERENCE = A – B Else DIFFERENCE = B – A Endif Print DIFFERENCE
(a) What is printed by the algorithm above if the input values are the following? (i) 20 30 (1 mark ) (ii) 100 100 (1 mark ) (iii) 50 10 (1 mark ) (b) Write a structured algorithm to prompts a user to input two values which are then stored in A and B. The algorithm must divide A by B and print the result. The algorithm must keep prompting for the second number as long as the user enters 0 (zero) for the second number. (7 marks) Total marks 10
20. (i) Write a structured algorithm which prompts a user to enter an integer value which is then stored in a variable N. The algorithm must the print each integer from 1 to N and its corresponding square. Each output line must show the integer and its square. You may assume that the user would enter an integer value. (5 marks)
Modify the algorithm in (i) to determine the sum of all the squares. Print this sum if it is less than or equal to 1000. Otherwise, print the following message:
THE SUM IS GREATER THAN ONE THOUSAND (5 marks) Total marks 10