COBOL Scenario-Based Interview Questions and Answers
1. How do you remove duplicates from a file using COBOL?
You can remove duplicates by sorting the input file and comparing current and previous records.
Example Code:
```cobol
READ IN-FILE
AT END MOVE 'Y' TO EOF-FLAG
NOT AT END
IF EMP-ID NOT = WS-PREV-EMP-ID
WRITE OUT-REC FROM IN-REC
MOVE EMP-ID TO WS-PREV-EMP-ID
END-IF
END-READ
```
Assumes input is sorted by EMP-ID.
2. How do you handle a file that has variable-length records in COBOL?
Use RECORD IS VARYING clause.
```cobol
FD EMP-FILE
RECORD IS VARYING IN SIZE FROM 10 TO 100
DEPENDING ON WS-REC-LEN
```
Allows reading variable-length records.
3. How to read a file backwards in COBOL?
COBOL can't read sequential files backward directly.
Workaround:
1. Store records in an array.
2. Process array in reverse order.
4. How to merge two sorted files without duplicates?
Compare keys of both files, write the smaller one.
```cobol
IF A-KEY < B-KEY
WRITE A
ELSE IF A-KEY > B-KEY
WRITE B
ELSE
WRITE A
READ BOTH
```
5. How do you debug a SOC7 abend in COBOL?
SOC7 = Data exception (usually numeric).
Steps:
- Check dump offset
- Match with source line
- Use DISPLAYs before numeric moves
- Use NUMVAL to validate alphanumeric data
6. How to implement pagination (Page In/Page Out) in CICS-COBOL?
Use Temporary Storage (TSQ) to store all records. Maintain current page index using EIBCALEN or
COMMAREA.
7. How to count the number of records in a VSAM file?
Use a simple loop with READ and counter increment.
```cobol
ADD 1 TO REC-COUNT
READ VSAM-FILE UNTIL EOF
```
8. How do you handle file status in COBOL?
Always declare FILE STATUS in FILE-CONTROL.
```cobol
IF WS-FILE-STAT = '00'
CONTINUE
ELSE DISPLAY ERROR
```
9. What are some performance tuning tips for COBOL programs?
- Use COMP/COMP-3 for arithmetic fields
- Avoid unnecessary file access
- Use binary counters
- Minimize SORTs
10. How to use CALL and pass variables to subprograms?
Use CALL with USING clause.
```cobol
CALL 'SUBPROG' USING VAR1 VAR2
LINKAGE SECTION.
01 VAR1 PIC X(10).
01 VAR2 PIC 9(4).
```