5.1 Procedures and functions are coding constructs often used as part of modular programming - NSC Information Technology - Question 5 - 2022 - Paper 2
Question 5
5.1 Procedures and functions are coding constructs often used as part of modular programming.
5.1.1 Give a reason for the use of modular programming.
5.1.2 State ONE... show full transcript
Worked Solution & Example Answer:5.1 Procedures and functions are coding constructs often used as part of modular programming - NSC Information Technology - Question 5 - 2022 - Paper 2
Step 1
Give a reason for the use of modular programming.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Modular programming is useful as it helps to avoid repetition of code. By using modules or procedures, code can be reused easily across different parts of a program, making it more efficient and reducing the likelihood of errors during coding.
Step 2
State ONE difference between a procedure and a function.
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
A key difference is that a procedure does not necessarily return a value, while a function must return a value.
Step 3
State whether each of the following statements are valid or invalid: 5.2.1 X := Y = 2; where the data type of X is Boolean and Y is integer.
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Invalid. The assignment is incorrect as it attempts to assign a Boolean result to X instead of setting Y to 2.
Step 4
State whether each of the following statements are valid or invalid: 5.2.2 If Name1 > Name2 then where Name1 and Name2 are declared as string type variables.
98%
120 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Valid. String comparison is valid in programming, given that Name1 and Name2 are initialized correctly.
Step 5
Explain what defensive programming is.
97%
117 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Defensive programming is a technique used to ensure that software continues to function under unforeseen circumstances. This involves anticipating potential errors and implementing measures to either avoid them or handle them gracefully.
Step 6
5.3.2 (a) Give a possible reason for an overflow error.
97%
121 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
An overflow error may occur if a number is stored in a variable that exceeds the maximum value the data type can hold. This often happens when calculations result in a value that exceeds the range of the data type being used.
Step 7
5.3.2 (b) State a way in which programming code can be used to prevent a runtime error.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
One way to prevent a runtime error is through data validation, where the inputs are checked to ensure they fall within the acceptable range before being processed.
Step 8
State the purpose of a constructor method.
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
A constructor method is used to instantiate or initialize an object of a class. It sets up the initial state of the object by assigning initial values to attributes.
Step 9
Identify an accessor method from the class diagram.
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The method getCompanyName() serves as an accessor method, as it retrieves the value of the Company's name without modifying the state of the object.
Step 10
5.4.3 (a) For which attribute will the use of a mutator method be the LEAST applicable?
98%
120 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The CompanyNum attribute would require the use of a mutator method the least, because it uniquely identifies the company and should remain constant.
Step 11
5.4.3 (b) Motivate your answer to QUESTION 5.4.3(a).
97%
117 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Changing the CompanyNum would result in inconsistency, as it serves as the unique identifier for the company. Thus, mutating this attribute is generally unnecessary and could lead to errors.
Step 12
5.4.4 (a) State where in the class diagram access specified to methods/attributes violates the recommended/allowed access to methods/attributes.
97%
121 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Some attributes, like +ContactNumber, are declared as public when they should be private to prevent unauthorized access.
Step 13
5.4.4 (b) Explain why the incorrect access to methods/attributes identified in QUESTION 5.4.4(a) may cause a problem when working with an object of this class.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Incorrect access can result in unintended modifications or access to sensitive information, leading to data integrity issues and potential security risks.
Step 14
When would a sentinel controlled/conditional loop be preferred to other loops?
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
A sentinel-controlled loop is preferred when the number of iterations is not known beforehand and you require a specific condition to terminate the loop. This is often used for input validation until a valid set of data is received.
Step 15
Rewrite the code using a WHILE loop.
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The code can be rewritten using a WHILE loop as follows:
iNumber := RandomRange(1,11);
While (iNumber = 5) OR (iNumber = 8) do
iNumber := RandomRange(1,11);