Chapter 7 Quiz: Conditional Logic

Test your understanding of IF statements, EVALUATE, condition names, and related concepts. Each question has one best answer. Try to answer before revealing the solution.


Question 1

What is the scope terminator for the IF statement in modern COBOL?

A) A period (.) B) END-IF C) ENDIF D) STOP IF

Answer **B) END-IF** END-IF is the explicit scope terminator introduced in COBOL-85. While a period can terminate an IF in legacy code, END-IF is the modern and strongly recommended approach. It eliminates the class of bugs caused by misplaced periods.

Question 2

What does the following condition evaluate to if WS-CODE contains "B"?

IF WS-CODE = "A" OR "B" OR "C"

A) True -- it checks if WS-CODE equals "A", "B", or "C" B) False -- "B" is not a valid boolean expression C) Compile error -- OR cannot be used this way D) True -- but only because "B" is a non-empty string (truthy)

Answer **A) True -- it checks if WS-CODE equals "A", "B", or "C"** This uses COBOL's implied subject feature. The condition is equivalent to: `IF WS-CODE = "A" OR WS-CODE = "B" OR WS-CODE = "C"`. Since WS-CODE contains "B", the condition is true.

Question 3

In a compound condition with both AND and OR (without parentheses), which operator has higher precedence?

A) OR has higher precedence B) AND has higher precedence C) They have equal precedence (evaluated left to right) D) It depends on the compiler

Answer **B) AND has higher precedence** In COBOL (as in most programming languages and Boolean algebra), AND has higher precedence than OR. The expression `A OR B AND C` is evaluated as `A OR (B AND C)`. Always use parentheses when mixing AND and OR to make your intent clear.

Question 4

What level number is used to define condition names in COBOL?

A) 66 B) 77 C) 88 D) 99

Answer **C) 88** Level-88 items define condition names. They are associated with the immediately preceding data item and specify values that give the condition a TRUE result. Level-66 is for RENAMES, level-77 is for independent elementary items, and level-99 does not exist in standard COBOL.

Question 5

What does SET ACCOUNT-ACTIVE TO TRUE do if ACCOUNT-ACTIVE is defined as 88 ACCOUNT-ACTIVE VALUE "A"?

A) Sets a boolean flag to TRUE B) Moves "A" to the parent variable of ACCOUNT-ACTIVE C) Moves "TRUE" to the parent variable D) Moves 1 to the parent variable

Answer **B) Moves "A" to the parent variable of ACCOUNT-ACTIVE** SET condition-name TO TRUE moves the first VALUE specified in the 88-level definition into the parent data item. Since ACCOUNT-ACTIVE has VALUE "A", the parent variable receives "A".

Question 6

What is the COBOL keyword for the default case in an EVALUATE statement?

A) DEFAULT B) ELSE C) OTHERWISE D) OTHER

Answer **D) OTHER** The WHEN OTHER clause matches when no other WHEN clause has matched. It is the COBOL equivalent of the DEFAULT case in C/Java switch statements. While optional, it is strongly recommended for robust code.

Question 7

Does COBOL guarantee short-circuit evaluation of compound conditions?

A) Yes, always B) Yes, but only for AND conditions C) No, COBOL does not guarantee short-circuit evaluation D) Only when the OPTIMIZE compiler option is used

Answer **C) No, COBOL does not guarantee short-circuit evaluation** The COBOL standard does not specify evaluation order for compound conditions, and most compilers evaluate all conditions. This means expressions like `IF X NOT = 0 AND (Y / X) > 5` may cause a division-by-zero error even when X is zero. Always use nested IFs for such cases.

Question 8

Which of the following is true about the NUMERIC class condition on a PIC X(5) field?

A) It returns true if the field contains "123.5" B) It returns true if the field contains "12345" C) It returns true if the field contains " 123" D) It returns true if the field contains "+1234"

Answer **B) It returns true if the field contains "12345"** For a PIC X field, NUMERIC is true only when every character is a digit (0-9). Decimal points, spaces, and signs are NOT considered numeric characters in an alphanumeric (PIC X) field. Only a field consisting entirely of digits passes the NUMERIC test.

Question 9

What does the ANY keyword mean in an EVALUATE statement?

A) Match any WHEN clause B) Match any value for this position in an ALSO clause C) Match any data type D) Skip this evaluation

Answer **B) Match any value for this position in an ALSO clause** In an EVALUATE with ALSO, the ANY keyword indicates that any value of the subject at that position will satisfy the match. It is a wildcard for one dimension of the decision matrix. For example: `WHEN "A" ALSO ANY` matches when the first subject is "A", regardless of the second subject's value.

Question 10

What is the result of this EVALUATE?

EVALUATE TRUE
    WHEN WS-X > 10
        MOVE "A" TO WS-RESULT
    WHEN WS-X > 5
        MOVE "B" TO WS-RESULT
    WHEN OTHER
        MOVE "C" TO WS-RESULT
END-EVALUATE

If WS-X contains 15, WS-RESULT will contain:

A) "A" B) "B" C) "C" D) "A" then "B" (both WHEN clauses execute)

Answer **A) "A"** EVALUATE executes only the FIRST matching WHEN clause. Since WS-X (15) is greater than 10, the first WHEN matches and "A" is moved to WS-RESULT. Even though the second WHEN (> 5) is also true, it is not executed. There is no fall-through in EVALUATE -- unlike C's switch statement.

Question 11

What is the De Morgan's equivalent of NOT (A AND B)?

A) NOT A AND NOT B B) NOT A OR NOT B C) A OR B D) NOT A AND B

Answer **B) NOT A OR NOT B** De Morgan's first law states that the negation of a conjunction is the disjunction of the negations: NOT (A AND B) = (NOT A) OR (NOT B). In COBOL terms: `NOT (WS-X > 5 AND WS-Y < 10)` is equivalent to `WS-X <= 5 OR WS-Y >= 10`.

Question 12

Which statement should you use instead of NEXT SENTENCE in modern COBOL?

A) SKIP B) PASS C) CONTINUE D) NOP

Answer **C) CONTINUE** CONTINUE is the no-operation statement that should replace NEXT SENTENCE in all modern COBOL code. NEXT SENTENCE transfers control past the next period, which can lead to confusing behavior. CONTINUE simply does nothing and proceeds to the next statement in sequence.

Question 13

Given this definition:

01  WS-SCORE PIC 9(3).
    88  GRADE-A         VALUE 90 THRU 100.
    88  GRADE-B         VALUE 80 THRU 89.
    88  PASSING-GRADE   VALUE 60 THRU 100.

If WS-SCORE is 85, which condition names are TRUE?

A) GRADE-B only B) GRADE-A and PASSING-GRADE C) GRADE-B and PASSING-GRADE D) All three

Answer **C) GRADE-B and PASSING-GRADE** When WS-SCORE is 85: GRADE-A (90-100) is false; GRADE-B (80-89) is true; PASSING-GRADE (60-100) is true. A score of 85 falls within the ranges of both GRADE-B and PASSING-GRADE. Multiple 88-level conditions can be simultaneously true for the same parent variable.

Question 14

What is the purpose of ALSO in an EVALUATE statement?

A) It adds an alternative condition to a WHEN clause B) It allows testing multiple subjects simultaneously C) It is equivalent to OR D) It provides a secondary sort for matching

Answer **B) It allows testing multiple subjects simultaneously** ALSO creates a multi-dimensional EVALUATE where multiple subjects are tested simultaneously. Each WHEN clause must include matching ALSO parts. A WHEN clause matches only when ALL its parts (connected by ALSO) match their respective subjects. This creates a decision matrix.

Question 15

What happens when this code runs?

IF WS-AMOUNT > 1000
    DISPLAY "Large order".
    DISPLAY "Processing...".

A) Both DISPLAY statements execute only when WS-AMOUNT > 1000 B) The first DISPLAY is conditional; the second always executes C) Compile error D) Neither DISPLAY executes

Answer **B) The first DISPLAY is conditional; the second always executes** The period after the first DISPLAY terminates the IF statement. The second DISPLAY is outside the IF scope and executes unconditionally. This is the classic "premature period" bug that END-IF was designed to prevent. In modern COBOL, always use END-IF and avoid periods inside conditional blocks.

Question 16

Which class condition is true for a field containing only spaces?

A) NUMERIC B) ALPHABETIC C) Both NUMERIC and ALPHABETIC D) Neither

Answer **B) ALPHABETIC** Spaces are considered ALPHABETIC (as well as ALPHABETIC-LOWER and ALPHABETIC-UPPER) but are NOT considered NUMERIC. The ALPHABETIC test accepts letters A-Z, a-z, and the space character. The NUMERIC test on a PIC X field requires every character to be a digit 0-9.

Question 17

What is the COBOL equivalent of this pseudocode?

switch(status) {
    case "A": case "B":
        process_active();
        break;
    case "C":
        process_closed();
        break;
}

A)

EVALUATE WS-STATUS
    WHEN "A"
    WHEN "B"
        PERFORM PROCESS-ACTIVE
    WHEN "C"
        PERFORM PROCESS-CLOSED
END-EVALUATE

B)

EVALUATE WS-STATUS
    WHEN "A" OR "B"
        PERFORM PROCESS-ACTIVE
    WHEN "C"
        PERFORM PROCESS-CLOSED
END-EVALUATE

C) Both A and B are valid D) Neither is valid

Answer **A)** Multiple WHEN clauses stacked together (without intervening statements) share the same statement block. Option B with `WHEN "A" OR "B"` is not standard EVALUATE syntax -- OR is not used to combine values in a single WHEN phrase. The correct way to match multiple values in EVALUATE is to stack separate WHEN clauses.

Question 18

If an 88-level has multiple values defined, what value does SET move to the parent variable?

01  WS-TYPE PIC X(2).
    88  CREDIT-TRANS    VALUE "DP" "TR" "RF".

After SET CREDIT-TRANS TO TRUE, WS-TYPE contains:

A) "DP" (the first value) B) "TR" (the middle value) C) "RF" (the last value) D) All three values are set

Answer **A) "DP" (the first value)** When SET is used with a condition name that has multiple values, the FIRST value listed in the VALUE clause is moved to the parent variable. In this case, "DP" is moved to WS-TYPE.

Question 19

What is NOT a valid COBOL relational operator?

A) IS GREATER THAN B) IS NOT LESS THAN C) IS EQUAL OR GREATER THAN D) IS GREATER THAN OR EQUAL TO

Answer **C) IS EQUAL OR GREATER THAN** The correct COBOL word form is "IS GREATER THAN OR EQUAL TO" (option D), not "IS EQUAL OR GREATER THAN." Options A and B are valid COBOL relational operators. "IS NOT LESS THAN" is equivalent to >=.

Question 20

In EVALUATE TRUE ALSO TRUE, what does the second TRUE represent?

A) A redundant keyword (ignored by the compiler) B) The second dimension of the evaluation matrix C) A condition that must be true for the EVALUATE to execute D) The default value for WHEN OTHER

Answer **B) The second dimension of the evaluation matrix** Each TRUE in `EVALUATE TRUE ALSO TRUE` represents one dimension (subject) of the evaluation. The first TRUE is tested against the first WHEN condition, the second TRUE against the ALSO condition. This creates a two-dimensional decision matrix where each WHEN clause specifies conditions for both dimensions.

Question 21

What is the sign condition test for a value that is either positive or zero?

A) IS POSITIVE B) IS NOT NEGATIVE C) IS ZERO OR POSITIVE D) IS NOT LESS THAN ZERO

Answer **B) IS NOT NEGATIVE** IS NOT NEGATIVE is true when the value is zero or positive (>= 0). IS POSITIVE is true only for values strictly greater than zero. Option C is not valid COBOL syntax. Option D is a relation condition (not a sign condition), though it would produce the same logical result.

Question 22

What does this user-defined class allow?

SPECIAL-NAMES.
    CLASS VALID-ID IS "A" THRU "Z" "0" THRU "9" "-".

A) Any alphabetic or numeric character B) Only uppercase letters, digits, or hyphens C) Any character that is a valid identifier in COBOL D) Letters A through Z9 and the hyphen

Answer **B) Only uppercase letters, digits, or hyphens** The CLASS definition specifies the exact characters allowed: uppercase letters A through Z (a range), digits 0 through 9 (a range), and the literal hyphen character. Lowercase letters are NOT included. When tested with `IS VALID-ID`, each character in the field must be one of these allowed characters.

Question 23

Why is the following code dangerous?

IF WS-DENOMINATOR > 0
    AND WS-NUMERATOR / WS-DENOMINATOR > 5
    PERFORM PROCESS-RATIO
END-IF

A) The division might produce a decimal result B) COBOL may evaluate the division even when WS-DENOMINATOR is 0 C) AND conditions must be in separate IF statements D) The comparison > 5 cannot be used with a computed result

Answer **B) COBOL may evaluate the division even when WS-DENOMINATOR is 0** COBOL does not guarantee short-circuit evaluation. Even though the first condition (WS-DENOMINATOR > 0) would be false when the denominator is zero, COBOL may still evaluate the division in the second condition, causing a divide-by-zero runtime error. The safe approach is to use nested IFs.

Question 24

What is the practical advantage of condition names over literal comparisons?

A) They execute faster B) They use less memory C) They make code self-documenting by replacing magic values with meaningful names D) They are required by the COBOL standard

Answer **C) They make code self-documenting by replacing magic values with meaningful names** The primary advantage of condition names is readability and maintainability. `IF ACCOUNT-ACTIVE` is immediately understandable, while `IF WS-STATUS = "A"` requires knowing what "A" means. Condition names do not affect performance or memory usage (they occupy no storage). They are optional, not required.

Question 25

When using EVALUATE, what happens if no WHEN clause matches and there is no WHEN OTHER?

A) A runtime error occurs B) The program terminates abnormally C) Control passes to the statement after END-EVALUATE (no action taken) D) The first WHEN clause is executed by default

Answer **C) Control passes to the statement after END-EVALUATE (no action taken)** If no WHEN clause matches and there is no WHEN OTHER, the EVALUATE statement simply does nothing and execution continues with the next statement after END-EVALUATE. No error occurs. However, WHEN OTHER is recommended as a safety net to handle unexpected values.

Question 26

Which of the following correctly defines overlapping condition name ranges?

01  WS-AGE  PIC 9(3).
    88  CHILD           VALUE 0 THRU 12.
    88  TEENAGER        VALUE 13 THRU 19.
    88  ADULT           VALUE 20 THRU 64.
    88  SENIOR          VALUE 65 THRU 120.
    88  MINOR           VALUE 0 THRU 17.
    88  WORKING-AGE     VALUE 16 THRU 64.

For WS-AGE = 16, which condition names are TRUE?

A) TEENAGER only B) TEENAGER and MINOR C) TEENAGER, MINOR, and WORKING-AGE D) TEENAGER and WORKING-AGE

Answer **C) TEENAGER, MINOR, and WORKING-AGE** Age 16 falls within: TEENAGER (13-19), MINOR (0-17), and WORKING-AGE (16-64). Overlapping 88-level ranges are perfectly legal and useful -- they allow the same value to satisfy multiple named conditions, representing different categorization perspectives.

Question 27

What is the difference between EVALUATE TRUE and EVALUATE FALSE?

A) EVALUATE TRUE matches the first true condition; EVALUATE FALSE matches the first false condition B) EVALUATE TRUE uses positive logic; EVALUATE FALSE is invalid syntax C) They produce the same results D) EVALUATE FALSE evaluates conditions in reverse order

Answer **A) EVALUATE TRUE matches the first true condition; EVALUATE FALSE matches the first false condition** EVALUATE TRUE selects the first WHEN clause whose condition evaluates to TRUE. EVALUATE FALSE selects the first WHEN clause whose condition evaluates to FALSE. EVALUATE FALSE is valid but uncommon -- most programmers prefer EVALUATE TRUE with negated conditions for clarity.

Question 28

Which approach is recommended when you need more than three levels of nested IF?

A) Add more levels of nesting with careful indentation B) Use GOTO to jump to the correct logic C) Refactor to use EVALUATE or separate paragraphs D) Use NEXT SENTENCE to simplify the nesting

Answer **C) Refactor to use EVALUATE or separate paragraphs** Deeply nested IFs (more than three levels) become difficult to read and maintain. The recommended approaches are: (1) convert to EVALUATE TRUE or EVALUATE TRUE ALSO TRUE, (2) break the logic into separate paragraphs invoked with PERFORM, or (3) restructure the logic using a different algorithm. GOTO and NEXT SENTENCE are deprecated practices.

Question 29

What does this condition test?

IF WS-DATA IS NOT ALPHABETIC-UPPER

A) The field contains at least one lowercase letter B) The field contains characters that are not uppercase letters or spaces C) The field is not in the correct case D) The field is empty

Answer **B) The field contains characters that are not uppercase letters or spaces** ALPHABETIC-UPPER is true when every character in the field is an uppercase letter (A-Z) or a space. NOT ALPHABETIC-UPPER is true when at least one character is something else -- it could be a lowercase letter, digit, punctuation, or any other non-uppercase-letter character.

Question 30

In a production COBOL system, what is the best way to implement a business rule like "Premium customers who have been active for 5+ years and have no overdue balance get a 10% discount"?

A)

IF WS-CUST-TYPE = "P" AND WS-YEARS >= 5
    AND WS-OVERDUE = "N"
    COMPUTE WS-DISCOUNT = WS-AMOUNT * 0.10
END-IF

B)

IF PREMIUM-CUSTOMER AND LONG-TENURE
    AND NOT-OVERDUE
    COMPUTE WS-DISCOUNT = WS-AMOUNT * 0.10
END-IF

C) Both are correct, but B is preferred D) Neither is correct; use EVALUATE instead

Answer **C) Both are correct, but B is preferred** Both versions produce identical results, but version B using condition names is strongly preferred in production COBOL. It is self-documenting: any reader can understand the business rule without knowing the underlying data codes. The condition names (PREMIUM-CUSTOMER, LONG-TENURE, NOT-OVERDUE) capture business meaning and make the code read like the original requirement.