Chapter 11 Quiz: Sequential File Processing

Test your understanding of sequential file processing concepts. Each question has a hidden answer -- try to answer before revealing it.


Question 1

What are the four COBOL divisions involved in file processing, and what is each division's role?

Show Answer 1. **IDENTIFICATION DIVISION** -- Names the program (no file-specific content) 2. **ENVIRONMENT DIVISION** -- Maps internal file names to external resources via SELECT...ASSIGN TO 3. **DATA DIVISION** -- Describes record layouts in the FILE SECTION via FD entries 4. **PROCEDURE DIVISION** -- Contains I/O verbs: OPEN, READ, WRITE, CLOSE, REWRITE

Question 2

What does the following SELECT statement do? Identify each clause.

           SELECT CUSTOMER-FILE
               ASSIGN TO CUSTMST
               ORGANIZATION IS SEQUENTIAL
               ACCESS MODE IS SEQUENTIAL
               FILE STATUS IS WS-CUST-STATUS.
Show Answer - `SELECT CUSTOMER-FILE` -- Declares an internal file name used in FD and I/O statements - `ASSIGN TO CUSTMST` -- Maps to the external DD name CUSTMST in JCL (or environment variable in GnuCOBOL) - `ORGANIZATION IS SEQUENTIAL` -- Records are stored and accessed sequentially (this is the default) - `ACCESS MODE IS SEQUENTIAL` -- Records are read/written one after another (the only valid mode for sequential files) - `FILE STATUS IS WS-CUST-STATUS` -- After every I/O operation, COBOL stores a 2-character status code in WS-CUST-STATUS

Question 3

What is the difference between READ EMPLOYEE-FILE and WRITE EMP-RECORD? Why does READ use the file name while WRITE uses the record name?

Show Answer READ operates on the file name because a file can have multiple record descriptions (01-levels under the FD), and the system determines which record buffer to fill. WRITE operates on the record name because you must specify which record buffer to write from. If an FD has multiple 01-level records, you choose which one to write. This design allows a file to have multiple record formats (e.g., header records, detail records, and trailer records with different layouts).

Question 4

What file status code is returned when a READ reaches the end of the file?

Show Answer **10** -- This is the AT END condition. It means no more records are available to read. This is a normal condition, not an error.

Question 5

What is the "priming read" pattern, and why is it used?

Show Answer The priming read is an initial READ performed before entering the processing loop. It ensures that: 1. The AT END condition is checked before any processing occurs (handles empty files correctly) 2. The loop's PERFORM UNTIL condition is tested with valid data 3. The record is available for processing at the top of the loop, not after processing Pattern:
READ first record (priming read)
PERFORM UNTIL end-of-file
    Process current record
    READ next record
END-PERFORM
Without it, the program would either process a garbage record (no data read yet) or process the last record twice.

Question 6

What are the four OPEN modes in COBOL, and when is each used?

Show Answer | Mode | Purpose | Allowed Operations | |---|---|---| | **INPUT** | Read an existing file | READ only | | **OUTPUT** | Create a new file (overwrites existing) | WRITE only | | **EXTEND** | Append to an existing file | WRITE at end only | | **I-O** | Read and update in place | READ and REWRITE |

Question 7

What does BLOCK CONTAINS 0 RECORDS mean in an FD entry?

Show Answer `BLOCK CONTAINS 0 RECORDS` tells the system to determine the optimal block size automatically. On z/OS, this defers to the JCL DCB BLKSIZE parameter or system-managed blocking (SMS). Setting BLKSIZE=0 in both the FD and JCL allows the system to calculate the largest block that fits efficiently on the disk device, maximizing I/O performance. This is the recommended practice.

Question 8

What is the difference between RECFM=FB and RECFM=VB in JCL?

Show Answer - **RECFM=FB** (Fixed Block): Every record is exactly the same length (LRECL bytes). Records are grouped into blocks for efficient I/O. - **RECFM=VB** (Variable Block): Records can vary in length. Each record is preceded by a 4-byte Record Descriptor Word (RDW) containing the record's actual length. LRECL specifies the maximum record length plus 4 bytes for the RDW. Records are grouped into variable-length blocks, each preceded by a 4-byte Block Descriptor Word (BDW).

Question 9

What is the purpose of HIGH-VALUES in the balanced line algorithm?

Show Answer HIGH-VALUES is used as a sentinel value. When a file reaches end-of-file, its current key is set to HIGH-VALUES (the highest possible value in the collating sequence). This ensures that: 1. The exhausted file's key will always be greater than any real key from the other file 2. Remaining records from the other file will be processed correctly 3. When both keys are HIGH-VALUES, both files are exhausted and processing stops Without HIGH-VALUES, special-case logic would be needed to handle one file ending before the other.

Question 10

In a sequential file update, what happens when the old master key equals the transaction key and the transaction code is 'D' (Delete)?

Show Answer The master record is **not written** to the new master file. By simply omitting the WRITE for that record, the record is effectively deleted. The program reads the next record from both files and continues. The old master file is unchanged (it is read-only); only the new master file is missing the deleted record.

Question 11

What do the following ASA carriage control characters mean?

Character Meaning?
' ' (space) ?
'0' ?
'1' ?
'+' ?
Show Answer | Character | Meaning | |---|---| | ' ' (space) | Single space before printing (normal line advance) | | '0' | Double space before printing (skip one line) | | '1' | Advance to top of next page before printing | | '+' | No advance (overprint previous line; used for underlining or bold effects) | These characters occupy the first byte of each record in FBA (Fixed Block ASA) format files and are not printed.

Question 12

What is a control break? When does it occur?

Show Answer A control break occurs when a key field (the control field) changes value between consecutive records in a sorted file. For example, in a file sorted by department, a control break occurs when the department code changes from one record to the next. At each control break, the program typically: 1. Prints subtotals for the group that just ended 2. Resets the group accumulators to zero 3. Saves the new key value as the "previous" key 4. Optionally prints a group header for the new group The program must also handle the final control break after the last record is read, to print subtotals for the last group.

Question 13

What is the difference between DISP=SHR and DISP=OLD in JCL?

Show Answer - **DISP=SHR** -- Shared access. Multiple jobs can read the data set simultaneously. Used for input files that are not being modified. - **DISP=OLD** -- Exclusive access. Only one job can access the data set at a time. Other jobs requesting the same data set will wait in the queue. Used when a job needs to ensure no other job is reading or writing the file. For sequential file processing: input files typically use SHR, while files being updated (in the rare case of I-O mode) use OLD.

Question 14

Explain the three subparameters of DISP=(NEW,CATLG,DELETE).

Show Answer 1. **NEW** -- The initial status. The data set does not exist yet; create it. 2. **CATLG** -- The normal disposition. If the step completes successfully, catalog the data set (make it permanently accessible by name). 3. **DELETE** -- The abnormal disposition. If the step ABENDs (terminates abnormally), delete the data set. This is the standard disposition for creating new output files. If the job fails, incomplete output files are automatically cleaned up.

Question 15

Why should you always include the FILE STATUS clause in your SELECT statements?

Show Answer Without FILE STATUS: - A file I/O error causes the program to ABEND with a cryptic system message (like S0C7, S001, or S013) - There is no way to detect and handle errors programmatically - The program cannot provide meaningful error messages - There is no opportunity for error recovery or graceful termination With FILE STATUS: - Every I/O operation stores a result code that you can check - You can detect specific errors (file not found, disk full, etc.) - You can provide meaningful error messages with context - You can attempt error recovery or gracefully terminate - You can set appropriate return codes for JCL condition checking It is universally considered a best practice, and many shops mandate it in their coding standards.

Question 16

What is QSAM, and how does it improve I/O performance?

Show Answer QSAM (Queued Sequential Access Method) is the z/OS access method for sequential files. It improves performance through: 1. **Buffering** -- QSAM reads entire blocks of records into memory buffers, so individual READ operations return records from the buffer without physical disk I/O. 2. **Look-ahead buffering** -- While the program processes records from one buffer, QSAM reads the next block into a second buffer in parallel. 3. **Write-behind** -- WRITE operations fill a buffer, and the physical write to disk occurs in the background when the buffer is full. 4. **Automatic blocking/deblocking** -- QSAM groups logical records into physical blocks (on WRITE) and extracts logical records from blocks (on READ) transparently. The programmer works with logical records and never deals with physical I/O directly.

Question 17

What happens if you CLOSE an output file without writing any records to it?

Show Answer An empty file is created. On z/OS, the data set exists but contains no records. The file status should be '00' (success) after the CLOSE. The DISP in JCL still applies: if DISP=(NEW,CATLG,DELETE), the empty data set is cataloged normally. However, if the step ABENDs before the CLOSE, the abnormal DISP applies. With DISP=(NEW,CATLG,DELETE), the data set would be deleted. Note: Some z/OS configurations may not catalog an empty data set, depending on SMS (Storage Management Subsystem) settings.

Question 18

What is ORGANIZATION IS LINE SEQUENTIAL, and on which platforms is it available?

Show Answer LINE SEQUENTIAL is a file organization that treats each record as a line of text terminated by a platform-specific line delimiter (newline on Unix, CR+LF on Windows). Features: - Available in **GnuCOBOL** and other non-mainframe COBOL implementations - **Not available on z/OS** (IBM Enterprise COBOL does not support it) - Trailing spaces are removed from records when written - Records can vary in length without explicit RECORD VARYING declarations - No concept of blocking, BDW, or RDW - Useful for processing text files, CSV files, and configuration files On z/OS, standard sequential files (ORGANIZATION IS SEQUENTIAL) handle all text file needs through QSAM.

Question 19

In the context of sequential file processing, what does OPEN EXTEND do differently from OPEN OUTPUT?

Show Answer - **OPEN OUTPUT** creates a new file or overwrites an existing file. The file pointer is positioned at the beginning. Any existing data in the file is lost. - **OPEN EXTEND** opens an existing file and positions the file pointer at the end, after the last existing record. New records written are appended after the existing data. If the file does not exist, OPEN EXTEND creates it (like OPEN OUTPUT). OPEN EXTEND is used for log files, audit trails, and any file where new data must be added without destroying existing content. In JCL, use DISP=MOD to correspond with OPEN EXTEND.

Question 20

A program reads 5,000 records from a master file and 200 transactions. After the update, the new master file has 5,010 records. What combination of transaction types could produce this result?

Show Answer The new master has 10 more records than the old master. The difference comes from ADDs and DELETEs: New count = Old count + Adds - Deletes 5,010 = 5,000 + Adds - Deletes Adds - Deletes = 10 With 200 transactions total (some of which could be Changes that do not affect the count): Possible combinations (among many): - 10 Adds, 0 Deletes, 190 Changes - 15 Adds, 5 Deletes, 180 Changes - 50 Adds, 40 Deletes, 110 Changes - 105 Adds, 95 Deletes, 0 Changes Any combination where Adds - Deletes = 10 and Adds + Deletes + Changes = 200 is valid. Some transactions may also be errors (rejected), so the actual valid transactions could be fewer than 200.

Question 21

What is wrong with the following FD entry and record description?

       FD  CUSTOMER-FILE
           RECORD CONTAINS 100 CHARACTERS.
       01  CUST-RECORD.
           05  CUST-ID        PIC X(10).
           05  CUST-NAME      PIC X(30).
           05  CUST-BALANCE   PIC S9(7)V99.
           05  CUST-STATUS    PIC X(01).
Show Answer The total length of the record fields does not equal 100 characters as specified in RECORD CONTAINS: - CUST-ID: 10 bytes - CUST-NAME: 30 bytes - CUST-BALANCE: 9 bytes (S9(7)V99 in DISPLAY format is 9 bytes; the sign and decimal are implied) - CUST-STATUS: 1 byte - **Total: 50 bytes** But RECORD CONTAINS says 100 CHARACTERS. The record is 50 bytes short. You need a FILLER field of 50 bytes:
           05  FILLER         PIC X(50).
Alternatively, correct RECORD CONTAINS to 50 CHARACTERS. This mismatch could cause file status 39 (file attribute mismatch) at OPEN time or corrupted data.

Question 22

What is the RETURN-CODE special register, and how do JCL COND parameters use it?

Show Answer RETURN-CODE is a COBOL special register that holds the program's exit status (a numeric value). It is passed to the operating system when the program ends, and JCL uses it to control subsequent job steps. Convention: - 0 = Success - 4 = Warning - 8 = Error - 12 = Severe error - 16 = Terminal error JCL uses COND parameters to test the return code:
//STEP2 EXEC PGM=NEXTPGM,COND=(8,LT,STEP1)
This means: skip STEP2 if 8 is Less Than STEP1's return code (i.e., skip if STEP1's RC > 8). Modern JCL uses IF/THEN/ELSE:
//     IF STEP1.RC <= 4 THEN
//STEP2 EXEC PGM=NEXTPGM
//     ENDIF

Question 23

Explain what RECFM=FBA means and when you would use it.

Show Answer RECFM=FBA means: - **F** -- Fixed-length records - **B** -- Blocked (multiple records per physical block) - **A** -- ASA carriage control (the first byte of each record controls printer spacing) Use FBA for **print files and reports**. The first byte of each 133-byte record (with LRECL=133) is the ASA control character: - '1' = new page - ' ' = single space - '0' = double space - '-' = triple space - '+' = overprint The remaining 132 bytes contain the actual print data. When routed to SYSOUT, JES interprets the ASA characters for proper formatting.

Question 24

In a variable-length record with OCCURS 0 TO 50 TIMES DEPENDING ON REC-COUNT, what happens if REC-COUNT contains 0?

Show Answer When REC-COUNT is 0, the variable portion of the record is empty -- there are no occurrences of the repeating group. The record contains only its fixed-length portion. This is perfectly valid and is handled correctly by COBOL. For example, with a 50-byte fixed portion and line items of 19 bytes each: - REC-COUNT = 0: record is 50 bytes (just the header) - REC-COUNT = 1: record is 69 bytes (50 + 19) - REC-COUNT = 50: record is 1000 bytes (50 + 50 * 19) Attempting to access an element beyond REC-COUNT (e.g., accessing element 5 when REC-COUNT is 3) results in undefined behavior and is a programming error.

Question 25

What is the difference between WRITE RPT-LINE FROM WS-DETAIL and performing a MOVE followed by a WRITE?

Show Answer There is no functional difference. They are equivalent:
      * Using FROM:
           WRITE RPT-LINE FROM WS-DETAIL

      * Using MOVE then WRITE:
           MOVE WS-DETAIL TO RPT-LINE
           WRITE RPT-LINE
The FROM clause is syntactic sugar that combines the MOVE and WRITE into a single statement. Most COBOL programmers prefer the FROM version because: 1. It is more concise 2. It clearly shows the intent (write this data to the file) 3. It reduces the chance of accidentally writing the wrong data (forgetting the MOVE) Similarly, `READ file INTO ws-area` is equivalent to a READ followed by MOVE.