Quiz — Chapter 5: Numeric Precision and Arithmetic

Multiple Choice

1. What is the default behavior when a COMPUTE result has more decimal places than the target field?

a) The result is rounded to the nearest value b) The excess decimal digits are truncated (dropped) c) A SIZE ERROR is raised d) The compiler rejects the statement

2. In the statement MULTIPLY WS-QTY BY WS-PRICE, which field is modified?

a) WS-QTY b) WS-PRICE c) Both fields d) Neither — a GIVING clause is required

3. What happens when a SIZE ERROR occurs and ON SIZE ERROR is specified?

a) The program abends with a S0C7 b) The target field is set to zero and the SIZE ERROR code executes c) The target field retains its previous value and the SIZE ERROR code executes d) The target field receives the truncated result and the SIZE ERROR code executes

4. Why is COMP-3 preferred over COMP-2 for financial calculations?

a) COMP-3 is faster on all platforms b) COMP-3 uses less storage c) COMP-3 provides exact decimal arithmetic; COMP-2 introduces representation errors d) COMP-3 supports larger numbers

5. What does the REMAINDER clause in DIVIDE provide?

a) The fractional part of the quotient b) The difference between the dividend and the product of quotient and divisor c) The number of digits truncated d) The rounding adjustment amount

6. In Banker's Rounding (NEAREST-EVEN), what is 33.335 rounded to two decimal places?

a) 33.33 b) 33.34 c) 33.33 (rounds to nearest even digit) d) 33.34 (rounds to nearest even digit)

7. What is the maximum intermediate result precision under ARITH(COMPAT)?

a) 15 digits b) 18 digits c) 31 digits d) Unlimited

8. Which statement about ADD CORRESPONDING is TRUE?

a) It matches fields by position within the group b) It matches fields by name and adds identically-named subordinate fields c) It adds all numeric fields in both groups regardless of name d) It requires both groups to have identical structures

9. What is the recommended approach for handling rounding in a multi-step financial calculation?

a) Round each intermediate result to two decimal places b) Carry extra decimal precision in intermediates and round only the final result c) Use floating point for intermediates and round at the end d) Avoid rounding entirely and truncate

10. In the penny distribution pattern, what is done with the remainder after dividing a total evenly?

a) The remainder is discarded b) The remainder is added to the last recipient c) The remainder pennies are distributed one per recipient starting from the first d) The remainder is reported as a rounding adjustment

True or False

11. Without ON SIZE ERROR, division by zero causes a silent error where the target retains its previous value.

12. The COMPUTE statement can store its result in multiple target fields simultaneously.

13. ON SIZE ERROR detects overflow only in the final result, not in intermediate calculations.

14. Adding 0.01 ten thousand times in COMP-1 (floating point) produces exactly 100.00.

15. The ROUNDED phrase can be applied to ADD, SUBTRACT, MULTIPLY, and DIVIDE statements, not just COMPUTE.

16. SUBTRACT A FROM B GIVING C modifies both B and C.

17. Exponentiation (**) has the highest precedence among COMPUTE operators.

18. ARITH(EXTEND) allows intermediate results up to 31 digits on IBM Enterprise COBOL.

19. In the loan amortization pattern, the final payment is typically adjusted to bring the balance to exactly zero.

20. COMP-3 fields can represent decimal fractions exactly because they store each digit as a 4-bit nibble.

Short Answer

21. Explain the "six-decimal rule" for intermediate interest calculations. Why is carrying extra precision important, and when should the final rounding occur?

22. Write a COBOL code fragment that safely divides WS-TOTAL by WS-COUNT, handling both the zero-divisor case and potential overflow.

23. A program adds $0.01 to a COMP-1 field 100,000 times. Explain why the result will not be exactly $1,000.00, and calculate the approximate magnitude of the error.

24. Describe the complete penny distribution algorithm: how to divide $100.00 among 7 recipients so that the sum of all shares is exactly $100.00.

25. Why might restructuring COMPUTE R = A * B * C / D as COMPUTE R = A * (B * C / D) help avoid intermediate precision loss? Under what conditions would this matter?

Answer Key

  1. b) Excess decimal digits are truncated (dropped) by default.
  2. b) WS-PRICE is modified — in MULTIPLY A BY B, B is the target.
  3. c) The target retains its previous value, and the SIZE ERROR imperative executes.
  4. c) COMP-3 provides exact decimal arithmetic; COMP-2 has binary representation errors.
  5. b) The remainder is dividend - (quotient * divisor).
  6. d) 33.34 — the digit before 5 is 3 (odd), so it rounds up to 4 (even).
  7. b) 18 digits under ARITH(COMPAT).
  8. b) ADD CORRESPONDING matches fields by name.
  9. b) Carry extra precision in intermediates and round only the final result.
  10. c) Remainder pennies are distributed one per recipient starting from the first.
  11. False — Without ON SIZE ERROR, division by zero typically causes an abend (S0C7 on z/OS) or undefined behavior, not a silent error.
  12. True — COMPUTE can have multiple targets before the = sign.
  13. False — ON SIZE ERROR detects overflow in intermediate results as well.
  14. False — Floating-point representation error accumulates; the result is approximately 100.00393.
  15. True — ROUNDED can be used with all arithmetic verbs.
  16. FalseSUBTRACT A FROM B GIVING C modifies only C; B is unchanged.
  17. True — ** has the highest precedence, then * and /, then + and -.
  18. True — ARITH(EXTEND) enables 31-digit intermediates.
  19. True — The final payment is adjusted for accumulated rounding.
  20. True — Packed decimal stores exact decimal representations.
  21. The "six-decimal rule" carries six decimal places in intermediate interest calculations (e.g., PIC V9(6)) rather than two. This preserves precision through multi-step calculations (e.g., daily interest accumulated over 30 days). Rounding occurs only when the final result is posted to the account (PIC V99 ROUNDED). If each day's interest were rounded to two places, the maximum error over 30 days would be 30 * $0.005 = $0.15 per account.
  22. IF WS-COUNT > ZERO / DIVIDE WS-TOTAL BY WS-COUNT / GIVING WS-AVERAGE ROUNDED / ON SIZE ERROR / PERFORM HANDLE-OVERFLOW / END-DIVIDE / ELSE / MOVE ZERO TO WS-AVERAGE / END-IF
  23. COMP-1 stores 0.01 as approximately 0.009999999776..., an error of about 2.24e-10 per addition. Over 100,000 additions, the error accumulates to approximately 100,000 * 2.24e-10 = 2.24e-5, or about $0.0000224. However, the actual error is larger due to accumulated floating-point rounding at each addition step, typically resulting in $0.03-$0.05 discrepancy.
  24. (1) DIVIDE $100.00 BY 7 GIVING $14.28 REMAINDER $0.04. (2) Assign $14.28 to all 7 recipients. (3) Convert remainder to pennies: 4 pennies. (4) Add $0.01 to recipients 1-4. (5) Result: recipients 1-4 get $14.29, recipients 5-7 get $14.28. (6) Verify: 4*$14.29 + 3*$14.28 = $57.16 + $42.84 = $100.00.
  25. With A * B * C / D, the intermediate A * B * C can be very large (sum of all integer digits). If A, B, C are each 9-digit numbers, the triple product needs up to 27 integer digits, exceeding the 18-digit ARITH(COMPAT) limit. Restructuring to A * (B * C / D) computes B * C / D first, which reduces the intermediate magnitude because the division brings the value down before multiplying by A. This matters when operands have many digits and the compiler uses ARITH(COMPAT).