Quiz: Pointer and Reference Modification

Multiple Choice

1. What is the correct syntax for reference modification?

a) identifier[start:length] b) identifier(start:length) c) identifier{start,length} d) SUBSTRING(identifier, start, length)

2. In WS-FIELD(5:3), positions 5, 6, and 7 are referenced. What is the starting position of the first byte?

a) 0 b) 1 c) 5 d) Depends on the data type

3. What happens if you omit the length in reference modification, as in WS-FIELD(5:)?

a) A compile error occurs b) A runtime error occurs c) The substring extends from position 5 to the end of the item d) One byte at position 5 is referenced

4. What does LENGTH OF WS-RECORD return?

a) The number of non-space characters b) The number of bytes allocated to the data item c) The number of characters including trailing spaces d) Both b and c

5. Which compile option enables runtime boundary checking for reference modification on IBM Enterprise COBOL?

a) BOUNDS b) SSRANGE c) REFCHECK d) RANGECHECK

6. What is the value of FUNCTION LENGTH("HELLO")?

a) 4 b) 5 c) 6 d) Undefined for literals

7. In the pointer-based output building pattern, the pointer variable should be initialized to:

a) 0 b) 1 c) The length of the output field d) The length of the first piece to be appended

8. Which statement correctly sets a pointer to the address of a working-storage item?

a) MOVE ADDRESS OF WS-ITEM TO WS-PTR b) SET WS-PTR TO ADDRESS OF WS-ITEM c) COMPUTE WS-PTR = ADDRESS OF WS-ITEM d) SET ADDRESS OF WS-ITEM TO WS-PTR

9. What is the key advantage of using reference modification over UNSTRING for parsing?

a) Reference modification is always faster b) Reference modification can handle complex delimiter patterns like quoted fields c) UNSTRING cannot parse delimited data d) Reference modification works with numeric fields

10. What does the WITH POINTER phrase do in a STRING statement?

a) Sets the pointer to the beginning of the receiving field b) Maintains the current position in the receiving field across operations c) Points to the source field d) Counts the number of delimiters found

True or False

11. Reference modification can be applied to numeric (PIC 9) items.

12. The starting position in reference modification can be an arithmetic expression.

13. LENGTH OF always returns a compile-time constant.

14. You can use MOVE with index names the same way you use MOVE with pointer data items.

15. Reference modification generates runtime boundary checks by default on most compilers.

Short Answer

16. Given WS-DATA PIC X(50) VALUE "ACCOUNT:12345:SAVINGS:ACTIVE", write the reference modification expression to extract "12345" assuming you have already determined that the first colon is at position 9 and the second colon is at position 14.

17. Explain why the "pointer build pattern" (maintaining a position variable and appending via reference modification) is sometimes preferred over STRING...DELIMITED BY for constructing output lines.

18. Describe two defensive programming practices essential when using reference modification with dynamic (variable) positions and lengths.

19. What is the difference between ADDRESS OF and LENGTH OF in terms of when they can be evaluated (compile time vs. runtime)?

20. A program uses WS-RECORD(WS-POS:WS-LEN) where WS-POS = 48 and WS-LEN = 10, but WS-RECORD is only PIC X(50). What will happen, and how should this be prevented?

Answer Key

  1. b — COBOL uses parentheses with a colon separator: identifier(start:length).
  2. c — The starting position is 5; COBOL uses 1-based positioning.
  3. c — When length is omitted, the substring extends from the starting position to the end of the data item.
  4. d — LENGTH OF returns the allocated byte length, which equals the total character length including trailing spaces.
  5. b — SSRANGE enables runtime boundary checking for subscripts and reference modification.
  6. b — FUNCTION LENGTH returns 5 for the literal "HELLO".
  7. b — The pointer starts at 1 because COBOL positions are 1-based.
  8. b — SET is the correct statement for pointer assignment: SET WS-PTR TO ADDRESS OF WS-ITEM.
  9. b — Reference modification gives you character-by-character control, enabling parsing of complex patterns like quoted CSV fields that UNSTRING handles poorly.
  10. b — WITH POINTER maintains the current position in the receiving field, allowing multiple STRING operations to append sequentially.
  11. False — Reference modification applies to alphanumeric, alphabetic, and national items. For numeric items, it depends on the compiler, and the result is treated as alphanumeric.
  12. True — Both starting position and length can be arithmetic expressions.
  13. False — LENGTH OF is evaluated at compile time for fixed-length items but at runtime for items with OCCURS DEPENDING ON.
  14. False — Index names must use SET, not MOVE. Pointer data items also typically use SET.
  15. False — Most compilers do NOT generate boundary checks by default; you must enable them with options like SSRANGE.
  16. WS-DATA(9:5) — Start at position 9 (after the first colon), length 5 (positions 9-13 = "12345"). Alternatively, computed: start = first-colon-pos + 1 = 9, length = second-colon-pos - first-colon-pos - 1 = 14 - 9 - 1 = 4... actually WS-DATA(9:5). The value 12345 spans positions 9-13, so length = 14 - 9 = 5.
  17. The pointer build pattern provides exact control over positioning without delimiter scanning overhead. STRING must search for delimiters in each source operand. For building fixed-format output (reports, file records) where you know exact positions and lengths, reference modification with a pointer is more efficient and produces more predictable results. It also handles right-alignment and padding more naturally.
  18. (1) Always validate that start-position >= 1 and start-position + length - 1 <= LENGTH OF the data item before any reference modification operation. (2) Enable the compiler's boundary checking option (SSRANGE) during all development and testing. Other valid answers: validate that length > 0, test with edge-case inputs (position 1, last position, maximum length).
  19. LENGTH OF is evaluated at compile time for fixed-length items (the compiler simply substitutes the known size) but at runtime for variable-length items (OCCURS DEPENDING ON). ADDRESS OF is always evaluated at runtime because memory addresses are determined during program execution, not compilation.
  20. Position 48 + length 10 - 1 = position 57, which exceeds the 50-byte field. This causes a storage overlay — the program reads 7 bytes beyond WS-RECORD, potentially corrupting data or causing an abend. Prevention: validate WS-POS + WS-LEN - 1 <= LENGTH OF WS-RECORD before the reference modification, or enable SSRANGE.