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.
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.
A COBOL program is divided into four main divisions, each serving a specific purpose:
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.
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.
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.
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.
There are several types of PERFORM statements in COBOL:
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.
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.
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.
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
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.
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.
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.
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.
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.
When writing efficient COBOL code, consider the following best practices:
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.
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.
In COBOL, data items are categorized into several types based on their usage and characteristics. The primary types of data items in COBOL are:
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.
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.