Advanced COBOL Interview Questions: Mastering File Handling & Program Structure

This guide delves into advanced COBOL concepts, focusing on file handling, program structure, and data manipulation techniques. We explore complex topics such as `INPUT PROCEDURE` and `OUTPUT PROCEDURE` in the `SORT` statement, the use of the `LINKAGE SECTION`, and different access modes for the `START` statement. This resource provides detailed answers to challenging COBOL interview questions, covering advanced data manipulation, search algorithms (binary vs. sequential), and the intricacies of COBOL's program structure. Prepare for in-depth questions on COBOL's features, its continued relevance in modern systems, and best practices for writing efficient and maintainable COBOL code.



Top COBOL Interview Questions and Answers

What is COBOL?

COBOL (Common Business-Oriented Language) is a high-level programming language primarily used for business applications. Created in the late 1950s, it's known for its ability to handle large amounts of data and its readability (using English-like statements). While older, it remains relevant in many legacy systems.

Is COBOL Outdated?

COBOL, while an older language, is far from outdated. Many large organizations still rely on COBOL systems, and there's ongoing demand for COBOL programmers to maintain and modernize these systems. Government agencies and financial institutions, in particular, frequently use COBOL.

History of COBOL

Developed in 1959 by the CODASYL (Conference on Data Systems Languages) committee, COBOL aimed to create a business-oriented programming language that was easy to read and use by business professionals. Its English-like syntax made it accessible.

COBOL Data Types

  • Alphanumeric (X): Can contain letters, numbers, and special characters.
  • Alphabetic (A): Contains only letters.
  • Numeric (9): Contains only numbers.

Subscript vs. Index in COBOL

A subscript directly refers to the position of an element within an array. An index represents the displacement (offset) from the start of the array. Indexes are modified using commands like SET, while subscripts are used directly to access array elements.

Key Features of COBOL

  • Standard Language: Runs on various platforms.
  • Business-Oriented: Handles large datasets efficiently.
  • Structured Programming: Improves code organization and maintainability.
  • Debugging Tools: Facilitates easier error detection.

COBOL Program Divisions

  • IDENTIFICATION DIVISION: Program identification and details.
  • ENVIRONMENT DIVISION: Specifies the hardware and software environment.
  • DATA DIVISION: Defines variables and data structures.
  • PROCEDURE DIVISION: Contains the program's logic.

Section vs. Paragraph in COBOL

A section groups related paragraphs within a COBOL program. A paragraph is the smallest executable unit; it contains one or more statements.

IS NUMERIC Clause

The IS NUMERIC clause checks if a variable contains only numeric characters (0-9), returning true if it does.

Level 66 and Level 88 in COBOL

  • Level 66: Used for the RENAMES clause (to create alternative names for data items).
  • Level 88: Used for condition names (to give names to specific conditions within a program).

Scope Terminators in COBOL

Scope terminators (e.g., END-IF, END-PERFORM) mark the end of a specific block of code.

SEARCH vs. SEARCH ALL

SEARCH SEARCH ALL
Sequential search (linear search); no sorting required. Binary search; requires the array to be sorted.

NEXT SENTENCE vs. CONTINUE

NEXT SENTENCE CONTINUE
Transfers control to the next statement. Continues execution with the next statement in the current block.

COBOL File Opening Modes

  • INPUT: For reading data.
  • OUTPUT: For writing data.
  • I-O (Input-Output): For both reading and writing.
  • EXTEND: For appending data to an existing file.

Maximum Size of Numeric Field in COBOL

The maximum size for a numeric field is typically defined as PIC 9(18), though this may vary based on the compiler and platform.

Static vs. Dynamic Linking in COBOL

Static Linking Dynamic Linking
Subroutines are linked directly into the main program during compilation. Subroutines are loaded at runtime.

COBOL as a Business Language

COBOL's strengths as a business language include its ability to handle large datasets, its readability, its support for various platforms, and the availability of tools for debugging and testing.

EVALUATE Statement in COBOL

The EVALUATE statement provides a structured way to handle multiple conditions, similar to a CASE statement in other languages.

Guidelines for Structured COBOL Programming

Structured COBOL programming emphasizes modularity, readability, and maintainability. Use clear naming conventions, scope terminators, inline PERFORM statements, and appropriate conditional statements (IF-THEN-ELSE).

PERFORM ... WITH TEST BEFORE vs. PERFORM ... WITH TEST AFTER

PERFORM ... WITH TEST BEFORE PERFORM ... WITH TEST AFTER
The condition is checked before each iteration; the loop may not execute at all. The condition is checked after each iteration; the loop executes at least once.

COPY Statement with REPLACING

The COPY statement copies code from another file. The REPLACING option allows you to substitute values within the copied code.

Scope Terminators (Reiterated)

Scope terminators clearly define the beginning and end of code blocks (e.g., IF ... END-IF, PERFORM ... END-PERFORM).

OCCURS Clause

The OCCURS clause is used to define arrays; it's not used at the 01 level (record level) because you can't repeat entire records directly.

ON SIZE ERROR in COBOL

The ON SIZE ERROR clause handles numeric errors like overflow, division by zero, and other arithmetic exceptions.

Structured vs. Object-Oriented COBOL

Structured COBOL Object-Oriented COBOL
Emphasizes modularity and control flow using structured programming constructs. Uses object-oriented programming principles (classes, objects, inheritance, polymorphism).

File Opening Mode for Writing

Use OUTPUT or EXTEND mode to open a file for writing (OUTPUT overwrites; EXTEND appends).

Sorting Order for SEARCH ALL

For SEARCH ALL, the table must be sorted in either ascending (default) or descending order (using ASCENDING KEY or DESCENDING KEY).

Call by Content vs. Call by Reference

Call by Content Call by Reference
A copy of the data is passed; the original data is not modified. The memory address is passed; the original data can be modified.

INPUT PROCEDURE and OUTPUT PROCEDURE in COBOL

INPUT PROCEDURE and OUTPUT PROCEDURE are used with the SORT verb to perform custom processing of records before or after the sorting operation.

INPUT PROCEDURE Example (Conceptual)

RELEASE record-name FROM input-record.  // Sends a record to the sort
        
OUTPUT PROCEDURE Example (Conceptual)

RETURN file RECORD into output-record.  // Retrieves a record from the sort
        

Call by Content vs. Call by Reference in COBOL

Call by Content Call by Reference
A copy of the data is passed to the subroutine; the original data cannot be modified. The memory address of the data is passed; the subroutine can modify the original data.

LINKAGE SECTION in COBOL

The LINKAGE SECTION is used to pass data between programs or procedures. It defines variables that will receive data from a calling program.

START Statement Access Modes

  • SEQUENTIAL
  • DYNAMIC

Sections in the COBOL DATA DIVISION

  • FILE SECTION
  • WORKING-STORAGE SECTION
  • LOCAL-STORAGE SECTION
  • SCREEN SECTION
  • REPORT SECTION
  • LINKAGE SECTION

Mandatory COBOL Divisions

The IDENTIFICATION DIVISION and PROCEDURE DIVISION are mandatory for all COBOL programs.

COBOL Program Divisions

  • IDENTIFICATION DIVISION: Program identification information.
  • ENVIRONMENT DIVISION: Specifies the hardware and software environment (optional).
  • DATA DIVISION: Defines data structures and variables.
  • PROCEDURE DIVISION: Contains the program's logic (mandatory).

GOBACK, STOP RUN, EXIT PROGRAM

GOBACK STOP RUN EXIT PROGRAM
Returns control to the calling program. Terminates the current program and returns control to the operating system. Exits a called program and returns control to the calling program.

COMP vs. COMP-3 in COBOL

COMP COMP-3
Binary data representation. Packed decimal data representation.

DELIMITED BY SPACE vs. DELIMITED BY SIZE

These clauses control how data is handled during string operations:

  • DELIMITED BY SPACE: Treats spaces as delimiters.
  • DELIMITED BY SIZE: Uses the entire field length as the delimiter.

REDEFINES Clause

The REDEFINES clause allows multiple variable names to refer to the same storage location. This is useful for representing data in different ways.

REWRITE and File Opening Mode

To use REWRITE, the file must be opened in I-O (input/output) mode because you need to read the record before rewriting it.

IN-LINE PERFORM

An IN-LINE PERFORM allows you to embed a block of code within a PERFORM statement, improving code readability and organization. It avoids the need for separate paragraphs for short routines.

Pointer in STRING Statement

The pointer in a STRING statement specifies the starting position within the target field where the data will be placed.

Global vs. External Variables

Global Variables External Variables
Accessible only within the current program. Accessible from multiple programs within the same program library.

Binary Search vs. Sequential Search

Binary Search Sequential Search
Requires a sorted array. More efficient for larger arrays. Uses SEARCH ALL. No sorting required. Less efficient for larger arrays. Uses SEARCH.

PIC 9V99

PIC 9V99 defines a three-digit numeric field with an implied decimal point after the first digit.

PIC 9.99 vs. PIC 9V99

PIC 9.99 PIC 9V99
Four-digit field with an explicit decimal point. Three-digit field with an implied decimal point.

Rules for SEARCH

A SEARCH statement requires the table to have an OCCURS clause and be indexed (using INDEXED BY). The index must be initialized.

Getting the Current Date with Century

Use the FUNCTION CURRENT-DATE to obtain the current date including the century.

SSRANGE and NOSSRANGE

These compiler options control how subscript out-of-range errors are handled. NOSSRANGE (default) suppresses runtime errors; SSRANGE causes errors to be reported.

Data Division Sections

The COBOL DATA DIVISION has six sections.

CALL vs. LINK in COBOL

CALL LINK
COBOL verb; used to invoke a program and return control. Similar to CALL but not a COBOL verb; used for linking programs.

COMP vs. COMP-3 (Reiterated)

COMP represents binary data, and COMP-3 represents packed decimal data.

SORT Operation Rules

The SORT statement requires that input and output files are initially closed, and a working storage file must be defined.

Searching a Table with/without Index

A SEARCH statement requires an index; a sequential search is possible without an index but is less efficient.