Quiz — Chapter 15: Sort and Merge
Multiple Choice
1. What level indicator is used to define a sort work file in the FILE SECTION?
a) FD b) SD c) WS d) SW
2. Which statement is used to pass a record to the sort inside an INPUT PROCEDURE?
a) WRITE b) RETURN c) RELEASE d) SEND
3. Which statement retrieves the next sorted record inside an OUTPUT PROCEDURE?
a) READ b) RELEASE c) FETCH d) RETURN
4. What happens if you attempt to OPEN a file declared with an SD entry?
a) It opens normally b) A runtime warning is issued c) A compilation error occurs d) The file opens in read-only mode
5. What value does SORT-RETURN contain after a successful sort?
a) 1 b) 4 c) 0 d) 8
6. How many input files does the MERGE verb require at minimum in its USING clause?
a) 1 b) 2 c) 3 d) No minimum
7. Which of the following is TRUE about the MERGE verb?
a) MERGE supports both INPUT PROCEDURE and OUTPUT PROCEDURE b) MERGE supports INPUT PROCEDURE but not OUTPUT PROCEDURE c) MERGE supports OUTPUT PROCEDURE but not INPUT PROCEDURE d) MERGE does not support either INPUT or OUTPUT PROCEDURE
8. What does the WITH DUPLICATES IN ORDER clause guarantee?
a) Duplicate records are removed b) Records with equal keys retain their original input order c) Duplicate records are written to a separate file d) Only the first duplicate is kept
9. What does setting SORT-RETURN to 16 inside an INPUT PROCEDURE do?
a) Skips the current record b) Forces the sort to terminate c) Writes the record to an error file d) Ignores the sort key for this record
10. Which COBOL compiler option allows the sort utility to bypass COBOL I/O routines for USING/GIVING operations?
a) OPTIMIZE b) FASTSRT c) SORTOPT d) DIRECTIO
True or False
11. You can execute a SORT statement inside the INPUT PROCEDURE of another SORT.
12. The RELEASE statement can be used outside of an INPUT PROCEDURE.
13. Input files to a MERGE must already be sorted on the keys specified in the MERGE statement.
14. You must explicitly OPEN and CLOSE files named in USING and GIVING phrases.
15. SORT-RETURN is a special register that exists without being declared in WORKING-STORAGE.
Short Answer
16. Explain the difference between USING/GIVING and INPUT/OUTPUT PROCEDURE. When would you choose each approach?
17. What is the "error threshold pattern" in sort processing? Why is it important in production batch environments?
18. Describe the tag sort technique. Under what circumstances would you use it?
19. A developer writes a MERGE program but gets incorrect results — some records appear out of order in the output. What is the most likely cause, and how would you diagnose it?
20. Maria Chen says: "If you're just reordering records, use JCL SORT. If you need to think about the records while sorting them, use COBOL SORT." Explain what she means and give an example of each scenario.
Answer Key
1. b) SD
2. c) RELEASE
3. d) RETURN
4. c) A compilation error occurs
5. c) 0
6. b) 2
7. c) MERGE supports OUTPUT PROCEDURE but not INPUT PROCEDURE
8. b) Records with equal keys retain their original input order
9. b) Forces the sort to terminate
10. b) FASTSRT
11. False — Nested SORT/MERGE is not permitted.
12. False — RELEASE is valid only inside an INPUT PROCEDURE.
13. True — MERGE assumes and requires pre-sorted input.
14. False — The SORT verb automatically opens and closes files named in USING and GIVING.
15. True — SORT-RETURN is an implicit special register.
16. USING/GIVING is a simple, declarative approach where the SORT verb handles all file I/O automatically. It is best for straightforward sorts that need no data transformation or filtering. INPUT/OUTPUT PROCEDUREs give the programmer explicit control over reading (INPUT) and writing (OUTPUT), allowing filtering, validation, transformation, and complex processing. Use USING/GIVING when all records should be sorted as-is; use INPUT/OUTPUT PROCEDUREs when you need to modify, filter, or validate records during the sort.
17. The error threshold pattern monitors the ratio of invalid records to total records during an INPUT PROCEDURE. If the error rate exceeds a defined threshold (e.g., 1%), the sort aborts by setting SORT-RETURN to 16. This is important because a high error rate usually indicates a systemic problem (wrong file, truncated transmission, format change) rather than isolated bad records, and processing bad data could corrupt downstream systems.
18. A tag sort extracts only the sort key plus a record sequence number into a short record, sorts these short records, and then uses the sequence numbers to resequence the original file. It is beneficial when records are very long (e.g., 2000 bytes) but sort keys are short (e.g., 20 bytes), reducing I/O during the sort phase dramatically.
19. The most likely cause is that one or more input files to the MERGE are not actually sorted on the keys specified in the MERGE statement. MERGE assumes pre-sorted input and does not verify it. To diagnose, examine each input file independently and verify that records are in the expected key order. A simple verification program or a DFSORT VERIFY step can confirm sort order.
20. "Just reordering records" means the sort has no business logic — no filtering, no validation, no transformation. A JCL SORT step (DFSORT/SyncSort invoked directly) is simpler, faster, and requires no COBOL program. Example: sorting a backup file by date for archival. "Thinking about records" means applying business logic during the sort — validating data, converting formats, filtering by criteria, or producing summary output. Example: sorting daily transactions while rejecting invalid records and producing an error report. This requires a COBOL SORT with INPUT/OUTPUT PROCEDUREs.