Chapter 6 Quiz: Arithmetic Operations and Numeric Processing

Test your understanding of COBOL arithmetic operations. Each question has one correct answer. Try to answer without looking at the chapter text first, then check your answers.


Question 1

What is the result stored in WS-B after executing the following?

       MOVE 100 TO WS-A
       MOVE 250 TO WS-B
       ADD WS-A TO WS-B

a) 100 b) 250 c) 350 d) 150

Answer **c) 350** `ADD WS-A TO WS-B` adds the value of WS-A (100) to WS-B (250) and stores the result in WS-B. WS-A remains unchanged at 100.

Question 2

In the statement ADD WS-X WS-Y GIVING WS-Z, which fields are modified?

a) WS-X and WS-Y b) WS-X, WS-Y, and WS-Z c) WS-Z only d) WS-Y and WS-Z

Answer **c) WS-Z only** The GIVING phrase preserves all operands before GIVING. Only the identifier after GIVING receives the result. Both WS-X and WS-Y remain unchanged.

Question 3

What does DIVIDE 5 INTO WS-TOTAL compute?

a) WS-TOTAL = 5 / WS-TOTAL b) WS-TOTAL = WS-TOTAL / 5 c) WS-TOTAL = WS-TOTAL * 5 d) WS-TOTAL = 5 - WS-TOTAL

Answer **b) WS-TOTAL = WS-TOTAL / 5** "DIVIDE a INTO b" means b = b / a. Think of it as "divide 5 into the total"---the total is being divided by 5. The result replaces WS-TOTAL.

Question 4

What is the result of COMPUTE WS-R = 2 + 3 * 4?

a) 20 b) 14 c) 24 d) 12

Answer **b) 14** Operator precedence: multiplication before addition. First `3 * 4 = 12`, then `2 + 12 = 14`. To get 20, you would need parentheses: `(2 + 3) * 4`.

Question 5

Which of the following correctly computes the square root of WS-VALUE?

a) COMPUTE WS-RESULT = WS-VALUE ** 2 b) COMPUTE WS-RESULT = WS-VALUE ** 0.5 c) COMPUTE WS-RESULT = WS-VALUE / 2 d) COMPUTE WS-RESULT = WS-VALUE ** -2

Answer **b) `COMPUTE WS-RESULT = WS-VALUE ** 0.5`** Raising a value to the power of 0.5 computes the square root. `** 2` computes the square, not the square root.

Question 6

What happens when a size error occurs and ON SIZE ERROR is NOT specified?

a) The program terminates with an error b) The result field is set to zero c) The result is undefined (unpredictable) d) A runtime warning is displayed

Answer **c) The result is undefined (unpredictable)** Without ON SIZE ERROR, an overflow produces undefined results. The receiving field may contain truncated digits, partial results, or garbage. The program continues execution with potentially incorrect data---a dangerous situation in financial systems.

Question 7

Given PIC 9(5)V99, what is the result of COMPUTE WS-RESULT = 10 / 3 (without ROUNDED)?

a) 3.33 b) 3.34 c) 3.00 d) 0.33

Answer **a) 3.33** Without ROUNDED, COBOL truncates (drops) excess decimal places. The true result 3.333... is truncated to 3.33 in a PIC V99 field. The third and subsequent decimal digits are simply discarded.

Question 8

Which data type is recommended for storing monetary values in COBOL?

a) COMP (binary) b) COMP-1 (single-precision float) c) COMP-3 (packed decimal) d) DISPLAY (zoned decimal)

Answer **c) COMP-3 (packed decimal)** COMP-3 provides exact decimal representation (no floating-point approximation), efficient storage (two digits per byte), and hardware-accelerated arithmetic on mainframes. It is the industry standard for financial data in COBOL systems.

Question 9

What is the REMAINDER after DIVIDE 1000 BY 7 GIVING WS-Q REMAINDER WS-R?

a) 142 b) 6 c) 7 d) 143

Answer **b) 6** 1000 / 7 = 142 with remainder 6. Verification: 142 * 7 = 994, and 1000 - 994 = 6.

Question 10

Which of the following is the correct operator precedence in COBOL COMPUTE (highest to lowest)?

a) + - > * / > ** b) ** > * / > + - c) * / > ** > + - d) All operators have equal precedence (left to right)

Answer **b) `**` > `* /` > `+ -`** Exponentiation has the highest precedence, followed by multiplication/division, then addition/subtraction. Within the same precedence level, evaluation is left to right. Parentheses override all precedence rules.

Question 11

What does ADD CORRESPONDING do?

a) Adds fields in the order they appear in each group b) Adds only fields that have matching names in both group items c) Adds all numeric fields in the first group to all numeric fields in the second group d) Adds a single field to every field in a group

Answer **b) Adds only fields that have matching names in both group items** ADD CORRESPONDING (ADD CORR) matches elementary items by name between two group items. Only items with identical names in both groups are processed. Non-matching items are ignored. Both operands must be group items.

Question 12

In the statement MULTIPLY WS-RATE BY WS-AMOUNT, which field receives the result?

a) WS-RATE b) WS-AMOUNT c) Both fields d) Neither (a GIVING clause is required)

Answer **b) WS-AMOUNT** In `MULTIPLY a BY b`, the result replaces the identifier after BY. WS-RATE remains unchanged. This can be counterintuitive---the word "BY" indicates the second operand is the destination.

Question 13

What is the storage size of a PIC S9(9)V99 COMP-3 field?

a) 11 bytes b) 6 bytes c) 4 bytes d) 8 bytes

Answer **b) 6 bytes** COMP-3 stores two digits per byte, with the last nibble reserved for the sign. For S9(9)V99 = 11 digits total. Formula: CEIL((11 + 1) / 2) = CEIL(6) = 6 bytes. Compare this to the same field in DISPLAY format, which would require 11 bytes.

Question 14

Which scenario triggers an ON SIZE ERROR?

a) Adding two positive numbers that fit in the result field b) Dividing by zero c) Subtracting zero from a number d) Multiplying by one

Answer **b) Dividing by zero** Division by zero always triggers a size error. The other scenarios produce valid results that fit in the receiving fields and therefore do not trigger size errors.

Question 15

What is the difference between DIVIDE A BY B GIVING C and DIVIDE A INTO B GIVING C?

a) They produce the same result b) BY computes A/B; INTO computes B/A c) BY requires ROUNDED; INTO does not d) INTO only works with integers

Answer **b) BY computes A/B; INTO computes B/A** `DIVIDE A BY B GIVING C` means C = A / B. `DIVIDE A INTO B GIVING C` means C = B / A. The dividend and divisor positions are swapped. This is one of the most commonly confused aspects of COBOL division syntax.

Question 16

If WS-RESULT is defined as PIC 9(3)V99, and you execute COMPUTE WS-RESULT ROUNDED = 2 / 3, what value is stored?

a) 0.66 b) 0.67 c) 0.60 d) 1.00

Answer **b) 0.67** 2 / 3 = 0.66666... The third decimal digit (6) is >= 5, so ROUNDED increases the second decimal from 6 to 7. Without ROUNDED, the result would be 0.66 (truncated).

Question 17

Why should you avoid COMP-1 and COMP-2 for financial calculations?

a) They are slower than COMP-3 b) They use too much storage c) They cannot represent decimal values exactly d) They are not supported by modern compilers

Answer **c) They cannot represent decimal values exactly** COMP-1 (single-precision float) and COMP-2 (double-precision float) use binary floating-point representation, which cannot exactly represent many decimal fractions (like 0.10). This introduces rounding errors that accumulate across many transactions---unacceptable for financial systems.

Question 18

What is "banker's rounding" (NEAREST-EVEN)?

a) Always round up b) Always round down c) Round to nearest even digit when exactly at the midpoint d) Round away from zero when exactly at the midpoint

Answer **c) Round to nearest even digit when exactly at the midpoint** Banker's rounding (NEAREST-EVEN) eliminates the systematic upward bias of standard half-up rounding. When the discarded portion is exactly 5 (halfway), it rounds to the nearest even digit. For example: 2.5 rounds to 2, 3.5 rounds to 4. Over many transactions, this method averages to zero bias.

Question 19

What best practice should you follow when designing result fields for a MULTIPLY operation?

a) Make the result field the same size as the larger operand b) The result needs the sum of the operands' digit counts c) Always use PIC 9(18)V99 to be safe d) The result should have fewer decimal places than the operands

Answer **b) The result needs the sum of the operands' digit counts** When multiplying PIC 9(m)V9(p) by PIC 9(n)V9(q), the full-precision result requires PIC 9(m+n)V9(p+q). For example, multiplying PIC 9(5)V99 by PIC 9(3)V99 could produce a result needing up to PIC 9(8)V9(4).

Question 20

Which statement will NOT compile correctly?

a) ADD 10 TO WS-A WS-B WS-C b) SUBTRACT 5 FROM WS-A WS-B c) MULTIPLY 2 BY WS-A WS-B d) COMPUTE WS-A = WS-B + WS-C

Answer **c) `MULTIPLY 2 BY WS-A WS-B`** The MULTIPLY...BY form (without GIVING) only allows a single identifier after BY. You cannot multiply into multiple targets without GIVING. The correct form would be: `MULTIPLY 2 BY WS-A GIVING WS-A WS-B` (stores the same result in both). ADD and SUBTRACT allow multiple targets with TO/FROM.

Question 21

What is the purpose of the scope terminator END-COMPUTE?

a) It is always required after COMPUTE b) It marks the end of ON SIZE ERROR handling c) It improves program performance d) It is only used in free-format COBOL

Answer **b) It marks the end of ON SIZE ERROR handling** Scope terminators (END-ADD, END-SUBTRACT, END-MULTIPLY, END-DIVIDE, END-COMPUTE) are required when using ON SIZE ERROR or NOT ON SIZE ERROR phrases to delimit the scope of the conditional imperative statements. Without SIZE ERROR handling, the scope terminator is optional but recommended for clarity.

Question 22

Given the following fields and calculation, what problem might occur?

       01  WS-BALANCE  PIC 9(7)V99  VALUE 500.00.
       01  WS-WITHDRAW PIC 9(5)V99  VALUE 750.00.

       SUBTRACT WS-WITHDRAW FROM WS-BALANCE

a) WS-BALANCE becomes 250.00 b) A size error occurs because the result (-250.00) is negative and the field is unsigned c) WS-BALANCE becomes 000.00 d) The program terminates

Answer **b) A size error occurs because the result (-250.00) is negative and the field is unsigned** WS-BALANCE is PIC 9(7)V99 (no S prefix), making it unsigned. Storing a negative result in an unsigned field is a size error. Without ON SIZE ERROR, the result is undefined. The fix is to use PIC S9(7)V99 or to check values before subtracting.

Question 23

What does "decimal alignment" mean in COBOL arithmetic?

a) All fields must have the same number of decimal places b) COBOL automatically aligns decimal points before performing arithmetic c) You must manually pad decimal places with zeros d) Only COMP-3 fields support decimal alignment

Answer **b) COBOL automatically aligns decimal points before performing arithmetic** COBOL automatically aligns decimal points when performing arithmetic between fields with different PIC clauses. A PIC 9(3)V9 field and a PIC 9(2)V9(4) field are aligned as if they were 123.4000 and 56.7890 before the operation is performed. This is a key feature that prevents manual alignment errors.

Question 24

Why is it better to multiply before dividing in a COMPUTE expression?

a) It executes faster b) It avoids division by zero c) It reduces precision loss from intermediate results d) It is required by the COBOL standard

Answer **c) It reduces precision loss from intermediate results** Dividing first can produce a repeating decimal in the intermediate result, losing precision. Multiplying first preserves larger integer values in intermediate results, which are then divided. For example: `100 * 7 / 3 = 700/3 = 233.33` is more precise than `100 / 3 * 7 = 33.33 * 7 = 233.31`.

Question 25

In a financial system processing millions of transactions daily, what is the accumulated error of standard half-up rounding compared to banker's rounding?

a) Half-up and banker's rounding produce identical accumulated errors b) Half-up rounding introduces a systematic upward bias; banker's rounding averages to zero c) Banker's rounding introduces a systematic downward bias d) Both methods are equally inaccurate but in opposite directions

Answer **b) Half-up rounding introduces a systematic upward bias; banker's rounding averages to zero** Standard half-up rounding always rounds 5 upward, creating an average bias of +0.5 per rounding event. Over millions of transactions, this accumulates into significant systematic overcharges. Banker's rounding (NEAREST-EVEN) rounds 5 to the nearest even digit, rounding up half the time and down half the time, averaging to zero bias.

Scoring Guide

Score Rating
23-25 Excellent -- You have a thorough understanding of COBOL arithmetic
20-22 Good -- Strong foundation with minor gaps to address
17-19 Satisfactory -- Review the topics you missed before proceeding
14-16 Needs Improvement -- Re-read the relevant sections and practice with examples
Below 14 Re-study -- Work through the chapter examples before retaking the quiz