Programming Constructs and Operators: Question 8

Syllabus 8.1

Structured 8 marks

An online store, GreenLeaf, builds a one-line order confirmation message by joining several pieces of text together with the & operator. The store's name never changes while the program runs, so it is declared as a constant rather than a variable.

CONSTANT StoreName = "GreenLeaf"
DECLARE CustomerName : STRING
DECLARE OrderNumber : INTEGER
DECLARE Message : STRING
CustomerName ← "Amara"
OrderNumber ← 4521
Message ← StoreName & " order confirmed for " & CustomerName & ", reference #" & OrderNumber
OUTPUT Message

(a) State one difference between StoreName, declared with CONSTANT, and CustomerName, declared with DECLARE, in terms of how their values may change while the program runs. [2]

(b) State the exact value of Message that is output, showing how each piece is joined by the & operator. [3]

(c) A second order is placed by a customer named "Femi", with OrderNumber 7803. State the exact value of Message output for this order, showing your working. [3]

Show worked solution Hide worked solution

Worked solution

Part (a): CONSTANT versus DECLARE

StoreName is declared with CONSTANT, which means its value, "GreenLeaf", is fixed for the entire time the program runs. Once a constant is declared, it can never be reassigned. There is no line anywhere in the algorithm that changes StoreName.

CustomerName is declared with DECLARE, which makes it a variable. A variable’s value can be assigned, and reassigned again later, while the program runs. This is exactly what happens when CustomerName ← "Amara" gives it a value partway through the algorithm; a different order could assign it a different value again.

[2 marks]: [1] for correctly stating that a CONSTANT’s value cannot change once set, [1] for correctly stating that a DECLAREd variable’s value can be assigned and reassigned.

Part (b): Tracing the concatenation

The & operator joins its operands together, in order, exactly as they are, with no extra characters inserted. Each piece is taken in turn:

PieceValue
StoreNameGreenLeaf
" order confirmed for "<space>order confirmed for<space>
CustomerNameAmara
", reference #",<space>reference #
OrderNumber4521

Joining these five pieces in sequence, one after another:

GreenLeaf order confirmed for Amara, reference #4521

[3 marks]: [1] for correctly substituting StoreName = GreenLeaf and CustomerName = Amara, [1] for correctly substituting OrderNumber = 4521 at the end, [1] for the fully correct final string with spacing exactly matching the literal text pieces (a space before “order”, a space before “Amara”, no space before the comma).

Part (c): A second order

For this order, CustomerName = "Femi" and OrderNumber = 7803. StoreName is still "GreenLeaf", since a CONSTANT cannot change between orders. Substituting these values into the same concatenation:

GreenLeaf & " order confirmed for " & Femi & ", reference #" & 7803

gives:

GreenLeaf order confirmed for Femi, reference #7803

[3 marks]: [1] for keeping StoreName = GreenLeaf unchanged, [1] for correctly substituting CustomerName = Femi and OrderNumber = 7803, [1] for the fully correct final string.

Final answers

  • (a) A CONSTANT cannot be reassigned once declared; a DECLAREd variable can be assigned and reassigned while the program runs.
  • (b) Message = GreenLeaf order confirmed for Amara, reference #4521.
  • (c) Message = GreenLeaf order confirmed for Femi, reference #7803.