COBOL Realtime (Scenario based) Interview Questions (61 - 70)
61. Why should we not define OCCURS clause at 01 level?
The OCCURS clause is not allowed at 01 level because:
- 01 level represents a complete record structure, and a record itself cannot repeat in the same definition.
- Files and data structures at 01 level are fixed, whereas OCCURS is meant for repeating elements within a structure.
- COBOL's hierarchical data structure does not support direct repetition of an entire 01 level group.
62. What do you understand by static and dynamic linking?
Static Linking:
- The called program is linked at compile time and becomes part of the main program's executable.
- Requires recompilation of the main program if the called program changes.
- Faster execution but results in a larger executable size.
Dynamic Linking:
- The called program is loaded at runtime and remains a separate module.
- No need to recompile the main program if the subprogram changes.
- Slightly slower execution but reduces executable size.
63. Consider a scenario where we have a program having an array of 20 items. When the program tries to access the 21st item, the program fails to abend. What is the reason for this?
By default, COBOL does not perform automatic boundary checking for subscripts or indexes. Because the program is likely compiled with the NOSSRANGE option.
If SSRANGE were enabled, accessing 21st item in an array of 20 would cause a runtime error (S0C4 abend).
64. Why is S9(4) COMP used most despite knowing that Comp-3 would utilize less space?
S9(4) COMP | COMP-3 |
---|---|
S9(4) COMP (Binary) is faster in arithmetic operations since CPUs process binary numbers directly. | COMP-3 (Packed Decimal) requires decimal-to-binary conversion, making arithmetic operations slower. |
S9(4) COMP always occupies 2 bytes, making memory allocation predictable. | COMP-3 stores two digits per byte + sign, requiring 3 bytes for S9(4), which is less efficient in binary arithmetic. |
65. What do you understand by the following terminologies?
- AMODE(31)
- AMODE(24)
- RMODE(24)
- RMODE(ANY)
AMODE(31) (31-bit Addressing Mode) - The program can access memory addresses up to 2GB (31-bit addressable storage). Most modern COBOL programs run in AMODE(31) for accessing extended memory.
AMODE(24) (24-bit Addressing Mode) - The program can access only the first 16MB of memory (24-bit addressable storage). Used in older COBOL programs and some legacy applications.
RMODE(24) (Residency Mode 24-bit) - The program must be loaded within the first 16MB of memory. Typically used for programs that interact with 24-bit AMODE.
RMODE(ANY) (Residency Mode Any) - The program can be loaded anywhere in memory, either above or below the 16MB line. Provides flexibility for modern applications running in AMODE(31).
66. How is INCLUDE different from COPY?
INCLUDE | COPY |
---|---|
Used in Embedded SQL (DB2 COBOL programs). | Used to import COBOL copybooks. |
|
|
Handled by the DB2 precompiler. | Handled by the COBOL compiler. |
Used for DB2 table declarations and SQL-related structures. | Used for COBOL data structures, file definitions, constants, etc. |
67. Under what circumstances are scope terminators mandatorily needed?
Scope terminators must be used in nested structures, in-line PERFORMs, EVALUATE, and file handling constructs to ensure clear, error-free execution.
68. How can we process two files by comparing key fields?
- Sort both files on the key field (if not already sorted).
- Read the first record from both files.
- Repeat until end of both files - Compare the key fields:
- If keys match, process the record.
- If File1 key < File2 key, read next record from File1.
- If File2 key < File1 key, read next record from File2.
69. How do we remove the spaces at end of every record in an output file that is of variable length?
Use the UNSTRING or INSPECT statement to remove trailing spaces before writing to the file.
Method 1: Using INSPECT to Find the Last Non-Space Character
INSPECT WS-RECORD
TALLYING WS-LENGTH FOR TRAILING SPACES.
SUBTRACT WS-LENGTH FROM LENGTH OF WS-RECORD GIVING WS-ACTUAL-LEN.
WRITE OUTPUT-RECORD FROM WS-RECORD
LENGTH WS-ACTUAL-LEN.
Method 2: Using UNSTRING to Trim Spaces
UNSTRING WS-RECORD
DELIMITED BY SPACE INTO WS-TRIMMED.
WRITE OUTPUT-RECORD FROM WS-TRIMMED.
70. Why should the file be opened in I-O mode when it is being used for REWRITE purposes?
The REWRITE statement modifies existing records in a file. To read and update a record, the file must be opened in I-O (Input-Output) mode. I-O mode allows both reading (READ) and updating (REWRITE) of records in the same session.