COBOL mostly asked Interview Question on Files

Question is there are two files in  a COBOL program one is Master file and the other one is Transaction file, and how to compare records these two files and write the data into master file finally.

Experienced programmers can write this logic. But this is little tricky for less experienced programmers.

The characteristics of Master file:

  • A master file is the major collection of data pertaining to a specific application.
  • Master files are stored on magnetic media because disk files:
    a. Can store very large files.
    b. Can read and write data very quickly.
    c. Can store records of any size.
  • Disk files can be processed either sequentially or randomly, while tapes can only be processed sequentially.
  • Tapes are not used much any more.

The characteristics of Transaction file:

  • Pre conditions before processing of Master file and Transaction file:
  • In a sequential master file, all records are in sequence by a key field.
  • The transaction file must also be in the same key field sequence.

The below is the logic we need to write:

  1. A record is read from each file immediately after the files are opened.
  2. The key fields from the master and transaction records are compared to determine what processing should be done:
    a. When a record exists on the master file with a corresponding transaction record (TRANS-KEY = MASTER-KEY):
    i. The master record is updated and written to the new master file.
    ii. Records are read from both the master and transaction files.

b. When there is a record on the master file with no corresponding transaction record (TRANS-KEY > MASTER-KEY):
i. No updating occurs. The old master record is written to the new master file as is.
ii. A record is read from the old master file.

c. When a record exists on the transaction file for which there is no corresponding record on the master file (TRANS-KEY < MASTER-KEY):
i. Either a new record is created from the transaction record or an error has occurred, depending on the type of update being performed.
ii. In either case, after the transaction is processed, a record is read from the transaction file only.

  1. Since both the end of the old master file and the end of the transaction file will not be reached at the same time, some provision must be made so processing continues properly:
    a. HIGH-VALUES can be moved to the key field of the file which has reached end-of-file to ensure that records from the other file will continue to be processed correctly.
    b. Processing continues until the key fields of both the master and transaction input records contain HIGH-VALUES.
  2. HIGH-VALUES is a COBOL reserved word which refers to the largest value in the computer’s collating sequence. HIGH-VALUES means that all bits are on.

Related: Click here to read more on File processing

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.