Strings Handling Interview Questions


What is the concept of COBOL string manipulation?

String handling statements are used to handle and manipulate strings. These statements are essential for processing textual data, allowing for modifications, evaluations, and extractions of character data within a COBOL program.

How do you handle character string manipulation in COBOL, and what are the different string manipulation verbs available?

They are -

  • STRING
  • UNSTRING
  • INSPECT

STRING Statement -

Explain string function in COBOL?

STRING statement concatenates two or more strings or literals into a single string and places them into a result variable.

What are the different options available for string concatenation?

  • DELIMITED BY - Specifies the character to place at the end of the source item. If SIZE is used, it'll consider the entire variable.
  • ON OVERFLOW statements-block-1 - Specifies the statements block executed when ON OVERFLOW occurs.
  • NOT ON OVERFLOW statement-block-2 - Specifies the statements block executed when the STRING operation is successful.

UNSTRING Statement -

What is the unstring usage in COBOL?

UNSTRING statement takes a single string, breaks it down into several separate strings, and places them into the variables.

What are the different options available in UNSTRING statement?

  • DELIMITED BY delimiter1 - The delimiter1 is used to specify where to split the string. If it's not coded, then each character is considered separately.
  • INTO target-item-1 [target-item-2 ...] - Specifies the target variables where the divided strings should be placed.
  • TALLYING IN - Count the number of characters transferred to the target items.
  • ON OVERFLOW statements-block-1 - Specifies the statements block executed when ON OVERFLOW occurs.
  • NOT ON OVERFLOW statement-block-2 - Specifies the statements block executed when the STRING operation is successful.

INSPECT Statement -

Inspect statement importance in COBOL?

INSPECT statement analyzes, counts, or replaces specific character(s) within a string. It is flexible and provides a range of functions to help with string manipulations.

What are the different types of pattern matching and data manipulation options available?

INSPECT statement has four formats -

  • INSPECT...TALLYING
  • INSPECT...REPLACING
  • INSPECT...TALLYING...REPLACING
  • INSPECT CONVERTING

What is the output of following program?

IDENTIFICATION DIVISION.
PROGRAM-ID. INSPEXAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CNT    PIC 9(02) VALUE 0.
01 WS-STRING PIC X(15) VALUE 'AABCDACDCCEDDDF'.
PROCEDURE DIVISION.
    INSPECT WS-STRING TALLYING WS-CNT FOR ALL 'D'.
    DISPLAY "WS-CNT:  " WS-CNT.
STOP RUN.

WS-CNT: 05

Common Questions -

Define DELIMITED BY SPACE and DELIMITED BY SIZE along with their utilization?

DELIMITED BY SPACE specifies the SPACE to place at the end of the source item.
DELIMITED BY SIZE it'll consider the entire variable and delimter will be placed at the end of the variable data.

Frequently Asked Questions -

What is the purpose of the string keyword in COBOL?

What is the use of string deletion by with pointer and unstring?

How do you use the inspect verb in COBOL?
Explain the COBOL verb INSPECT and list some typical uses?
What is the purpose of the INSPECT verb in COBOL, and what are some common scenarios where it is used?

INSPECT and EXAMINE?