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 essential as it avoids repetition of code. By breaking down a program into smaller modules or functions, developers can ensure that code can be reused across different parts of the application, thereby improving maintainability and reducing errors.
Step 2
5.1.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 significant difference is that a procedure does not necessarily return a value, whereas a function must return a value. This distinction makes functions particularly useful for computations and operations that need to provide a result to the caller.
Step 3
5.2.1 State whether the statement 'X := Y = 2;' is valid or invalid.
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 invalid. The assignment in programming languages should assign values correctly respecting the data types, and in this case, it attempts to assign a Boolean expression to a variable of a boolean type, which is incorrectly structured.
Step 4
5.2.2 State whether the statement 'If Name1 > Name2 then' is valid or invalid.
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 statement is valid. Assuming Name1 and Name2 are string type variables, they can be compared using relational operators; hence this statement is valid.
Step 5
5.3.1 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 coding practice aimed at ensuring the software continues to function under challenging conditions. It involves anticipating potential errors and programming safeguards to prevent these errors from causing undue program failures.
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
One possible reason for an overflow error is when a value is being stored in a variable, and that value exceeds the allowable range for the data type, such as trying to store a value larger than what an integer can hold.
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
To prevent runtime errors, programmers can implement data validation techniques. For example, checking if the input values fall within the allowable range before executing operations can avoid potential errors.
Step 8
5.4.1 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
The primary purpose of a constructor method is to instantiate, create, or initialize an object, ensuring that all necessary starting values for the object's properties are set when the object is created.
Step 9
5.4.2 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 accessor method identified in the class diagram is 'getCompanyName():String', which is used to retrieve the value of the CompanyName attribute without modifying it.
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 attribute 'CompanyNum' would have the least application for a mutator method since it uniquely identifies the company and should remain constant once set.
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
Modifying 'CompanyNum' using a mutator would undermine its role as a unique identifier for the company. It is crucial for maintaining data integrity and consistency within the system.
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
The class diagram indicates that certain attributes, such as 'CompanyName' and 'NumberOfEmployees', are public, which violates the principle of encapsulation. Access to these attributes should be restricted to provide better control over how they are modified.
Step 13
5.4.4(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.
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 lead to unintentional modification of critical data attributes, resulting in inconsistencies or errors throughout the program. For instance, if 'CompanyName' can be changed freely, it may lead to misrepresentation of data within the application.
Step 14
5.5.1 When would a sentinelled/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 sentinelled or conditional loop is preferably used when the number of iterations is unknown beforehand. This allows the program to keep looping until a specific condition is met, offering greater flexibility.
Step 15
5.5.2 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 using a WHILE loop can be rewritten as follows:
iNumber := RandomRange(1,11);
While (iNumber = 5) OR (iNumber = 8) do
begin
iNumber := RandomRange(1,11);
end;