Photo AI

5.1 Procedures and functions are coding constructs often used as part of modular programming - NSC Information Technology - Question 5 - 2022 - Paper 2

Question icon

Question 5

5.1-Procedures-and-functions-are-coding-constructs-often-used-as-part-of-modular-programming-NSC Information Technology-Question 5-2022-Paper 2.png

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 O... 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

Answer

Modular programming is beneficial because it avoids repetition of code, allowing developers to reuse functions and procedures across various parts of the program. This promotes cleaner, more maintainable code.

Step 2

State ONE difference between a procedure and a function.

99%

104 rated

Answer

One key difference is that a procedure does not return a value, while a function must return a value, often using that value as part of an expression.

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

Answer

This statement is invalid because you cannot assign an integer value (2) to a Boolean variable (X); Boolean variables must only hold values of true or false.

Step 4

5.2.2 If Name1 > Name2 then where Name1 and Name2 are declared as string type variables.

98%

120 rated

Answer

This statement is valid as string comparisons are permissible in programming languages, allowing for lexicographical comparisons.

Step 5

Explain what defensive programming is.

97%

117 rated

Answer

Defensive programming is a technique used by programmers to anticipate and mitigate potential errors that could cause a program to fail. This involves creating code that checks assumptions, validates inputs, and handles exceptions gracefully to ensure robust performance.

Step 6

Give a possible reason for an overflow error.

97%

121 rated

Answer

An overflow error can occur when a value being stored in a data type exceeds the maximum limit of that data type. For instance, trying to store a value larger than what an integer can hold will trigger this error.

Step 7

State a way in which programming code can be used to prevent a runtime error.

96%

114 rated

Answer

To prevent runtime errors, programmers can utilize data validation techniques. This involves checking that input values fall within expected ranges before processing them.

Step 8

State the purpose of a constructor method.

99%

104 rated

Answer

The purpose of a constructor method is to initialize an object. It sets default values for data attributes and prepares the object for use upon its creation.

Step 9

Identify an accessor method from the class diagram.

96%

101 rated

Answer

An accessor method from the class diagram is getCompanyName(), which allows retrieval of the company name attribute.

Step 10

For which attribute will the use of a mutator method be the LEAST applicable?

98%

120 rated

Answer

The attribute for which the use of a mutator method will be the least applicable is CompanyNum, as it uniquely identifies the company and should not be changed once set.

Step 11

Motivate your answer to QUESTION 5.4.3(a).

97%

117 rated

Answer

Using a mutator method on the CompanyNum attribute is not advisable as this number is a unique identifier for the company. Changing it could lead to data integrity issues and confusion in identifying the company.

Step 12

State where in the class diagram access specified to methods/attributes violates the recommended/allowed access to methods/attributes.

97%

121 rated

Answer

In the class diagram, the method toString() is specified as public, which is appropriate. However, attributes such as CompanyNum and NumberOfEmployees should ideally be private to protect their integrity.

Step 13

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

Answer

Allowing direct public access to sensitive attributes can lead to unintended modifications. For instance, if the CompanyNum could be altered from outside the class, it might result in references to wrong entities or corruption of object state.

Step 14

When would a sentinel controlled/conditional loop be preferred to other loops?

99%

104 rated

Answer

A sentinel controlled loop is preferred when the number of iterations is unknown ahead of time and you want to continue looping until a specific condition is met, allowing for more flexible control over loop execution.

Step 15

Rewrite the code using a WHILE loop.

96%

101 rated

Answer

Here’s how you could rewrite the provided code using a WHILE loop:

  iNumber := RandomRange(1,11);
  While (iNumber > 5) OR (iNumber < 8) do
    iNumber := RandomRange(1,11);

Join the NSC students using SimpleStudy...

97% of Students

Report Improved Results

98% of Students

Recommend to friends

100,000+

Students Supported

1 Million+

Questions answered

;