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 w... 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
5.1.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 used to avoid repetition of code, allowing programmers to write reusable code blocks. This enhances readability and makes it easier to debug individual modules, thereby improving collaboration between programmers.
Step 2
5.1.2 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.
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
The statement is invalid. The expression 'Y = 2' evaluates to a Boolean value, and in the assignment 'X := Y = 2', 'X' is expected to hold a Boolean value. Therefore, it conflicts with the type expectations.
Step 3
5.2.2 If Name1 > Name2 then where Name1 and Name2 are declared as string type variables.
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 statement is valid. In many programming languages, string comparisons are possible, and it evaluates based on lexicographical order.
Step 4
5.3.1 Explain what defensive programming is.
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
Defensive programming is a practice that involves writing code to anticipate and handle potential errors, ensuring that the program behaves predictably under various unexpected circumstances. This helps prevent runtime errors and improves the robustness of the code.
Step 5
5.3.2 While the program was running, an error caused the program to stop executing. The following error message was displayed: 'Execution halted - an overflow error occurred'. (a) Give a possible reason for an overflow error.
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
One possible reason for an overflow error is when a value exceeds the maximum limit defined for a data type. For instance, if an integer variable attempts to store a value that is outside the allowable range for that type, it results in an overflow.
Step 6
(b) State a way in which programming code can be used to prevent a runtime 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
To prevent runtime errors, data validation can be implemented. This involves checking input values to ensure they are within acceptable limits before they are processed.
Step 7
5.4.1 State the purpose of a constructor method.
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
The purpose of a constructor method is to instantiate, create, or initialize an object when it is created. It often sets initial values for the object's attributes.
Step 8
5.4.2 Identify an accessor method from the class diagram.
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
The accessor method identified from the class diagram is 'getCompanyName()'.
Step 9
5.4.3 (a) For which attribute will the use of a mutator method be the LEAST applicable?
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 attribute 'CompanyNum' will require the least applicability of a mutator method, as it is intended to uniquely identify the company and should not be changed.
Step 10
(b) Motivate your answer to QUESTION 5.4.3(a).
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
Changing 'CompanyNum' could lead to inconsistencies in identifying the company, thereby violating unique identification, making it impractical to have a mutator method for this attribute.
Step 11
5.4.4 (a) State where in the class diagram access specified to methods/attributes violates the recommended/allowed access to methods/attributes.
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
The attributes 'CompanyNum' and 'NumberOfEmployees' are declared public, which violates the principle of encapsulation. They should be private to restrict direct access from outside the class.
Step 12
(b) Explain why the incorrect access to methods/attributes identified in QUESTION 5.4.4(a) might cause a problem when working with an object of this class.
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
Incorrect access to methods or attributes can lead to unintended modifications and may compromise the integrity of the object's state. If attributes are publicly accessible, any external code can alter them, resulting in unpredictable behaviors and bugs.
Step 13
5.5.1 When would a sentinel controlled/conditional loop be preferred to other loops?
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
A sentinel controlled/conditional loop is preferred when the number of iterations is unknown beforehand, and the loop continues until a specific condition is met, making it ideal for situations requiring indefinite repetition until a condition signals completion.
Step 14
5.5.2 The following code was used to generate a random number in the range 1 to 10, excluding numbers 5 and 8. Rewrite the code using a WHILE loop.
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
To rewrite the given code using a WHILE loop:
iNumber := RandomRange(1,11);
While (iNumber = 5) OR (iNumber = 8) do
iNumber := RandomRange(1,11);