Quiz: Intrinsic Functions

Multiple Choice

1. What keyword must precede all intrinsic function names (unless FUNCTION ALL INTRINSIC is declared)?

a) CALL b) INVOKE c) FUNCTION d) COMPUTE

2. What does FUNCTION LENGTH(WS-NAME) return when WS-NAME is PIC X(30) VALUE "HELLO"?

a) 5 b) 25 c) 30 d) 6

3. What is the difference between FUNCTION INTEGER(-3.7) and FUNCTION INTEGER-PART(-3.7)?

a) Both return -3 b) INTEGER returns -4, INTEGER-PART returns -3 c) INTEGER returns -3, INTEGER-PART returns -4 d) Both return -4

4. What happens if you call FUNCTION NUMVAL("ABC123")?

a) Returns 123 b) Returns zero c) Causes a compile error d) Causes a runtime abend

5. Which function returns the ordinal position of the maximum value among its arguments?

a) MAX b) ORD-MAX c) POSITION-MAX d) INDEX-MAX

6. What does FUNCTION ANNUITY(rate, periods) return?

a) The total amount of an annuity b) The interest earned per period c) The ratio to multiply by principal to get periodic payment d) The present value of all payments

7. Which function was added in COBOL 2002 to safely test if a string is valid for NUMVAL conversion?

a) VALIDATE-NUMVAL b) CHECK-NUMVAL c) TEST-NUMVAL d) IS-NUMERIC

8. What is the result of FUNCTION REVERSE("ABC ") (where there are 2 trailing spaces)?

a) "CBA" b) "CBA " c) " CBA" d) " ABC"

9. FUNCTION CURRENT-DATE returns a string of how many characters?

a) 8 b) 14 c) 21 d) 26

10. How do you compute the number of days between two dates using intrinsic functions?

a) FUNCTION DATE-DIFF(date1, date2) b) FUNCTION DAYS-BETWEEN(date1, date2) c) FUNCTION INTEGER-OF-DATE(date2) - FUNCTION INTEGER-OF-DATE(date1) d) FUNCTION DAY-COUNT(date1, date2)

True or False

11. FUNCTION MEAN can accept a variable number of arguments.

12. FUNCTION SQRT will return zero if given a negative argument.

13. The statistical functions (MEAN, MEDIAN, etc.) can use the ALL subscript to process all elements of a table.

14. FUNCTION PRESENT-VALUE requires all future cash flows to be equal amounts.

15. FUNCTION UPPER-CASE modifies numeric digits in the input string.

Short Answer

16. Explain why FUNCTION MEAN(WS-AMOUNT(ALL)) can produce incorrect results on a partially loaded table. What is the correct approach?

17. Write a COBOL statement that safely converts the alphanumeric field WS-INPUT-AMT to a numeric value, handling invalid input without abending.

18. Describe a business scenario where FUNCTION ORD-MAX would be more useful than FUNCTION MAX.

19. How would you use intrinsic functions to determine whether a given date (PIC 9(8)) falls on a weekend? (Hint: Consider what day-of-week information you can derive from INTEGER-OF-DATE.)

20. Explain the theme "Legacy != Obsolete" as it applies to COBOL's intrinsic functions.

Answer Key

  1. c — The FUNCTION keyword is required before all intrinsic function names.
  2. c — FUNCTION LENGTH returns the allocated length (30), not the content length.
  3. b — INTEGER returns -4 (floor, toward negative infinity); INTEGER-PART returns -3 (truncation toward zero).
  4. d — NUMVAL causes a runtime abend on non-numeric input. Use TEST-NUMVAL first.
  5. b — ORD-MAX returns the ordinal position (1-based) of the maximum value.
  6. c — ANNUITY returns the ratio; multiply by the principal to get the periodic payment amount.
  7. c — TEST-NUMVAL returns 0 if the string is valid for NUMVAL conversion, non-zero otherwise.
  8. c — REVERSE operates on the full field including trailing spaces, producing " CBA".
  9. c — CURRENT-DATE returns 21 characters: 8 date + 8 time + 5 timezone offset.
  10. c — Convert both dates to integer day numbers and subtract.
  11. True — MEAN accepts one or more numeric arguments.
  12. False — FUNCTION SQRT with a negative argument causes a runtime error, not a zero result.
  13. True — The ALL subscript passes all table elements. But beware of partially loaded tables.
  14. False — PRESENT-VALUE accepts individual amounts for each period; they can differ.
  15. False — UPPER-CASE only affects alphabetic characters; digits and special characters are unchanged.
  16. FUNCTION MEAN(WS-AMOUNT(ALL)) divides by the declared OCCURS count (e.g., 100), not the logical count. If only 50 entries are loaded, the 50 uninitialized entries (likely zero) dilute the mean. The correct approach is to compute the sum manually using a loop up to the logical count, then divide by that count.
  17. IF FUNCTION TEST-NUMVAL(WS-INPUT-AMT) = ZERO then COMPUTE WS-AMOUNT = FUNCTION NUMVAL(WS-INPUT-AMT) else handle the error. For currency: use TEST-NUMVAL-C and NUMVAL-C with the appropriate currency symbol.
  18. When you need to identify WHICH item has the maximum value, not just the value itself. For example, determining which of 10 branches had the highest revenue — ORD-MAX returns the branch position (e.g., 7), which you can then use to access the branch name and other details from the table.
  19. Compute FUNCTION INTEGER-OF-DATE(ws-date) and then FUNCTION MOD(integer-result, 7). The result maps to a day of the week. You need to determine which MOD values correspond to Saturday and Sunday by testing with a known date (e.g., January 1, 2024 was a Monday). If MOD returns 0 for Monday, then 5=Saturday and 6=Sunday.
  20. COBOL's intrinsic functions demonstrate that the language has evolved significantly from its 1960s origins. Functions like MEAN, STANDARD-DEVIATION, PRESENT-VALUE, and ANNUITY provide built-in capabilities that many "modern" languages lack without external libraries. The COBOL 2002 and 2014 standards added TRIM, TEST-NUMVAL, CONCATENATE, and more. The language is not frozen in time — it is actively maintained and enhanced for its core domain of business computing.