Quiz — Chapter 17: String Handling

Multiple Choice

1. What does DELIMITED BY SPACE mean in a STRING statement?

a) The sending field is padded with spaces b) Characters are used up to (but not including) the first space c) All spaces are removed from the sending field d) A space is inserted between each sending field

2. What must you do with the POINTER variable before using it with STRING?

a) Declare it as COMP-3 b) Initialize it (typically to 1) c) Nothing — STRING initializes it automatically d) Set it to the length of the receiving field

3. Which UNSTRING clause tells you which delimiter was actually found between fields?

a) COUNT IN b) TALLYING IN c) DELIMITER IN d) FOUND IN

4. What does DELIMITED BY ALL SPACES do differently from DELIMITED BY SPACE in UNSTRING?

a) Nothing — they are identical b) ALL SPACES treats consecutive spaces as a single delimiter c) ALL SPACES removes all spaces from the field d) ALL SPACES requires the entire field to be spaces

5. In reference modification WS-DATA(5:3), what do 5 and 3 represent?

a) Line 5, column 3 b) Starting position 5, length 3 characters c) Array index 5, occurrence 3 d) Offset from end 5, length 3

6. Which INSPECT verb counts the number of occurrences of a character?

a) INSPECT COUNTING b) INSPECT TALLYING c) INSPECT REPLACING d) INSPECT SEARCHING

7. What does INSPECT WS-DATA CONVERTING 'abc' TO '123' do?

a) Replaces the string "abc" with "123" b) Replaces each 'a' with '1', each 'b' with '2', each 'c' with '3' c) Converts the first 3 characters to '123' d) Adds the numeric values of a, b, c

8. What happens when a STRING operation overflows and ON OVERFLOW is not specified?

a) A runtime error occurs b) The program ABENDs c) The operation completes silently with truncated data d) An exception is raised

9. In UNSTRING, what does the TALLYING IN clause count?

a) The total number of characters parsed b) The number of delimiters found c) The number of receiving fields that were populated d) The number of overflow characters

10. Which approach is typically fastest for high-volume string operations in COBOL?

a) STRING with DELIMITED BY SIZE b) UNSTRING with multiple fields c) MOVE with reference modification d) INSPECT CONVERTING

True or False

11. INSPECT CONVERTING requires the FROM and TO strings to be the same length.

12. The UNSTRING TALLYING IN variable is automatically reset to zero before each UNSTRING.

13. Reference modification uses 0-based indexing (the first character is position 0).

14. STRING can concatenate more than two sending fields in a single statement.

15. INSPECT can perform both TALLYING and REPLACING in a single statement.

Short Answer

16. Explain why DELIMITED BY SPACE can cause problems when concatenating a city name like "NEW YORK". What alternatives exist?

17. Describe how you would parse a CSV field that contains embedded commas within quotes, using only standard COBOL string verbs.

18. What is the difference between INSPECT REPLACING ALL and INSPECT REPLACING FIRST? Give an example where each is appropriate.

19. Maria Chen says: "Use STRING for readability in low-volume paths. Use MOVE with reference modification for performance in high-volume inner loops." Explain the trade-off she is describing.

20. How would you use INSPECT TALLYING to validate that a field contains only numeric characters?


Answer Key

1. b) Characters are used up to (but not including) the first space

2. b) Initialize it (typically to 1)

3. c) DELIMITER IN

4. b) ALL SPACES treats consecutive spaces as a single delimiter

5. b) Starting position 5, length 3 characters

6. b) INSPECT TALLYING

7. b) Replaces each 'a' with '1', each 'b' with '2', each 'c' with '3'

8. c) The operation completes silently with truncated data

9. c) The number of receiving fields that were populated

10. c) MOVE with reference modification

11. True — the FROM and TO strings must have the same length for the character-by-character mapping to work.

12. False — TALLYING IN adds to the existing value. You must initialize it to zero before each UNSTRING.

13. False — reference modification uses 1-based indexing. The first character is position 1.

14. True — STRING can concatenate any number of sending fields, each with its own DELIMITED BY clause.

15. True — a single INSPECT statement can perform both TALLYING and REPLACING operations.

16. DELIMITED BY SPACE stops at the first space, so "NEW YORK" would be truncated to "NEW". Alternatives: (1) Use DELIMITED BY SIZE to include the full field including trailing spaces, then trim separately. (2) Use a different delimiter character (like '|' or tab). (3) Use reference modification to access the exact substring needed. (4) Store the actual length and use DELIMITED BY SIZE with the trimmed field.

17. Pre-process the record: scan character by character using reference modification, tracking whether you are inside quotes. When inside quotes, replace commas with a placeholder character (like X'FF'). After pre-processing, use UNSTRING DELIMITED BY ',' to split the fields. Then use INSPECT REPLACING to restore the placeholders back to commas in each parsed field. Also remove the quote characters with INSPECT REPLACING ALL '"' BY SPACE.

18. REPLACING ALL replaces every occurrence of the target in the field. REPLACING FIRST replaces only the first occurrence. Use ALL when normalizing data (e.g., replacing all hyphens with spaces in a phone number). Use FIRST when you need to change only the initial occurrence (e.g., replacing the first 'ERROR' with 'WARN ' in a log message while leaving subsequent occurrences intact).

19. STRING is more readable because it expresses concatenation declaratively — you can see what is being joined and how. However, STRING has overhead: delimiter scanning, pointer management, overflow checking. MOVE with reference modification places data directly at a computed offset, which is simpler for the runtime to execute. In a loop processing millions of records, the cumulative overhead of STRING can be significant. For code executed occasionally or in low-volume paths, readability should win. For inner loops in high-volume batch processing, performance should win.

20. Count all non-numeric characters and check if the count is zero:

MOVE ZERO TO WS-NON-NUMERIC-CNT
INSPECT WS-FIELD TALLYING
    WS-NON-NUMERIC-CNT FOR ALL SPACE
    WS-NON-NUMERIC-CNT FOR ALL 'A' 'B' 'C' ... 'Z'
    (etc. for all non-numeric characters)

A simpler approach: count all digit characters and compare to the field length:

MOVE ZERO TO WS-DIGIT-CNT
INSPECT WS-FIELD TALLYING
    WS-DIGIT-CNT FOR ALL '0' '1' '2' '3' '4'
                         '5' '6' '7' '8' '9'
IF WS-DIGIT-CNT = FUNCTION LENGTH(WS-FIELD)
    ... field is all numeric

Or simplest: use the NUMERIC class test: IF WS-FIELD IS NUMERIC.