Q1
Explain REDEFINES clause with an example.
REDEFINES allows the same storage area to be referenced by different data names with different definitions.
Rules:
- Must be at same level as item being redefined
- Cannot redefine item with OCCURS
- Redefined item must appear first
- Lengths should match or redefining should be shorter
01 WS-DATE. 05 WS-DATE-NUM PIC 9(8). 01 WS-DATE-X REDEFINES WS-DATE. 05 WS-YEAR PIC 9(4). 05 WS-MONTH PIC 9(2). 05 WS-DAY PIC 9(2).
Same 8 bytes can be accessed as single number or individual components.
Q2
What is the difference between WORKING-STORAGE and LOCAL-STORAGE?
WORKING-STORAGE is allocated once at program load and persists across calls. LOCAL-STORAGE is allocated fresh each time the program is called and initialized each invocation. Use LOCAL-STORAGE for reentrant programs.
Q3
What is REDEFINES and what are its restrictions?
REDEFINES allows multiple data descriptions for same memory location. Restrictions: Cannot redefine with larger size, must be at same level, cannot use VALUE with REDEFINES target, occurs-depending cannot be redefined.
Q4
What is FILE STATUS and how to use it?
FILE STATUS is 2-byte field capturing I/O results. Declare in SELECT, check after each I/O. Common codes: 00=success, 10=EOF, 22=duplicate, 23=not found, 35=not exists, 39=mismatch.
Q5
How do you handle S0C7 abend?
S0C7 is data exception - non-numeric in numeric field. Debug: Check OFFSET in dump, review initialization, validate input data, check REDEFINES, examine hex dump for invalid bytes in COMP-3 fields.
Q6
Explain STRING and UNSTRING verbs.
STRING concatenates: STRING f1 DELIMITED BY SPACE f2 INTO output. UNSTRING splits: UNSTRING input DELIMITED BY ',' INTO f1 f2. POINTER tracks position, OVERFLOW handles errors, TALLYING counts fields.
Q7
What is OCCURS DEPENDING ON?
ODO creates variable-length tables. 05 TABLE OCCURS 1 TO 100 DEPENDING ON WS-COUNT. Table size varies based on counter. Used with variable-length records. Only variable portion at end of record.
Q8
Explain reference modification.
Reference modification extracts substring: WS-FIELD(start:length). Example: WS-NAME(1:3) gets first 3 chars. Start is 1-based. Length optional (to end). Works with MOVE, IF, DISPLAY.
Q9
What is CORRESPONDING option?
MOVE CORRESPONDING moves fields with matching names between groups. ADD CORRESPONDING/SUBTRACT CORRESPONDING for arithmetic. Convenient but use carefully - silent partial moves if names don't match exactly.
Q10
How does SORT work in COBOL?
SORT verb: SORT sort-file ON ASCENDING KEY key-field USING input-file GIVING output-file. INPUT/OUTPUT PROCEDURE allows processing during sort. RELEASE adds to sort, RETURN retrieves.
Q11
Explain CALL BY REFERENCE vs BY CONTENT vs BY VALUE.
BY REFERENCE: Passes address, changes visible to caller. BY CONTENT: Passes copy, original unchanged. BY VALUE: Passes value for C interop. Default is BY REFERENCE.