Chapter 13 Quiz: Relative File Processing and Advanced File Techniques

Test your understanding of COBOL relative file processing and advanced file techniques. Each question is followed by a hidden answer -- try to answer before revealing it.


Question 1

What clause in the FILE-CONTROL paragraph declares that a file uses relative organization?

  • a) ORGANIZATION IS DIRECT
  • b) ORGANIZATION IS RELATIVE
  • c) ACCESS MODE IS RELATIVE
  • d) FILE TYPE IS RELATIVE
Show Answer **b) ORGANIZATION IS RELATIVE**. The ORGANIZATION clause specifies the file's internal structure. The three valid values are SEQUENTIAL, INDEXED, and RELATIVE. There is no DIRECT or FILE TYPE clause in standard COBOL.

Question 2

Where must the RELATIVE KEY field be defined?

  • a) In the FD record description
  • b) In the FILE SECTION under a separate 01 level
  • c) In the WORKING-STORAGE SECTION
  • d) In the LINKAGE SECTION
Show Answer **c) In the WORKING-STORAGE SECTION**. The RELATIVE KEY must be an unsigned integer defined in WORKING-STORAGE (or LOCAL-STORAGE). It must NOT be defined within the FD record description. This is different from RECORD KEY for indexed files, which IS defined in the FD.

Question 3

What is the minimum valid value for a RELATIVE KEY?

  • a) 0
  • b) 1
  • c) -1
  • d) It depends on the compiler
Show Answer **b) 1**. Relative record numbering starts at 1, not 0. There is no record 0 in a relative file. Attempting to use 0 as a relative key results in an INVALID KEY condition (typically file status 23 or 24).

Question 4

Which ACCESS MODE allows both random and sequential operations on a relative file within the same OPEN?

  • a) ACCESS MODE IS SEQUENTIAL
  • b) ACCESS MODE IS RANDOM
  • c) ACCESS MODE IS DYNAMIC
  • d) ACCESS MODE IS MIXED
Show Answer **c) ACCESS MODE IS DYNAMIC**. Dynamic access allows both random reads/writes (by setting the RELATIVE KEY) and sequential reads (using READ NEXT after a START). There is no ACCESS MODE IS MIXED in COBOL.

Question 5

What VSAM data set type corresponds to COBOL's relative file organization on z/OS?

  • a) ESDS (Entry-Sequenced Data Set)
  • b) KSDS (Key-Sequenced Data Set)
  • c) RRDS (Relative Record Data Set)
  • d) LDS (Linear Data Set)
Show Answer **c) RRDS (Relative Record Data Set)**. VSAM RRDS maps directly to COBOL's ORGANIZATION IS RELATIVE. ESDS corresponds to sequential, KSDS to indexed, and LDS is a special-purpose data set not directly used by COBOL file I/O.

Question 6

What happens when you perform a sequential READ on a relative file and encounter an empty slot?

  • a) The READ returns file status 23
  • b) The program ABENDs with an I/O error
  • c) The empty slot is automatically skipped
  • d) The READ returns a record filled with binary zeros
Show Answer **c) The empty slot is automatically skipped**. During sequential reading of a relative file, the system automatically advances past empty (deleted or never-written) slots and returns the next occupied record. The RELATIVE KEY is updated with the record number of the occupied slot that was actually read.

Question 7

Which of the following is a valid combination for a relative file?

  • a) OPEN EXTEND with ACCESS MODE IS RANDOM
  • b) OPEN I-O with ACCESS MODE IS DYNAMIC
  • c) OPEN OUTPUT with DELETE operation
  • d) OPEN INPUT with REWRITE operation
Show Answer **b) OPEN I-O with ACCESS MODE IS DYNAMIC**. This combination allows reading (random and sequential), writing new records, rewriting existing records, and deleting records. OPEN EXTEND requires SEQUENTIAL access. OPEN OUTPUT does not support DELETE. OPEN INPUT does not support REWRITE.

Question 8

File status code 22 on a relative file WRITE operation indicates:

  • a) End of file reached
  • b) Record not found
  • c) Duplicate key (slot already occupied)
  • d) Boundary violation
Show Answer **c) Duplicate key (slot already occupied)**. Status 22 means the program attempted to WRITE to a slot that already contains a record. To replace an existing record, you must READ it first and then use REWRITE. Status 23 = not found, status 10 = end of file, status 24 = boundary violation.

Question 9

What does the LINAGE clause specify?

  • a) The maximum number of records in a file
  • b) The logical page layout for a print file
  • c) The record length of a variable-length file
  • d) The blocking factor for a sequential file
Show Answer **b) The logical page layout for a print file**. The LINAGE clause defines the number of printable lines per logical page, along with optional footing area, top margin, and bottom margin. It is used with print files (sequential output) to manage page overflow declaratively.

Question 10

In the LINAGE clause LINAGE IS 60 LINES WITH FOOTING AT 55 LINES AT TOP 3 LINES AT BOTTOM 3, what is the total physical page size?

  • a) 55 lines
  • b) 60 lines
  • c) 63 lines
  • d) 66 lines
Show Answer **d) 66 lines**. The total physical page = LINAGE body (60) + top margin (3) + bottom margin (3) = 66 lines. The FOOTING AT 55 is a trigger point within the 60-line body, not an additional allocation. When LINAGE-COUNTER reaches 55, the AT END-OF-PAGE condition triggers.

Question 11

What special register tracks the current line position within a LINAGE page body?

  • a) LINE-COUNTER
  • b) LINAGE-COUNTER
  • c) PAGE-COUNTER
  • d) PRINT-LINE
Show Answer **b) LINAGE-COUNTER**. This special register is automatically maintained by the system and reflects the current line position within the LINAGE page body. It starts at 1 after a page advance and increments with each WRITE ADVANCING. LINE-COUNTER and PAGE-COUNTER are Report Writer registers, not LINAGE registers.

Question 12

In the balanced line algorithm for multi-file processing, what action is taken when the master file key is less than the transaction file key?

  • a) The transaction is an unmatched transaction (error)
  • b) The master record is written through unchanged and the master file is advanced
  • c) Both files are advanced
  • d) The transaction is applied to the master
Show Answer **b) The master record is written through unchanged and the master file is advanced**. When the master key is lower, it means there is no transaction for that master record. The master record passes through to the output unchanged, and the next master record is read. The transaction file remains at its current position.

Question 13

True or False: A relative file supports variable-length records.

Show Answer **False**. Relative files require fixed-length records. This is because the system calculates a record's position using the formula: byte-offset = (record-number - 1) * record-length. If records had variable lengths, this direct calculation would be impossible. If you need variable-length records, use indexed (KSDS) or sequential (ESDS) organization instead.

Question 14

True or False: The RELATIVE KEY clause is required when ACCESS MODE IS SEQUENTIAL for a relative file.

Show Answer **False**. The RELATIVE KEY clause is optional when ACCESS MODE IS SEQUENTIAL. If provided, the system will update it with the record number of each record read, which is useful for tracking position. If omitted, the program simply reads records in sequence without knowing their slot numbers. However, RELATIVE KEY is required for RANDOM and DYNAMIC access modes.

Question 15

True or False: You can use the START statement with a relative file to position for sequential reading.

Show Answer **True**. The START statement positions the file for subsequent sequential READ operations. With a relative file, you set the RELATIVE KEY to the desired starting position and execute START. The system positions to the first record at or after that position (depending on the relational operator used). START requires ACCESS MODE IS SEQUENTIAL or DYNAMIC.

Question 16

True or False: When a relative file is opened OUTPUT, all existing records in the file are deleted.

Show Answer **True**. OPEN OUTPUT creates a new file (or empties an existing one). All previously existing records are discarded. To preserve existing records while adding new ones, use OPEN I-O (for random writes to specific slots) or OPEN EXTEND (for appending in sequential mode). This is the same behavior as OPEN OUTPUT for sequential and indexed files.

Question 17

True or False: A REWRITE on a relative file requires a prior successful READ of the same record.

Show Answer **True** for sequential access mode. For RANDOM and DYNAMIC access modes, a prior READ is NOT required -- you can REWRITE by simply setting the RELATIVE KEY and executing REWRITE. However, the slot must be occupied (status 23 results if the slot is empty). For sequential access, the last READ must have been successful, and REWRITE replaces the last-read record.

Question 18

True or False: The DELETE operation on a relative file physically removes the record and compacts the file.

Show Answer **False**. DELETE on a relative file marks the slot as empty but does not physically remove it or compact the file. The slot remains in its position and can be reused by a subsequent WRITE to that same slot number. The file size does not change after a DELETE. This "tombstone" approach is necessary to preserve the direct-calculation addressing scheme.

Question 19

What is the output of the following code? Assume the relative file has records in slots 1, 3, and 5 only.

       MOVE 2 TO WS-REL-KEY
       START REL-FILE KEY IS >= WS-REL-KEY
           INVALID KEY
               DISPLAY "START FAILED"
           NOT INVALID KEY
               DISPLAY "POSITIONED AT " WS-REL-KEY
       END-START
       READ REL-FILE NEXT
           AT END DISPLAY "EOF"
           NOT AT END
               DISPLAY "READ SLOT " WS-REL-KEY
       END-READ
Show Answer The output is:
POSITIONED AT 0003
READ SLOT 0003
The START with KEY IS >= 2 positions the file at the first record at or after slot 2. Since slot 2 is empty, the system advances to slot 3 (the next occupied slot). After START, WS-REL-KEY is updated to 3. The subsequent READ NEXT retrieves the record at slot 3 and confirms the key. Note: some implementations may not update WS-REL-KEY on START -- they only update it on READ. In that case, the START output would show "POSITIONED AT 0002" but the READ would still show "READ SLOT 0003".

Question 20

What is wrong with the following code?

       OPEN OUTPUT REL-FILE
       MOVE 100 TO WS-REL-KEY
       READ REL-FILE
           INVALID KEY
               DISPLAY "NOT FOUND"
           NOT INVALID KEY
               DISPLAY "FOUND"
       END-READ
Show Answer The file is opened OUTPUT, but a READ operation is being attempted. READ is not valid when a file is opened OUTPUT -- it can only be used with OPEN INPUT or OPEN I-O. OPEN OUTPUT is for creating/writing records only. The corrected code should use OPEN INPUT (for read-only access) or OPEN I-O (for read and write access).

Question 21

What is the output of the following code? The relative file contains records in slots 1 through 5, each with a 20-character name field.

       OPEN I-O REL-FILE
       MOVE 3 TO WS-REL-KEY
       READ REL-FILE INTO WS-RECORD
           INVALID KEY
               DISPLAY "MISS"
       END-READ
       DISPLAY "STATUS: " WS-FILE-STATUS
       MOVE "UPDATED RECORD" TO WS-NAME IN WS-RECORD
       REWRITE REL-REC FROM WS-RECORD
           INVALID KEY
               DISPLAY "REWRITE FAILED"
           NOT INVALID KEY
               DISPLAY "REWRITE OK AT " WS-REL-KEY
       END-REWRITE
Show Answer The output is:
STATUS: 00
REWRITE OK AT 0003
The READ succeeds (slot 3 exists), returning status 00. The program then modifies the working-storage copy and REWRITE replaces the record at slot 3. With RANDOM access, REWRITE uses the current value of WS-REL-KEY (which is still 3) to determine which slot to update. The INVALID KEY does not fire because the slot exists and the REWRITE succeeds.

Question 22

Examine the following LINAGE-based write logic. What happens when the 56th detail line is written?

       FD  REPORT-FILE
           LINAGE IS 60 LINES
           WITH FOOTING AT 55
           LINES AT TOP 3
           LINES AT BOTTOM 3.
       ...
       WRITE REPORT-REC FROM DETAIL-LINE
           AFTER ADVANCING 1 LINE
           AT END-OF-PAGE
               PERFORM 9000-PAGE-BREAK
       END-WRITE
Show Answer When the 56th line is written, the LINAGE-COUNTER has moved past the FOOTING AT 55 position. The AT END-OF-PAGE condition is triggered, and the 9000-PAGE-BREAK paragraph is performed. This paragraph typically writes a page footing, advances to a new page (WRITE AFTER ADVANCING PAGE), writes a new page heading, and resets any line counters. The AT END-OF-PAGE fires when LINAGE-COUNTER exceeds the FOOTING value after a WRITE, not when it equals the FOOTING value.

Question 23

What will happen when the following code executes? The relative file has 100 slots, records exist in slots 1-50.

       MOVE 0 TO WS-WRITE-COUNT
       OPEN I-O REL-FILE
       PERFORM VARYING WS-REL-KEY FROM 51 BY 1
           UNTIL WS-REL-KEY > 100
           MOVE SPACES TO REL-REC
           MOVE WS-REL-KEY TO RR-SLOT-NUM
           MOVE "NEW ACCOUNT" TO RR-NAME
           MOVE ZEROS      TO RR-BALANCE
           WRITE REL-REC
               INVALID KEY
                   DISPLAY "WRITE FAILED AT " WS-REL-KEY
                       " STATUS=" WS-FILE-STATUS
               NOT INVALID KEY
                   ADD 1 TO WS-WRITE-COUNT
           END-WRITE
       END-PERFORM
       DISPLAY "RECORDS WRITTEN: " WS-WRITE-COUNT
Show Answer The code will write 50 new records to slots 51 through 100. Since slots 51-100 are empty, each WRITE succeeds (no duplicate key error). The output will be:
RECORDS WRITTEN: 000050
The file is opened I-O, which allows both reading existing records and writing new records to empty slots. Each WRITE uses the current value of WS-REL-KEY (set by the PERFORM VARYING loop) to determine the target slot. If any slot in the 51-100 range were already occupied, that specific WRITE would fail with status 22 (duplicate key) and the INVALID KEY clause would fire.

Question 24

A programmer writes the following code to delete all records from a relative file while keeping the file intact. Will it work? Explain.

       OPEN I-O REL-FILE
       MOVE 1 TO WS-REL-KEY
       READ REL-FILE NEXT
           AT END DISPLAY "FILE EMPTY"
       END-READ
       PERFORM UNTIL WS-FILE-STATUS = '10'
           DELETE REL-FILE
           READ REL-FILE NEXT
               AT END CONTINUE
           END-READ
       END-PERFORM
       CLOSE REL-FILE
       DISPLAY "ALL RECORDS DELETED"
Show Answer This code has a subtle issue. With **sequential** access mode, the DELETE after READ NEXT deletes the last-read record, which is correct. However, if the access mode is **RANDOM**, the DELETE uses WS-REL-KEY and does not depend on the last READ. The code assumes sequential/dynamic access with READ NEXT behavior. With DYNAMIC access mode and READ NEXT, this code will work correctly. Each READ NEXT retrieves the next occupied slot (skipping empty ones), and DELETE removes it. After the last record is deleted, READ NEXT returns status 10 (end of file), and the loop terminates. However, the code could be made more robust by checking WS-FILE-STATUS after the DELETE operation. If the DELETE fails for any reason, the program would loop infinitely (reading the same record and failing to delete it repeatedly).

Question 25

What is the key difference between the following two WRITE approaches for a relative file with RANDOM access?

      * Approach A
       MOVE 42 TO WS-REL-KEY
       MOVE "JOHN SMITH" TO REL-NAME
       WRITE REL-REC
           INVALID KEY DISPLAY "SLOT OCCUPIED"
       END-WRITE

      * Approach B
       MOVE 42 TO WS-REL-KEY
       MOVE "JOHN SMITH" TO WS-NAME
       WRITE REL-REC FROM WS-RECORD
           INVALID KEY DISPLAY "SLOT OCCUPIED"
       END-WRITE
Show Answer Both approaches write to slot 42, but they differ in where the record data comes from: **Approach A** modifies the FD record area (REL-NAME is a field within REL-REC) directly and then writes it. Any uninitialized fields in REL-REC will contain whatever was last in the buffer. **Approach B** builds the record in WORKING-STORAGE (WS-RECORD) and uses the FROM clause to copy it into the FD record area before writing. This is generally safer because the WORKING-STORAGE record can be fully initialized with MOVE SPACES or INITIALIZE before populating specific fields. The FROM clause is the recommended approach because it gives you full control over the record content without relying on the state of the FD buffer.

Question 26

A bank application uses a relative file with 10,000 slots to store account records. The account number is a 10-digit number. The program calculates the slot as: COMPUTE WS-REL-KEY = FUNCTION MOD(WS-ACCOUNT-NUM, 10000) + 1. Two different accounts produce the same slot number. What is this situation called, and how should the program handle it?

Show Answer This is called a **hash collision** (or key collision). Two different account numbers hash to the same relative record slot. For example, accounts 1234510001 and 1234520001 would both hash to slot 1 (since both MOD 10000 = 1). Common handling strategies: 1. **Linear probing:** If slot N is occupied by a different account, try slot N+1, N+2, etc., until an empty slot is found. This requires storing the original account number in the record for verification during reads. 2. **Overflow area:** Write colliding records to a separate overflow file and link them by storing a pointer in the main slot. 3. **Reject and report:** Write the colliding record to an exception file for manual resolution. 4. **Avoid the problem:** Use a larger slot range or a better hash function to minimize collisions. In a banking context, option 1 or 2 with careful verification is most common, as every account must be stored reliably.

Question 27

Explain the purpose of the INVALID KEY and NOT INVALID KEY clauses for relative file operations. For which I/O verbs are these clauses valid?

Show Answer **INVALID KEY** specifies the action to take when an I/O operation fails due to a key-related condition (e.g., record not found, duplicate key, boundary violation). **NOT INVALID KEY** specifies the action when the operation succeeds. Valid I/O verbs for INVALID KEY with relative files: | Verb | INVALID KEY fires when... | |------|--------------------------| | READ (random) | Slot is empty (status 23) or key is invalid (status 24) | | WRITE | Slot is already occupied (status 22) or key is invalid (status 24) | | REWRITE | Slot is empty (status 23) in random mode | | DELETE | Slot is empty (status 23) in random mode | | START | No record found at or beyond the specified key (status 23) | Note: INVALID KEY is not valid for sequential READ (READ NEXT). Sequential reads use AT END / NOT AT END instead.

Question 28

A programmer needs to process a relative file of 5,000 bank branch records and an indexed file of 100,000 customer records simultaneously. The branch file maps branch number (1-5000) to branch details, and the customer file contains a branch number field. For each customer, the program must look up the branch details. Which file should be opened with which access mode, and why?

Show Answer **Relative file (branch records):** Open with ACCESS MODE IS RANDOM. The branch number from each customer record maps directly to a relative key, enabling O(1) lookups. Since the program jumps between branches in the order determined by the customer file (not in branch-number order), random access is essential. **Indexed file (customer records):** Open with ACCESS MODE IS SEQUENTIAL (or DYNAMIC if both sequential and random access are needed). If processing all customers in key order for a report, sequential access is most efficient. If processing random customer lookups, use RANDOM. The relative file is ideal for the lookup table role because the branch number IS the record position, giving instant access with no index traversal. This is a classic pattern: use a relative file as a direct-access reference table and an indexed or sequential file as the main processing file.

Question 29

True or False: The LINAGE clause can only be used with sequential output files (print files), not with relative or indexed files.

Show Answer **True**. The LINAGE clause is exclusively for sequential files opened for output. It provides logical page formatting for printed reports. It has no applicability to relative or indexed files, which store data records rather than formatted print lines. The LINAGE clause appears in the FD entry and works with the WRITE... AFTER ADVANCING statement to manage page layout.

Question 30

A relative file containing loan records has the following slot occupancy pattern. Slots 1-100: 90 occupied, 10 empty. Slots 101-200: 45 occupied, 55 empty. Slots 201-300: all empty.

If a program reads this file sequentially from the beginning, how many READ NEXT operations will be needed to reach end-of-file (status 10)?

Show Answer **136 READ NEXT operations** (135 successful reads + 1 that returns status 10). Sequential reading skips empty slots automatically. The program will read 90 occupied records in slots 1-100, then 45 occupied records in slots 101-200, for a total of 135 occupied records. After the 135th successful read, the next READ NEXT will attempt to find another occupied record, scan through all empty slots in 201-300, reach the end of the file, and return status 10. So: 135 reads that return data + 1 read that returns AT END = 136 total READ NEXT operations. The empty slots are skipped internally and do not count as separate READ operations.