Computer Science 9618/22 — May/June 2023
Cambridge AS Level · Fundamental Problem-solving and Programming Skills · worked solutions for every part, with the mark scheme
Topics Programming · Software Development · Data Types and Structures · Algorithm Design and Problem-solving
Refer to the insert for the list of pseudocode functions and operators.
A program calculates the postal cost based on the weight of the item and its destination. Calculations occur at various points in the program and these result in the choice of several possible postal costs. The programmer has built these postal costs into the program.
For example, the postal cost of $3.75 is used in the following lines of pseudocode:
IF Weight < 250 AND ValidAddress = TRUE THEN
ItemPostalCost ← 3.75 // set postal cost for item to $3.75
ItemStatus ← "Valid" // item can be sent
ENDIF
Identify a more appropriate way of representing the postal costs.
Answer
- Use named constants for the postal costs.
Named constants
Background Concept
A constant is a named value that does not change while the program runs. In program design, constants are often used for fixed values such as tax rates, conversion factors or prices.
If a programmer writes a value like 3.75 directly inside statements, that value is called a hard-coded literal. Hard-coded literals work, but they are not usually the best design choice when the same value may appear in several places.
Using a named constant gives the value a meaningful name and stores it in one place.
Understanding the Question
The program uses postal charges such as 3.75 directly in the pseudocode. The question asks for a more appropriate way to represent those postal costs.
The key clue is that these are fixed charges that the programmer has built into the program. That points to constants rather than ordinary variables.
Approach
Look at the role of the value:
- it is a preset value
- it represents a known postal charge
- it is likely to be used in more than one place
That makes a named constant the best answer.
Step-by-Step Reasoning
In the example, the program contains:
ItemPostalCost ← 3.75
The value 3.75 is a literal. Instead of repeating that literal, the programmer could declare a constant with a meaningful name, such as a constant representing the charge for that category of item.
Then the assignment would use the constant name rather than the number itself. This is more appropriate because the postal cost is a fixed programmed value, not a value entered by the user or calculated differently each time.
So the required answer is to represent the postal costs as named constants.
Key Takeaways
- Fixed values in a program should usually be stored as constants.
- A named constant is better than scattering literal numbers through the code.
- This improves the quality of the program design.
Common Mistakes
- Saying "variable" instead of "constant". A variable is for a value that can change during execution.
- Saying "array" without justification. An array might store several costs, but the direct improvement being tested here is replacing literals with constants.
- Repeating the example value only, rather than naming the programming feature.
Things to Be Careful About
- The question asks for a way of representing the postal costs, not for rewriting the whole program.
- The important word is more appropriate, so the answer should focus on software design quality.
- In Cambridge pseudocode, a constant should be clearly treated as fixed and given a meaningful identifier.
Describe the advantages of your answer to part (a)(i) with reference to this program.
Answer
- If a postal cost changes, the value only needs to be changed once.
- This reduces the chance of missing one occurrence and leaving different parts of the program with different postal costs.
- Meaningful constant names make the program easier to read and understand than using values such as
3.75directly.
Change once; fewer errors; clearer code
Background Concept
A good program is not judged only by whether it works. It should also be easy to read, maintain and update. Constants help with this.
When the same numeric value is written directly in many places, changing that value later becomes harder. If the programmer forgets one occurrence, the program becomes inconsistent. Named constants solve that problem because the value is defined once and then referred to by name wherever needed.
This is especially useful in real-world systems such as pricing, postage, tax or discount calculations, where fixed values may change over time.
Understanding the Question
Part (a)(ii) asks for the advantages of the answer to part (a)(i), with reference to this program. So it is not enough to say vague things like "it is better". The answer should connect directly to the postal cost program.
The program uses several possible postal costs. That means the same charge values may appear in multiple places. If those values are stored as named constants, there are clear benefits for this exact situation.
Approach
Think about what happens when a postage price changes.
- Would the programmer need to search through the whole program?
- Could one old value be left behind by mistake?
- Would a name be easier to understand than a bare number?
These lead naturally to the three strongest advantages:
- easier to update
- less chance of errors or inconsistency
- improved readability
Step-by-Step Reasoning
Suppose the cost 3.75 is used in several IF statements.
If the programmer writes 3.75 directly each time:
- every occurrence must be found and edited if the price changes
- one occurrence could easily be missed
- a reader has to guess what
3.75represents
If instead the program uses a named constant:
- the programmer changes the value once, in the constant declaration
- every use of that constant automatically uses the new value
- the program is less likely to contain mixed old and new prices
- the constant name explains the meaning of the charge
For example, a name tells the reader that the value is a postal charge for a particular case, whereas 3.75 on its own tells the reader nothing about why that value is used.
That is why the best advantages are easier maintenance, fewer mistakes and improved clarity.
Key Takeaways
- Constants are useful when fixed values are reused.
- Named constants improve maintainability because one edit updates all uses.
- They also improve reliability and readability.
Common Mistakes
- Giving only one advantage when the question asks for several.
- Saying only "easier" without explaining why it is easier.
- Describing variables instead of constants.
- Forgetting to refer to this specific program about postal costs.
Things to Be Careful About
- The benefit is not that the value can change during execution; constants do not do that.
- The maintenance advantage matters because postal charges may be updated in future.
- Readability comes from the name, not just from storing the value elsewhere.
- Avoid repeating the same idea in different words; examiners usually want distinct points.
The lines of pseudocode contain features that make them easier to understand.
State three of these features.
1 ................................................................................................................................................
2 ................................................................................................................................................
3 ................................................................................................................................................
Answer
- Meaningful identifier names are used, for example
Weight,ValidAddress,ItemPostalCost. - Indentation/layout shows that the statements belong inside the
IFblock. - Comments are included to explain what the statements do.
Meaningful names; indentation; comments
Background Concept
Pseudocode is written to communicate an algorithm clearly to a human reader. Because of that, presentation matters. Good pseudocode usually includes:
- meaningful names for variables and data items
- clear block structure shown by indentation
- comments where useful
- consistent formatting and keyword use
These features do not change what the algorithm does, but they make it easier to read, check and maintain.
Understanding the Question
The question points to the example lines of pseudocode and asks for three features that make them easier to understand. So the answer must come from the way the pseudocode is written, not from what the postal system itself does.
You are being asked to notice readability features in the example.
Approach
Look at the given code and ask:
- Are the variable names descriptive?
- Is the structure visually clear?
- Are there comments explaining purpose?
The strongest three visible features are meaningful names, indentation and comments.
Step-by-Step Reasoning
In the example:
Weightclearly suggests the mass of the item.ValidAddressclearly suggests whether the address is acceptable.ItemPostalCostclearly suggests the cost being assigned.ItemStatusclearly suggests the status of the item.
These names are much better than vague names like A, B or X.
Next, the two assignment lines are indented under the IF statement. That makes it obvious that they only happen when the condition is true.
Finally, the comments:
// set postal cost for item to $3.75// item can be sent
explain the purpose of those statements in plain language.
All three features help a reader understand the code more quickly and reduce the chance of misunderstanding it.
Key Takeaways
- Readable pseudocode uses descriptive identifiers.
- Indentation shows structure and nesting.
- Comments explain intention, especially when a statement might not be obvious.
Common Mistakes
- Naming programming constructs like
IFandENDIFas readability features on their own. They are part of pseudocode syntax, but stronger answers focus on features such as names, indentation and comments. - Giving features that are not visible in the example.
- Repeating the same idea twice, such as "good names" and "descriptive names" as separate points.
Things to Be Careful About
- The question asks for features that make the code easier to understand, so focus on presentation and clarity.
- Choose three distinct features.
- If using examples, make sure they come from the given pseudocode.
Give the appropriate data types for the following variables:
ValidAddress ........................................................................................................................
ItemPostalCost ...................................................................................................................
ItemStatus ............................................................................................................................
Answer
ValidAddress—BOOLEANItemPostalCost—REALItemStatus—STRING
BOOLEAN, REAL, STRING
Background Concept
A data type tells us what kind of value a variable can store. Choosing the correct data type is important because it affects how the value is stored, checked and used in expressions.
Common data types in Cambridge pseudocode include:
BOOLEANfor values that are onlyTRUEorFALSEINTEGERfor whole numbersREALfor numbers with a fractional partSTRINGfor text
Understanding the Question
The question gives three variable names and asks for the appropriate data type for each one. The clue comes from how those variables are used in the example pseudocode.
So the task is to look at the values assigned to them and the way they appear in conditions.
Approach
For each variable, identify the sort of value it holds:
- logical true/false value
- number
- text
Then choose the matching data type.
Step-by-Step Reasoning
ValidAddress appears in the condition:
ValidAddress = TRUE
That means it can hold either TRUE or FALSE, so its data type is BOOLEAN.
ItemPostalCost is assigned the value:
3.75
This is a number with a decimal part, so it is not an INTEGER. The correct type is REAL.
ItemStatus is assigned:
"Valid"
Because this is text enclosed in quotation marks, the data type is STRING.
So the answers are BOOLEAN, REAL and STRING.
Key Takeaways
BOOLEANis for true/false values.REALis for decimal values.STRINGis for text.- The way a variable is used often tells you its type.
Common Mistakes
- Writing
INTEGERforItemPostalCost. That would be wrong because3.75has a fractional part. - Writing
CHARforItemStatus. A character is a single symbol, but"Valid"is several characters, so it is aSTRING. - Confusing a Boolean variable with a string such as
"TRUE".
Things to Be Careful About
- Base your answer on the value shown, not just the variable name.
- In Cambridge pseudocode,
TRUEandFALSEare Boolean values, not strings. - Any numeric value with a decimal point should be treated as
REAL. - Text in quotation marks is normally a
STRINGunless it is exactly one character and the language distinguishesCHARseparately.
The rest of this paper
7 more questions- Q2Programming · Data Types and Structures · Algorithm Design and Problem-solving9M
- Q3Data Types and Structures · Programming10M
- Q4Programming6M
- Q5Programming · Software Development8M
- Q6Programming6M
- Q7Algorithm Design and Problem-solving · Software Development9M
- Q8Programming · Data Types and Structures · Software Development17M