A teacher wants a program to store the maximum number of students allowed in a classroom.
- Write a declaration for a constant called MAX_STUDENTS
with the value 30.
- Declare a variable called currentStudents
.
- State why MAX_STUDENTS
should be declared as a constant rather than a variable.
Answer:
Constant: CONST MAX_STUDENTS ← 30
Variable: currentStudents ← 0
Explanation: The maximum number of students will never change, so it should be fixed as a constant.
For each of the following, state the most appropriate data type:
Answer:
1) Integer
2) Real
3) Character
4) Boolean
5) String
total ← 0
FOR counter ← 1 TO 5
total ← total + counter
NEXT counter
a) What is the final value of total
after the loop finishes?
b) Explain how the assignment statement works in line 2.
c) What type of iteration is being used here?
Answer:
a) Final value = 15 (sum of 1+2+3+4+5)
b) The assignment updates total
each time by adding the current counter value.
c) A FOR loop is being used (count-controlled iteration).
Write pseudocode for a program that:
total ← 0
FOR i ← 1 TO 5
INPUT score
total ← total + score
NEXT i
average ← total / 5
OUTPUT average
number ← 3
FOR i ← 1 TO 4
number ← number * 2
NEXT i
OUTPUT number
a) Complete a trace table for i
and number
.
b) What is the final output?
c) Explain why a FOR loop is appropriate here.
Answer:
Trace table:
i=1 → number=6
i=2 → number=12
i=3 → number=24
i=4 → number=48
Final output = 48
Explanation: The loop runs a fixed number of times, so a FOR loop is the most suitable.