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.

What is the Purpose of Pointer Phrase in STRING command?

WITH POINTER phrase provides an integer data item that indicates the leftmost character position within the receiving field.

Define overflow statement in COBOL string manipulation?

ON OVERFLOW specifies the statements block executed when the overflow occurs when strings are concatenated.

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.

What is the ON OVERFLOW phase usage in UNSTRING statement?

ON OVERFLOW specifies the statements block executed when the overflow occurs when strings are separated.

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 INSPECT..TALLYING?

INSPECT TALLYING counts the occurrences of the specific characters in the input string.

What is INSPECT..REPLACING?

INSPECT...REPLACING replaces the replacing string with the replacing string in the source string.

What is INSPECT..TALLYING…REPLACING?

INSPECT TALLYING REPLACING counts the occurrences of the specific characters and replaces them with new characters.

What is INSPECT..CONVERTING?

INSPECT...CONVERTING performs character conversion in a variable. It's a way to convert all instances of certain characters with other characters.

What is INSPECT..TALLYING..ALL?

Each occurrence of tallying character is counted when ALL alone coded (without BEFORE or AFTER phrases).

What is INSPECT..TALLYING..LEADING?

Leftmost occurrence of ws-tally-chars is counted when LEADING alone coded (without BEFORE or AFTER phrases).

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 pointer option in COBOL string manipulation?

What is the ON OVERFLOW phase usage in STRING statement?

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?