Writing a program with variables, constants, input and strings

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Writing a program with variables, constants, input and strings

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly sets up a constant for a wage rate?
A CONSTANT HourlyRate ← 12.50
B CONSTANT HourlyRate : REAL
2. Which line reads the hours worked from the user?
A Hours ← INPUT
B INPUT Hours
3. Which line correctly calculates the wage?
A Wage ← Hours * HourlyRate
B Hours * HourlyRate ← Wage
4. Which expression gives the whole minutes in TotalSeconds?
A TotalSeconds MOD 60
B TotalSeconds DIV 60
5. Which expression gives the leftover seconds after removing whole minutes?
A TotalSeconds MOD 60
B TotalSeconds DIV 60

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1CONSTANT HourlyRate ← 12.50 AA constant must be given a literal value, not an expression that uses a variable. It should be written as CONSTANT HourlyRate ← 12.50.
2Seconds ← TotalSeconds MOD 60 BSets a fixed pay rate at the top of the wage program. The value is a literal and cannot change while the program runs.
3Read the number of hours worked from the user into a declared variable CWage ← HoursWorked * HourlyRate
4Process the input by working out the wage from the hours and the constant rate DINPUT HoursWorked
5CONSTANT HourlyRate ← HoursWorked * 2 EFinds the seconds left over after taking out whole minutes from a total number of seconds.
6Initial ← SUBSTRING(Name, 1, 3) FA length of 3 returns three characters, not a single initial. The length should be 1: SUBSTRING(Name, 1, 1).

Definitions

DECLARE INPUT OUTPUT

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
// Rate never changes ________ HourlyRate ← 12.50
2.
DECLARE Hours : REAL _____ Hours
3.
// Calculate the wage Wage ← Hours _ HourlyRate OUTPUT "Wage: ", Wage
4.
Minutes ← TotalSeconds DIV 60 Seconds ← TotalSeconds ___ 60
5.
DECLARE Name : STRING Initial ← _________(Name, 1, 1) OUTPUT Initial

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Hours : REAL
B CONSTANT HourlyRate = 12.5
C INPUT Hours
D OUTPUT Hours * HourlyRate
Question 2
A DECLARE Seconds : WHOLE
B DECLARE Mins : INTEGER
C INPUT Seconds
D Mins ← Seconds DIV 60
E OUTPUT "Minutes: ", Mins
Question 3
A DECLARE Surname : STRING
B DECLARE Total : INTEGER
C READ Surname
D Total ← LENGTH(Surname)
E OUTPUT Total
Question 4
A DECLARE Name : STRING
B DECLARE Shout : STRING
C INPUT Name
D Shout ← UPPER(Name)
E OUTPUT Shout
Question 5
A DECLARE Total : INTEGER
B CONSTANT SecsPerMin ← 60
C # Read the total seconds
D INPUT Total
E OUTPUT Total DIV SecsPerMin

Answer Key

Writing a program with variables, constants, input and strings
Section A: Quick Questions
Q1
A — CONSTANT HourlyRate ← 12.50
Q2
B — INPUT Hours
Q3
A — Wage ← Hours * HourlyRate
Q4
B — TotalSeconds DIV 60
Q5
A — TotalSeconds MOD 60
Section B: Match the Pairs
Q1
CONSTANT HourlyRate ← 12.50 → Sets a fixed pay rate at the top of the wage program. The value is a literal and cannot change while the program runs.
Q2
Seconds ← TotalSeconds MOD 60 → Finds the seconds left over after taking out whole minutes from a total number of seconds.
Q3
Read the number of hours worked from the user into a declared variable → INPUT HoursWorked
Q4
Process the input by working out the wage from the hours and the constant rate → Wage ← HoursWorked * HourlyRate
Q5
CONSTANT HourlyRate ← HoursWorked * 2 → A constant must be given a literal value, not an expression that uses a variable. It should be written as CONSTANT HourlyRate ← 12.50.
Q6
Initial ← SUBSTRING(Name, 1, 3) → A length of 3 returns three characters, not a single initial. The length should be 1: SUBSTRING(Name, 1, 1).
Section C: Definitions
Q1
DECLARE INPUT OUTPUT — A complete program declares variables and a CONSTANT, reads values with INPUT, processes them with assignments, then shows results with OUTPUT, using meaningful names and // comments.
Section D: Fill in the Blanks
Q1
// Rate never changes CONSTANT HourlyRate ← 12.50
Q2
DECLARE Hours : REAL INPUT Hours
Q3
// Calculate the wage Wage ← Hours * HourlyRate OUTPUT "Wage: ", Wage
Q4
Minutes ← TotalSeconds DIV 60 Seconds ← TotalSeconds MOD 60
Q5
DECLARE Name : STRING Initial ← SUBSTRING(Name, 1, 1) OUTPUT Initial
Section E: Spot the Error
Q1
B — A constant is given its value with the assignment arrow, not =. It should be CONSTANT HourlyRate ← 12.5.
Q2
A — WHOLE is not a Cambridge pseudocode data type. Whole numbers use INTEGER, so it should be DECLARE Seconds : INTEGER.
Q3
C — Cambridge pseudocode reads a value from the user with INPUT, not READ. It should be INPUT Surname.
Q4
D — The Cambridge function that converts a string to upper case is UCASE, not UPPER. It should be Shout ← UCASE(Name).
Q5
C — Comments in Cambridge pseudocode start with //, not #. It should be // Read the total seconds.