Interview

20 COBOL Interview Questions and Answers

Prepare for your next interview with our comprehensive guide to COBOL, featuring expert insights and common questions to boost your confidence.

COBOL, an acronym for Common Business-Oriented Language, has been a cornerstone in the world of business computing for decades. Despite its age, COBOL remains crucial for many legacy systems, particularly in sectors like finance, government, and insurance. Its robustness and reliability have ensured its continued use, making proficiency in COBOL a valuable skill for maintaining and upgrading these critical systems.

This article offers a curated selection of COBOL interview questions designed to help you demonstrate your expertise and problem-solving abilities. By familiarizing yourself with these questions, you can confidently showcase your knowledge and readiness to tackle the challenges associated with COBOL-based projects.

COBOL Interview Questions and Answers

1. Describe the structure of a COBOL program.

A COBOL program is divided into four main divisions, each serving a specific purpose:

  • Identification Division: Contains metadata about the program, such as the program name and author, primarily for documentation.
  • Environment Division: Specifies the environment in which the program will run, including configuration details like the computer and operating system, as well as any external files or databases the program will interact with.
  • Data Division: Used to define the data structures and variables that the program will use. It includes sections like the File Section, Working-Storage Section, and Linkage Section.
  • Procedure Division: Contains the executable instructions and is divided into paragraphs and sections to organize the code into logical units.

2. Explain the purpose of the IDENTIFICATION DIVISION.

The IDENTIFICATION DIVISION is the first and mandatory division of a COBOL program, serving as a header that provides metadata about the program. It includes several paragraphs, such as PROGRAM-ID, AUTHOR, INSTALLATION, DATE-WRITTEN, DATE-COMPILED, SECURITY, and REMARKS, each serving a specific purpose.

Example:

IDENTIFICATION DIVISION.
PROGRAM-ID. SampleProgram.
AUTHOR. John Doe.
INSTALLATION. XYZ Corporation.
DATE-WRITTEN. 2023-10-01.
DATE-COMPILED. 2023-10-02.
SECURITY. Confidential.
REMARKS. This is a sample COBOL program.

3. How do you define variables in the DATA DIVISION?

In COBOL, the DATA DIVISION is where variables are defined. It is divided into sections like the WORKING-STORAGE SECTION and the FILE SECTION. Variables are defined using a hierarchical level-number structure, indicating the relationship between different data items.

Example:

DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER-RECORD.
   05 CUSTOMER-ID        PIC 9(5).
   05 CUSTOMER-NAME      PIC A(30).
   05 CUSTOMER-BALANCE   PIC 9(7)V99.

In this example, the level number 01 indicates the beginning of a new data structure called CUSTOMER-RECORD, and the level number 05 indicates subordinate data items within CUSTOMER-RECORD.

4. What is the difference between LEVEL 01 and LEVEL 77 in the DATA DIVISION?

In the DATA DIVISION, LEVEL 01 and LEVEL 77 are used to define different types of data items. LEVEL 01 is used for group items, which can contain multiple subordinate data items, allowing for complex data organization. LEVEL 77 is used for standalone elementary items that are not part of any group.

5. Explain the use of the PERFORM statement.

The PERFORM statement in COBOL is used to execute a block of code multiple times, either iteratively or conditionally. Variations include PERFORM UNTIL, PERFORM TIMES, and PERFORM VARYING.

Example:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PerformExample.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 COUNTER PIC 9(02) VALUE 0.
       PROCEDURE DIVISION.
           PERFORM UNTIL COUNTER = 5
               ADD 1 TO COUNTER
               DISPLAY 'Counter: ' COUNTER
           END-PERFORM
           STOP RUN.

6. What are the different types of PERFORM statements?

There are several types of PERFORM statements in COBOL:

  • PERFORM THRU: Executes a range of paragraphs from a starting paragraph to an ending paragraph.
  • PERFORM TIMES: Executes a paragraph or section a specified number of times.
  • PERFORM UNTIL: Executes a paragraph or section until a specified condition is met.
  • PERFORM VARYING: Executes a paragraph or section with a loop that varies a control variable.

Example:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PerformExample.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 COUNTER PIC 9(02) VALUE 0.
       PROCEDURE DIVISION.
           PERFORM 10 TIMES
               DISPLAY 'PERFORM TIMES: ' COUNTER
               ADD 1 TO COUNTER
           END-PERFORM

           PERFORM UNTIL COUNTER > 15
               DISPLAY 'PERFORM UNTIL: ' COUNTER
               ADD 1 TO COUNTER
           END-PERFORM

           PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > 5
               DISPLAY 'PERFORM VARYING: ' COUNTER
           END-PERFORM

           STOP RUN.

7. How do you read from and write to files?

File handling in COBOL involves defining the file structure, opening the file, performing read or write operations, and then closing the file. COBOL supports various file organizations such as sequential, indexed, and relative.

Example:

IDENTIFICATION DIVISION.
PROGRAM-ID. FileHandlingExample.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT InputFile ASSIGN TO 'input.txt'
        ORGANIZATION IS LINE SEQUENTIAL.
    SELECT OutputFile ASSIGN TO 'output.txt'
        ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD  InputFile.
01  InputRecord PIC X(100).

FD  OutputFile.
01  OutputRecord PIC X(100).

WORKING-STORAGE SECTION.
01  WS-EOF PIC X VALUE 'N'.

PROCEDURE DIVISION.
    OPEN INPUT InputFile
    OPEN OUTPUT OutputFile

    PERFORM UNTIL WS-EOF = 'Y'
        READ InputFile INTO InputRecord
            AT END
                MOVE 'Y' TO WS-EOF
            NOT AT END
                MOVE InputRecord TO OutputRecord
                WRITE OutputRecord
        END-READ
    END-PERFORM

    CLOSE InputFile
    CLOSE OutputFile
    STOP RUN.

8. Explain the use of the SORT verb.

The SORT verb in COBOL is used to sort records in a file based on one or more keys, in ascending or descending order.

Example:

IDENTIFICATION DIVISION.
PROGRAM-ID. SortExample.

DATA DIVISION.
FILE SECTION.
FD  INPUT-FILE
    LABEL RECORDS ARE STANDARD
    DATA RECORD IS IN-REC.
01  IN-REC.
    05  IN-NAME     PIC X(20).
    05  IN-AGE      PIC 9(2).

FD  OUTPUT-FILE
    LABEL RECORDS ARE STANDARD
    DATA RECORD IS OUT-REC.
01  OUT-REC.
    05  OUT-NAME    PIC X(20).
    05  OUT-AGE     PIC 9(2).

SD  SORT-FILE
    DATA RECORD IS SORT-REC.
01  SORT-REC.
    05  SORT-NAME   PIC X(20).
    05  SORT-AGE    PIC 9(2).

PROCEDURE DIVISION.
    OPEN INPUT INPUT-FILE
    OPEN OUTPUT OUTPUT-FILE

    SORT SORT-FILE
        ON ASCENDING KEY SORT-NAME
        INPUT PROCEDURE IS Read-Input
        OUTPUT PROCEDURE IS Write-Output

    CLOSE INPUT-FILE
    CLOSE OUTPUT-FILE
    STOP RUN.

Read-Input.
    PERFORM UNTIL EOF
        READ INPUT-FILE INTO IN-REC
        AT END
            SET EOF TO TRUE
        NOT AT END
            MOVE IN-REC TO SORT-REC
            RELEASE SORT-REC
    END-PERFORM.

Write-Output.
    RETURN SORT-FILE INTO OUT-REC
    AT END
        GO TO End-Write
    NOT AT END
        WRITE OUT-REC
        GO TO Write-Output.

End-Write.

9. What is the purpose of the EVALUATE statement?

The EVALUATE statement in COBOL is used for conditional processing, similar to a switch-case statement in other programming languages. It allows for multiple conditions to be evaluated in a structured and readable manner.

Example:

EVALUATE TRUE
    WHEN condition-1
        PERFORM action-1
    WHEN condition-2
        PERFORM action-2
    WHEN OTHER
        PERFORM default-action
END-EVALUATE

10. How do you use subprograms?

In COBOL, subprograms are used to modularize code, making it more organized and easier to maintain. The main program can call these subprograms using the CALL statement, and data can be passed between the main program and subprograms using the USING clause.

Example:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MainProgram.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1 PIC 9(2) VALUE 10.
       01 WS-NUM2 PIC 9(2) VALUE 20.
       01 WS-RESULT PIC 9(4).
       PROCEDURE DIVISION.
           CALL 'SubProgram' USING WS-NUM1 WS-NUM2 WS-RESULT.
           DISPLAY 'Result: ' WS-RESULT.
           STOP RUN.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SubProgram.
       DATA DIVISION.
       LINKAGE SECTION.
       01 LK-NUM1 PIC 9(2).
       01 LK-NUM2 PIC 9(2).
       01 LK-RESULT PIC 9(4).
       PROCEDURE DIVISION USING LK-NUM1 LK-NUM2 LK-RESULT.
           COMPUTE LK-RESULT = LK-NUM1 + LK-NUM2.
           EXIT PROGRAM.

11. Describe the LINKAGE SECTION.

The LINKAGE SECTION in COBOL is used to define data items that are passed to a program from another program or from a calling program to a called subprogram. It allows for the sharing of data between different parts of a COBOL application.

Example:

IDENTIFICATION DIVISION.
PROGRAM-ID. SubProgram.

DATA DIVISION.
LINKAGE SECTION.
01 Passed-Data.
   05 Data-Item-1 PIC X(10).
   05 Data-Item-2 PIC 9(5).

PROCEDURE DIVISION USING Passed-Data.
   DISPLAY "Data Item 1: " Data-Item-1
   DISPLAY "Data Item 2: " Data-Item-2
   EXIT PROGRAM.

12. How do you pass parameters to a subprogram?

In COBOL, parameters are passed to a subprogram using the CALL statement. The parameters can be passed by reference or by content.

Example:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MainProgram.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUMBER1 PIC 9(4) VALUE 1000.
       01 WS-NUMBER2 PIC 9(4) VALUE 2000.

       PROCEDURE DIVISION.
           CALL 'SubProgram' USING WS-NUMBER1 WS-NUMBER2.
           STOP RUN.
       END PROGRAM MainProgram.
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SubProgram.

       DATA DIVISION.
       LINKAGE SECTION.
       01 LS-NUMBER1 PIC 9(4).
       01 LS-NUMBER2 PIC 9(4).

       PROCEDURE DIVISION USING LS-NUMBER1 LS-NUMBER2.
           DISPLAY 'Number 1: ' LS-NUMBER1.
           DISPLAY 'Number 2: ' LS-NUMBER2.
           EXIT PROGRAM.
       END PROGRAM SubProgram.

13. How do you handle tables (arrays)?

In COBOL, tables (arrays) are defined using the OCCURS clause, which specifies the number of times a data item is repeated. Tables can be single-dimensional or multi-dimensional.

Example:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TableExample.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  STUDENT-TABLE.
           05  STUDENT-RECORD OCCURS 10 TIMES.
               10  STUDENT-ID    PIC 9(5).
               10  STUDENT-NAME  PIC A(20).

       PROCEDURE DIVISION.
       INITIALIZE-TABLE.
           PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
               MOVE I TO STUDENT-ID (I)
               MOVE "STUDENT" TO STUDENT-NAME (I)
           END-PERFORM.

       DISPLAY-TABLE.
           PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
               DISPLAY "ID: " STUDENT-ID (I) " NAME: " STUDENT-NAME (I)
           END-PERFORM.

       STOP RUN.

14. Describe the SEARCH and SEARCH ALL statements.

The SEARCH and SEARCH ALL statements in COBOL are used for searching within tables (arrays). The SEARCH statement is used for a linear search, while the SEARCH ALL statement is used for a binary search, which requires the table to be sorted.

Example:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SearchExample.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  TABLE.
           05  ITEM OCCURS 10 TIMES INDEXED BY IDX.
               10  ITEM-NUMBER    PIC 9(2).
       01  SEARCH-KEY              PIC 9(2) VALUE 5.
       01  RESULT                  PIC X(10).

       PROCEDURE DIVISION.
           PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > 10
               MOVE IDX TO ITEM(IDX)
           END-PERFORM.

           SEARCH ITEM
               AT END
                   MOVE "NOT FOUND" TO RESULT
               WHEN ITEM(IDX) = SEARCH-KEY
                   MOVE "FOUND" TO RESULT
           END-SEARCH.

           DISPLAY RESULT.

           SORT ITEM ASCENDING KEY ITEM-NUMBER.

           SEARCH ALL ITEM
               AT END
                   MOVE "NOT FOUND" TO RESULT
               WHEN ITEM(IDX) = SEARCH-KEY
                   MOVE "FOUND" TO RESULT
           END-SEARCH.

           DISPLAY RESULT.

           STOP RUN.

15. What are some best practices for writing efficient code?

When writing efficient COBOL code, consider the following best practices:

  • Structured Programming: Use clear and consistent indentation, meaningful variable names, and modular code with well-defined sections.
  • Efficient Data Handling: Minimize the use of large data structures and avoid unnecessary data movement.
  • Optimize I/O Operations: Use efficient file handling techniques, such as block reads and writes.
  • Minimize Computational Overhead: Avoid redundant calculations and use efficient algorithms.
  • Code Reusability: Write reusable code by creating subroutines and functions for common tasks.
  • Testing and Debugging: Regularly test and debug your code to identify and fix performance bottlenecks.
  • Documentation: Provide clear and concise documentation for your code.

16. Explain how COBOL interacts with databases.

COBOL interacts with databases primarily through embedded SQL and database management systems (DBMS). COBOL programs can include SQL statements that are executed by the DBMS, allowing the program to perform operations such as querying, updating, and managing data.

Example of embedded SQL in COBOL:

       EXEC SQL
           SELECT EMPLOYEE_NAME, EMPLOYEE_SALARY
           INTO :EMP-NAME, :EMP-SALARY
           FROM EMPLOYEE
           WHERE EMPLOYEE_ID = :EMP-ID
       END-EXEC.

17. Explain the purpose of the WORKING-STORAGE SECTION.

The WORKING-STORAGE SECTION in COBOL is used to declare variables and constants that are required during the execution of a program. This section is part of the Data Division and is essential for defining data items that need to persist throughout the program’s execution.

Example:

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUMBER        PIC 9(4) VALUE 0.
01 WS-NAME          PIC A(20).
01 WS-TOTAL         PIC 9(5)V99 VALUE 0.

18. What are the different types of data items in COBOL?

In COBOL, data items are categorized into several types based on their usage and characteristics. The primary types of data items in COBOL are:

  • Elementary Data Items: Basic data items that cannot be further divided.
  • Group Data Items: Composed of one or more elementary data items.
  • Numeric Data Items: Used to store numeric values.
  • Alphabetic Data Items: Used to store alphabetic characters.
  • Alphanumeric Data Items: Can store both alphabetic characters and numeric digits.
  • Edited Data Items: Used to format and display data in a specific way.

19. How do you use the INSPECT verb?

The INSPECT verb in COBOL is used for counting occurrences of specific characters within a string and replacing characters within a string.

Example:

IDENTIFICATION DIVISION.
PROGRAM-ID. InspectExample.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  InputString     PIC X(20) VALUE 'HELLO WORLD'.
01  Count           PIC 9(4) VALUE 0.

PROCEDURE DIVISION.
    INSPECT InputString TALLYING Count FOR ALL 'L'.
    DISPLAY 'Number of Ls: ' Count.

    INSPECT InputString REPLACING ALL 'L' BY 'X'.
    DISPLAY 'Modified String: ' InputString.

    STOP RUN.

20. What is the significance of the PROCEDURE DIVISION?

The PROCEDURE DIVISION in COBOL is where the program’s logic is implemented. It contains the executable statements that perform the operations defined by the program.

Example:

IDENTIFICATION DIVISION.
PROGRAM-ID. HelloWorld.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-MESSAGE PIC X(20) VALUE 'Hello, World!'.

PROCEDURE DIVISION.
DISPLAY-MESSAGE.
    DISPLAY WS-MESSAGE.
    STOP RUN.

In this example, the PROCEDURE DIVISION contains a single paragraph, DISPLAY-MESSAGE, which displays a message stored in the WORKING-STORAGE SECTION.

Previous

10 SQS Testing Interview Questions and Answers

Back to Interview
Next

10 SOAP UI Tool Interview Questions and Answers