COBOL Interview Questions and Answers (21 - 30)


21. How many bytes S9(8) comp field occupy and its maximum value?

S9(8) COMP occupies 4 bytes. Minimum Value: -2,147,483,648. Maximum Value: +2,147,483,647

22. Difference between redefines and renames in COBOL?

REDEFINES allows one data item to share the same memory location as another data item. It must use the same level number.

RENAMES provides an alternative name for multiple existing fields in a group structure. It must be declared under 66 level number.

FeatureREDEFINESRENAMES
PurposeAllows multiple data representations for the same memoryCreates an alternative name for multiple consecutive fields
Syntax LevelBoth items should be at same levelMust be declared at 66 level
Can Change Data Declaration?Yes (e.g., numeric ↔ alphanumeric) No (only changes name)
Requires THRU Clause? NoYes, in some screnarios
Common UsageMemory optimization, data conversionGrouping fields under one name

23. Can we redefine the field of X(200) to X(100) (smaller in size)?

Yes, we can redefine a field of X(200) to X(100), but there are important considerations regarding memory allocation.

WORKING-STORAGE SECTION.
01 WS-BIG-FIELD     PIC X(200).
01 WS-SMALL-FIELD   REDEFINES WS-BIG-FIELD PIC X(100).

WS-SMALL-FIELD uses the first 100 bytes of WS-BIG-FIELD. The remaining 100 bytes of WS-BIG-FIELD remain unused by WS-SMALL-FIELD.

24. Can we redefine the field of X(100) to X(200) (larger in size)?

Yes, we can redefine a field of X(100) to X(200), but there are important considerations regarding memory allocation.

WORKING-STORAGE SECTION.
01 WS-SMALL-FIELD     PIC X(100).
01 WS-BIG-FIELD   REDEFINES WS-SMALL-FIELD PIC X(200).

WS-BIG-FIELD uses the first 100 bytes of WS-SMALL-FIELD. The remaining 100 bytes of WS-BIG-FIELD is unintialized and allocated by system.

25. What does the INITIALIZE verb do?

The INITIALIZE verb is used to reset data items (variables) by assigning them default values based on their data type. It is useful for clearing all elementary variables in the group variable before reuse.

Default initialization based on data type follows -

Data TypeDefault Value Assigned by INITIALIZE
Alphabetic (A)Spaces (SPACES)
Alphanumeric (X)Spaces (SPACES)
Numeric (9)Zeros (ZEROES)
Packed Decimal (COMP-3)Zeros (ZEROES)
Binary (COMP)Zeros (ZEROES)

26. How do we get current date from system with century?

To retrieve the current date with the century, COBOL provides the ACCEPT statement with the special register DATE or DATE YYYYMMDD.

The easiest way to get the system date with the 4-digit year (century included) is:

...
WORKING-STORAGE SECTION.
01 WS-CURRENT-DATE PIC 9(8).  *> YYYYMMDD format

PROCEDURE DIVISION.
    ACCEPT WS-CURRENT-DATE FROM DATE YYYYMMDD.
    DISPLAY "Current Date: " WS-CURRENT-DATE.

    STOP RUN.

Output: Current Date: 20250221

27. When is a scope terminator mandatory?

Scope terminators are explicit closing keywords used to mark the end of a statement or block. A scope terminator is mandatory in nested and inline statements to avoid ambiguity. For example -

...
IF A > B
    IF C > D
        DISPLAY "C is greater"
	ELSE
		DISPLAY "D is greater" 
    END-IF
ELSE
	DISPLAY "A is greater"
END-IF.

Without END-IF, the ELSE might be linked to the wrong IF, leading to logical errors.

28. Give some examples of command terminators?

Command terminators are also called scope terminators. Some of the common command terminators are - END-IF, END-PERFORM, END-EVALUATE, END-READ, END-WRITE and END-RETURN.

29. What is the use of EVALUATE statement?

The EVALUATE statement is similar to a CASE or SWITCH statement in other programming languages. It is used to simplify multiple conditional checks by evaluating an expression and executing the corresponding block of code.

  • Replaces Multiple IF-ELSE Conditions - Makes the program more readable and structured.
  • Supports Multiple Conditions in One Line - Can check multiple variables in a single EVALUATE.
  • Handles Ranges and Multiple Values - Allows defining ranges (THRU) and multiple matching values.
  • Eliminates Nested IF Statements - Reduces code complexity and improves maintainability.

30. Explain the difference between the EVALUATE and IF statements?

FeatureIF StatementEVALUATE Statement
PurposeUsed for simple or nested decision-makingUsed for multiple condition checking (similar to CASE or SWITCH)
ReadabilityBecomes complex with multiple conditionsMore structured and readable for multiple conditions
Multiple ConditionsRequires multiple AND / OR conditionsCan check multiple conditions in one line using ALSO
Range CheckingNeeds explicit comparison (>= and <=)Supports range (THRU) in conditions
Default Case HandlingRequires an explicit ELSE conditionUses WHEN OTHER to handle all unmatched cases
Nested ConditionsNested IF statements can become hard to readReduces deep nesting, making code cleaner