Q1
What are the four divisions of a COBOL program?
- IDENTIFICATION DIVISION - Program identification (PROGRAM-ID)
- ENVIRONMENT DIVISION - Hardware/software environment, file assignments
- DATA DIVISION - Data definitions (FILE, WORKING-STORAGE, LINKAGE SECTION)
- PROCEDURE DIVISION - Executable code and business logic
Only IDENTIFICATION and PROCEDURE DIVISION are mandatory.
Q2
What is the difference between SECTION and PARAGRAPH in COBOL?
SECTION:
- Contains one or more paragraphs
- Ends with next SECTION or end of program
- Used for logical grouping
- Can be performed as a unit
PARAGRAPH:
- Basic unit of code
- Named block of statements
- Ends at next paragraph name or SECTION
PERFORM SECTION-NAME executes all paragraphs in the section.
PERFORM PARA-NAME executes only that paragraph.
Q3
What is a copybook and why is it used?
A copybook is a reusable code file that can be included in multiple programs using the COPY statement.
Uses:
- Standard record layouts
- Common working storage definitions
- Reusable paragraphs
- Ensures consistency across programs
Syntax:
COPY EMPREC. COPY EMPREC REPLACING ==EMP== BY ==WS-EMP==.
Benefits:
- Reduces code duplication
- Easier maintenance
- Standard definitions across team
Q4
Explain the four divisions of a COBOL program.
IDENTIFICATION DIVISION: Program identification (PROGRAM-ID required). ENVIRONMENT DIVISION: Hardware/software configuration, file definitions. DATA DIVISION: Variable declarations, file records, working storage. PROCEDURE DIVISION: Executable code, business logic.
Q5
Explain COMP, COMP-1, COMP-2, and COMP-3 data types.
COMP: Binary (2/4/8 bytes based on PIC). COMP-1: Single precision floating point (4 bytes). COMP-2: Double precision floating point (8 bytes). COMP-3: Packed decimal (2 digits per byte + sign nibble).
Q6
How does SEARCH differ from SEARCH ALL?
SEARCH: Sequential search from current index position. SEARCH ALL: Binary search requiring sorted table with KEY clause. SEARCH ALL is faster for large tables but requires sorted data and indexed table.
Q7
What is COPY statement and how does REPLACING work?
COPY includes copybook at compile time. REPLACING substitutes text: COPY copybook REPLACING ==OLD== BY ==NEW==. Used for code reuse and standardization across programs.
Q8
Explain the PERFORM verb variations.
PERFORM para: Execute once. PERFORM para THRU para-exit: Execute range. PERFORM para n TIMES: Execute n times. PERFORM para UNTIL condition: Loop until true. PERFORM VARYING: Counter-controlled loop.
Q9
What is EVALUATE statement and its advantages?
EVALUATE is COBOL's case construct. EVALUATE TRUE WHEN condition-1 action WHEN OTHER default. Cleaner than nested IF, can evaluate multiple subjects, supports ALSO for multiple conditions, uses THRU for ranges.
Q10
What is INITIALIZE and what doesn't it initialize?
INITIALIZE sets fields to defaults: alphabetic to SPACES, numeric to ZEROS. Does NOT initialize: FILLER items, REDEFINES items, index data items. Can use REPLACING for custom values.
Q11
What is the purpose of LINKAGE SECTION?
LINKAGE SECTION defines parameters received from calling program. Items have no memory until CALL provides addresses. Used for passed parameters and dynamically addressed data.
Q12
What is GOBACK vs STOP RUN?
STOP RUN terminates entire run unit. GOBACK returns to caller; if main, acts like STOP RUN. Always use GOBACK in subprograms. STOP RUN from subprogram terminates everything.
Q13
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.
Q14
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.
Q15
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.
Q16
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.
Q17
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.
Q18
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.
Q19
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.
Q20
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.
Q21
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.
Q22
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.
Q23
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.
Q24
How do nested programs work in COBOL?
Nested programs are contained between IDENTIFICATION DIVISION and END PROGRAM. Can access outer program data with GLOBAL clause. COMMON attribute allows sibling access. Promotes modular design.