75% found this document useful (4 votes)
1K views24 pages

TOP 40 COBOL Interview Questions PDF

The document discusses COBOL interview questions and answers. It provides details on how to define and access tables in COBOL using the OCCURS clause. It also explains packed decimal (COMP-3) fields and how they are more efficient for storing numbers. Additionally, it covers using the INSPECT statement to validate, replace, or convert character data.

Uploaded by

Narayan Mahato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
75% found this document useful (4 votes)
1K views24 pages

TOP 40 COBOL Interview Questions PDF

The document discusses COBOL interview questions and answers. It provides details on how to define and access tables in COBOL using the OCCURS clause. It also explains packed decimal (COMP-3) fields and how they are more efficient for storing numbers. Additionally, it covers using the INSPECT statement to validate, replace, or convert character data.

Uploaded by

Narayan Mahato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

TOP 40 COBOL interview questions

Anyone who is working with COBOL expected to know few topics which are must known. In any COBOL
interview, one can expect to have questions asked on these topics.

Part-I

1. How table is defined in COBOL and what are the different ways table entries are
accessed?
OR, How Do You Define a Table/array In Cobol?

A. OCCURS is used to define table and table entries are accessed either by using Subscript or index.
-Table is a collection of data items or an array of elements.
-OCCURS clause is used in COBOL to define a table.

Sample 1: Simple table with one field repeating 3 times


Working storage:
01 WS-SAMPLE-TABLE.
05 WS-SAMPLE-TABLE-FIELD1 PIC 9(05) OCCURS 3 TIMES.
Procedure division:

MOVE 0 TO WS-SAMPLE-TABLE-FIELD1(1)
MOVE 12345 TO WS-SAMPLE-TABLE-FIELD1(2)
MOVE 98765 TO WS-SAMPLE-TABLE-FIELD1(3)
DISPLAY ‘WS-SAMPLE-TABLE-FIELD1(1):’
WS-SAMPLE-TABLE-FIELD1(1)
DISPLAY ‘WS-SAMPLE-TABLE-FIELD1(2):’
WS-SAMPLE-TABLE-FIELD1(2)
DISPLAY ‘WS-SAMPLE-TABLE-FIELD1(3):’
WS-SAMPLE-TABLE-FIELD1(3)

Output:
WS-SAMPLE-TABLE-FIELD1(1):00000
WS-SAMPLE-TABLE-FIELD1(2):12345
WS-SAMPLE-TABLE-FIELD1(3):98765

Can the OCCURS clause be at the 01 level? No.

Why occurs can’t not be used in 01 level?


A. Because, Occurs clause is there to repeat fields with same format, not the records.

2. What is COMP -3 field and what is the use of it?


A. COMP-3 is packed decimal representation of data and is used to save space.
-It occupies 1 byte for two decimal digits
-Right most bit is used to represent sign
-Efficient usage of space when odd number of bytes is present.
- A byte is comprised of 8 bits. Those 8 bits are split into two parts (or nibbles). The decimal
digits are stored in these nibbles (upper and lower). The lower nibble of the last byte of the field
will hold the sign (+, - or unsigned).

Declaration: PIC S9 (5) usage is computational-3.


Or, PIC S9 (5) comp-3.

Calculation: PIC S9 (7) COMP-3. Byte size = (7 + 1) / 2 = 4


PIC S9 (5) V99 COMP-3. Byte size = (5 + 2 + 1) / 2 = 4
PIC S9 (6) COMP-3. Byte size = (6 + 1) / 2 = 3.5, rounded to 4

Comp-3 fields reserve a nibble for the sign, even for "unsigned" values, so the following fields are still 4 bytes:
PIC 9(7) COMP-3. Byte size = (7 + 1) / 2 = 4

PIC 9(6) COMP-3. Byte size = (6 + 1) / 2 = 3.5, rounded to 4

Why COMP-1, COMP-2 don’t have PIC clause?


PIC creates relation between data name and data type. Whereas here data is numeric and length is predefined
So no relation is required using PIC clause.

Sample 1:
Working storage variables declared
01 VAR-W PIC 9(10).
01 VAR-W-BINARY PIC 9(10)V9(2) USAGE BINARY. <--also known as COMP and COMP-4
01 VAR-W-COMP1 COMP-1. <--short floating format. 4 bytes
01 VAR-W-COMP2 COMP-2. <--long floating format. 8 bytes
01 VAR-W-COMP3 PIC 9(10) USAGE COMP-3. <--Packed decimal. 1 byte for two decimal digits

Procedure division statements:


MOVE 12345678 TO VAR-W
MOVE VAR-W TO VAR-W-BINARY
VAR-W-COMP1
VAR-W-COMP2
VAR-W-COMP3
DISPLAY ‘VAR-W:’ VAR-W
DISPLAY ‘VAR-W-BINARY:’ VAR-W-BINARY
DISPLAY ‘VAR-W-COMP1 :’ VAR-W-COMP1
DISPLAY ‘VAR-W-COMP2 :’ VAR-W-COMP2
DISPLAY ‘VAR-W-COMP3 :’ VAR-W-COMP3

Output:
VAR-W:0012345678
VAR-W-BINARY:00000000001234567800
VAR-W-COMP1 : .12345678E 08
VAR-W-COMP2 : .12345678000000000E 08
VAR-W-COMP3 :0012345678
Explanation:
VAR-W-COMP1 and VAR-W-COMP2 shows that there is no need to use PICTURE clause with COMP-1 and
COMP-2 declarations.
Though Binary and Packed decimal format values look same, the storage used is different.

Sample 2:
Working storage variables:
01 VAR-W PIC S9(10).
01 VAR-W-BINARY PIC S9(10) USAGE BINARY.
01 VAR-W-COMP1 COMP-1.
01 VAR-W-COMP2 COMP-2.
01 VAR-W-COMP3 PIC S9(10) USAGE COMP-3.

Procedure division statements:


MOVE 12345678 TO VAR-W
MOVE VAR-W TO VAR-W-BINARY
VAR-W-COMP1
VAR-W-COMP2
VAR-W-COMP3
DISPLAY ‘VAR-W:’ VAR-W
DISPLAY ‘VAR-W-BINARY:’ VAR-W-BINARY
DISPLAY ‘VAR-W-COMP1 :’ VAR-W-COMP1
DISPLAY ‘VAR-W-COMP2 :’ VAR-W-COMP2
DISPLAY ‘VAR-W-COMP3 :’ VAR-W-COMP3

Output:
VAR-W:001234567H
VAR-W-BINARY:0000000000012345678
VAR-W-COMP1 : .12345678E 08
VAR-W-COMP2 : .12345678000000000E 08
VAR-W-COMP3 :0012345678
Explanation:
This sample shows how signed variables displays positive values.

Sample 3:
Procedure division statements:
MOVE -12345678 TO VAR-W
MOVE VAR-W TO VAR-W-BINARY
VAR-W-COMP1
VAR-W-COMP2
VAR-W-COMP3
DISPLAY ‘VAR-W:’ VAR-W
DISPLAY ‘VAR-W-BINARY:’ VAR-W-BINARY
DISPLAY ‘VAR-W-COMP1 :’ VAR-W-COMP1
DISPLAY ‘VAR-W-COMP2 :’ VAR-W-COMP2
DISPLAY ‘VAR-W-COMP3 :’ VAR-W-COMP3

Output:
VAR-W:001234567Q
VAR-W-BINARY:000000000001234567Q
VAR-W-COMP1 :-.12345678E 08
VAR-W-COMP2 :-.12345678000000000E 08
VAR-W-COMP3 :001234567Q

Explanation:
This sample shows how the negative numbers are displayed for signed variables.

Sample 4:
Procedure division statements:
MOVE -12345678.1 TO VAR-W
MOVE VAR-W TO VAR-W-BINARY
VAR-W-COMP1
VAR-W-COMP2
VAR-W-COMP3
DISPLAY ‘VAR-W:’ VAR-W
DISPLAY ‘VAR-W-BINARY:’ VAR-W-BINARY
DISPLAY ‘VAR-W-COMP1 :’ VAR-W-COMP1
DISPLAY ‘VAR-W-COMP2 :’ VAR-W-COMP2
DISPLAY ‘VAR-W-COMP3 :’ VAR-W-COMP3

Output:
VAR-W:00123456781}
VAR-W-BINARY:000000000123456781}
VAR-W-COMP1 :-.12345678E 08
VAR-W-COMP2 :-.12345678100000000E 08
VAR-W-COMP3 :00123456781}
Explanation:
This sample shows how negative decimal number is displayed for signed variables.

3. Can we move Alphanumeric value to numeric variable?


A. Yes, we can move Alphanumeric to Numeric and it will not create any abend as long as it has
characters or numeric values. Anyway just moving an alphanumeric value into numeric field
doesn’t cause any abend. But issue might happen only if the numeric field is used for
calculation.

Sample 1:
Working storage variables:
01 VAR-W PIC X(10).
01 VAR-W-NUM PIC 9(10).

Procedure division statements:


MOVE ‘CHECK’ TO VAR-W
MOVE VAR-W TO VAR-W-NUM
DISPLAY ‘VAR-W:’ VAR-W
DISPLAY ‘VAR-W-NUM:’ VAR-W-NUM

Output:
VAR-W: CHECK
VAR-W-NUM: CHECK 0

The above example shows that just movement of any alphanumeric field with all characters into a numeric field
will not cause any S0C7 abend.

Sample 2:
Procedure division statements:
MOVE ‘CHECK’ TO VAR-W
MOVE VAR-W TO VAR-W-NUM
DISPLAY ‘VAR-W:’ VAR-W
DISPLAY ‘VAR-W-NUM:’ VAR-W-NUM
COMPUTE VAR-W-NUM = VAR-W-NUM + 1
DISPLAY ‘VAR-W-NUM :’ VAR-W-NUM

Output:
VAR-W:CHECK
VAR-W-NUM:CHECK 0
VAR-W-NUM :3853200001
The example shows that even if move alphanumeric field with all character data into numeric field and use that
numeric field in calculation will not cause any abend(S0C7 abend) . But the data may not be correct as for each
character default numeric value is considered.

Sample 3:
Procedure division statements:
MOVE ‘-12.3‘ TO VAR-W
MOVE VAR-W TO VAR-W-NUM
DISPLAY ‘VAR-W:’ VAR-W
DISPLAY ‘VAR-W-NUM:’ VAR-W-NUM
COMPUTE VAR-W-NUM = VAR-W-NUM + 1
DISPLAY ‘VAR-W-NUM :’ VAR-W-NUM

Output:
VAR-W:-12.3
VAR-W-NUM:-12.3 0
and then S0C7 abend due to due to “A decimal digit or sign was invalid”

The abend happened as we try to perform some computation on numeric data item which got the data copied
from alphanumeric.

To avoid this, we have to convert the alphanumeric value to numeric value and that can be done by using the
defined function NUMVAL in COBOL.

Sample 4:
Working storage variables declared:
01 VAR-W PIC X(5).
01 VAR-X PIC S9(3)V9(2).
01 VAR-Y PIC S9(3)V9(2).
01 VAR-Z-DIS PIC -9(3).9(2).

Procedure division statements:


DISPLAY ‘TEST PROGRAM’
MOVE ‘-12.3‘ TO VAR-W
COMPUTE VAR-X = FUNCTION NUMVAL(VAR-W)
COMPUTE VAR-Y = VAR-X + 5
MOVE VAR-X TO VAR-Z-DIS
DISPLAY ‘VALUE OF X:’ VAR-X
DISPLAY ‘VALUE OF Y:’ VAR-Y
DISPLAY ‘VALUE OF X IN DISPLAY FORMAT:’ VAR-Z-DIS

Output:
TEST PROGRAM
VALUE OF X:0123}
VALUE OF Y:0073}
VALUE OF X IN DISPLAY FORMAT:-012.30

4.What is INSPECT and the use of it?


A.INSPECT is a defined COBOL verb and is used to find occurrence of character or string appears
or to replace single/a set of characters from any data item.

Usage:
1. If any user id kind of variables are there in a program, then INSPECT is used to make sure
there are no spaces present in that variables. So INSPECT in COBOL is used as a kind of data
validation for a field in any program.
2. If you have a requirement where any variable needs to be read and to be replaced that with
some specific data, then also INSPECT can be used in COBOL.

Declaration:
1. INSPECT <$source string> TALLYING
<$counter Var1> FOR [ALL/LEADING] <$char1> [BEFORE/AFTER] <$char2>…
<$counter Var2> FOR [CHARACTERS] <$char1> [BEFORE/AFTER] <$char2>…
Ex,
inspect item tallying
count-0 for all "AB" before "BC"
count-1 for leading "B" after "D"
count-2 for characters after "A" before "C"

2. INSPECT <$source string> REPLACING


[ALL/LEADING/FIRST/CHARACTERS BY] <$char1> [BEFORE/AFTER] <$char2>…

Ex,
inspect item replacing
all "AB" by "XY" before "BC"
leading "B" by "W" after "D"
first "E" by "V" after "D"
characters by "Z" after "A" before "C"

3. INSPECT <$source string> TALLYING


<$counter Var1> FOR [ALL/LEADING] <$char1> [BEFORE/AFTER] <$char2>…
<$counter Var2> FOR [CHARACTERS] <$char1> [BEFORE/AFTER] <$char2>…
REPLACING
[ALL/LEADING/FIRST/CHARACTERS BY] <$char1> [BEFORE/AFTER]
<$char2>…

Ex,
inspect item tallying
count-0 for all "AB" before "BC"
replacing
all "AB" by "XY" before "BC"

4. . INSPECT <$source string> CONVERTING


<$char1> TO <$char2> [BEFORE/AFTER] <$char3>…

Ex,
inspect item coverting
"AB" to "12"
Or,
inspect item replacing
all "AB" to "12"

I/P:- ACCBDAB O/P:-1CC2D12

5. Can we use MAX or MIN with Alphanumeric data?


A. Yes, we can use MAX or MIN for Alphanumeric data as well.
MAX – Returns the Alphanumeric variable with highest length
MIN – Returns the Alphanumeric variable with smallest length
ORD-MAX – Returns the variable position of the highest length
ORD-MIN – Returns the variable position of the smallest length

Sample:
Variables declared as
01 VAR-W PIC X(10).
01 VAR-W1 PIC X(10).
01 VAR-W2 PIC X(10).
01 VAR-X PIC 9.

Procedure division statements:

MOVE ‘ABCDEFGHIJ’ TO VAR-W


MOVE ‘ABCDEFG ‘ TO VAR-W1
MOVE ‘ABCD’ TO VAR-W2
DISPLAY ‘MAXIMUM VALUE:’
FUNCTION MAX(VAR-W,VAR-W1,VAR-W2)
DISPLAY ‘MINIMUM VALUE:’
FUNCTION MIN(VAR-W,VAR-W1,VAR-W2)
COMPUTE VAR-X =
FUNCTION ORD-MAX(VAR-W,VAR-W1,VAR-W2)
DISPLAY ‘MAXIMUM VALUE POSITION:’ VAR-X
COMPUTE VAR-X =
FUNCTION ORD-MIN(VAR-W,VAR-W1,VAR-W2)
DISPLAY ‘MINIMUM VALUE POSITION:’ VAR-X

Output:
MAXIMUM VALUE:ABCDEFGHIJ
MINIMUM VALUE:ABCD
MAXIMUM VALUE POSITION:1
MINIMUM VALUE POSITION:3

6.How to find when the COBOL program was compiled?


A. We can find by using WHEN-COMPILED function or by searching the LOAD library.
There is a defined function in COBOL which can be used to find the compilation date and time
of any program.
The function used is WHEN-COMPILED and it returns 21 characters..

Sample:
Procedure division statement:
DISPLAY ‘PROGRAM COMPILED ON:’ FUNCTION WHEN-COMPILED
Output:
PROGRAM COMPILED ON:2013122412433857+0000

8.What is DELIMITED BY SIZE and DELIMITED BY SPACE and when they are used?
A.DELIMITED BY SIZE or DELIMITED BY SPACE will be used along with STRING to control how the
data is copied.
DELIMITED BY SIZE: Total size that is declared for the data items will be considered as delimiter.
DELIMITED BY SPACE: Space is considered as delimiter.

Sample 1:

Working storage variables:


01 VAR-STRING-CHECK PIC X(20) VALUE SPACES.
01 VAR-STRING-DISPLAY PIC X(30).

Procedure division statements:


MOVE ‘STRING CHECK’ TO VAR-STRING-CHECK
STRING ‘THIS IS FOR’ VAR-STRING-CHECK
DELIMITED BY SIZE INTO VAR-STRING-DISPLAY
DISPLAY ‘VAR-STRING-DISPLAY:’ VAR-STRING-DISPLAY

Output:
VAR-STRING-DISPLAY:THIS IS FORSTRING CHECK*******
Here ‘*’ means Spaces

Explanation:
As delimited by size is mentioned, all data items got copied to receiving data item.

Sample 2:
Procedure division statements:
MOVE ‘STRING CHECK’ TO VAR-STRING-CHECK
STRING ‘THIS IS FOR’ VAR-STRING-CHECK
DELIMITED BY SPACE INTO VAR-STRING-DISPLAY
DISPLAY ‘VAR-STRING-DISPLAY:’ VAR-STRING-DISPLAY

Output:
VAR-STRING-DISPLAY:THISSTRING

Explanation:
As delimited by space is mentioned,as soon as space is encountered the data item copy will be stopped.
in the first string’THIS IS FOR’, only THIS is copied and as space is there after that,the data item copy is
stopped.
Next data item has ‘STRING CHECK’ and after copying STRING,space is encountered and copy is stopped.
So output has become THISSTRING.

Sample 3:
We can even use any character as delimiter

Procedure division statements:


MOVE ‘STRING CHECK’ TO VAR-STRING-CHECK
STRING ‘THIS IS FOR’ VAR-STRING-CHECK
DELIMITED BY ‘C’ INTO VAR-STRING-DISPLAY
DISPLAY ‘VAR-STRING-DISPLAY:’ VAR-STRING-DISPLAY

Output:
VAR-STRING-DISPLAY:THIS IS FORSTRING

Explanation:
As character “C” is used as delimiter, data item copy is stopped as soon as C is encountered.

9. Any scenario which can result in Infinite loop by using PERFORM?


A. With PERFORM UNTIL and PERFORM TIMES, infinite loop can be produced.
Procedure division statements:
Main-para:
DISPLAY ‘TEST PROGRAM’
MOVE 1 TO VAR-X
PERFORM THIS-IS-TO-CHECK-PARA-LENGTH
UNTIL VAR-X > 3

THIS-IS-TO-CHECK-PARA-LENGTH Para:
DISPLAY ‘IN CHECK PARA’ .
PERFORM THIS-IS-TO-CHECK-PARA-LENGTH VAR-X TIMES
DISPLAY ‘VALUE OF VAR-X:’ VAR-X
ADD 1 TO VAR-X
DISPLAY ‘VALUE OF VAR-X:’ VAR-X

What will be the output?

Answer:
The code will result in an infinite loop.

Explanation:

Perform in the main para tries to process the paragraph once. But inside that paragraph another PERFORM is
there which tries to perform the same paragraph once again and the same repeats for ever resulting in an
infinite loop.

10.What is the use of TEST BEFORE and TEST AFTER in PERFORM statements?
A.
1.
Procedure division statements:
Main-para:
DISPLAY ‘TEST PROGRAM’
MOVE 1 TO VAR-X
PERFORM THIS-IS-TO-CHECK-PARA-LENGTH WITH TEST AFTER UNTIL VAR-X < 5

THIS-IS-TO-CHECK-PARA-LENGTH paragraph:
*———————
DISPLAY ‘IN CHECK PARA’ .
DISPLAY ‘VALUE OF VAR-X:’ VAR-X
ADD 1 TO VAR-X
DISPLAY ‘VALUE OF VAR-X:’ VAR-X

Output:
TEST PROGRAM
IN CHECK PARA
VALUE OF VAR-X:01
VALUE OF VAR-X:02
Explanation:
As TEST AFTER condition is mentioned with UNTIL, first paragraph is processed and then condition is
checked. So Paragraph processed once and then condition checked and it became true(1<5).So the paragraph
executed only once.

2.
If Perform is changed as below
PERFORM THIS-IS-TO-CHECK-PARA-LENGTH WITH TEST BEFORE
UNTIL VAR-X < 5

Output:
TEST PROGRAM

Explanation:
As TEST BEFORE condition is used, before coming into paragraph the condition is checked first. As the
condition is valid, execution didn’t go to paragraph.

3.
If PERFORM is coded as below
PERFORM THIS-IS-TO-CHECK-PARA-LENGTH
UNTIL VAR-X < 5

Output:
TEST PROGRAM

Explanation:
If there is no TEST condition, then by default TEST BEFORE is used. That’s why the paragraph was
not even executed once.

Part-II

1.When do you get S013 abend?


A. One of the reasons for this ABEND , is missing of any member name which the job is trying to refer.
If any job is referring some member of a PDS and if the job doesn’t find that member, then you can
expect to get S013 abend.

2.
Variables declared as
01 VAR-W PIC 9(2).
01 VAR-X PIC 9(2).
01 VAR-Y PIC 9(2).
01 VAR-Z PIC 9(2).

Procedure division statements:


MOVE 10 TO VAR-X
MOVE 15 TO VAR-Y
MOVE 20 TO VAR-Z
ADD VAR-X VAR-Y VAR-Z GIVING VAR-W
SUBTRACT VAR-X VAR-Y FROM VAR-Z

What will be the values of VAR-W and VAR-Z ?

Answer:
VAR-w = 45
VAR-Z = 05

3. What is the difference between PIC 9.99 and 9v99?


A. PIC 9.99 is a FOUR-POSITION field that actually contains a decimal point where as PIC 9v99 is
THREE- POSITION numeric field with implied or assumed decimal position.

1.
If variables are declared as below
01 VAR-X PIC 99V99.
01 VAR-Y PIC 9999V999.

Procedure division statements:

MOVE 12.35 TO VAR-X


MOVE VAR-X TO VAR-Y

What will be the output?

Solution:

VALUE OF X:1235
VALUE OF Y:0012350

As size of the variable Y is more than X , ZEROES are added .

2.
Variables are declared as below
01 VAR-X PIC 9(5)V9(2).
01 VAR-Y PIC 9(3)V9.

Procedure division statements:


DISPLAY ‘TEST PROGRAM’
MOVE 12345.56 TO VAR-X
MOVE VAR-X TO VAR-Y
DISPLAY ‘VALUE OF X:’ VAR-X
DISPLAY ‘VALUE OF Y:’ VAR-Y

Output produced is:


TEST PROGRAM
VALUE OF X:1234556
VALUE OF Y:3455

As variable Y is of smaller length, the value gets truncated.Left most digits got removed before decimal and
right most digits got removed after decimal.

3.
To see the decimal point in the display, the declaration to be as below
01 VAR-X PIC 9(5).9(2).
01 VAR-Y PIC 9(3).9.

Procedure division statements:


DISPLAY ‘TEST PROGRAM’
MOVE 12345.56 TO VAR-X
MOVE VAR-X TO VAR-Y
DISPLAY ‘VALUE OF X:’ VAR-X
DISPLAY ‘VALUE OF Y:’ VAR-Y

Output:
TEST PROGRAM
VALUE OF X:12345.56
VALUE OF Y:345.5

4.What is the maximum length of a paragraph name?


A. Length should be less than 30 characters.
Compilation will fail if the length of the paragraph name crosses 30 characters and the error message
shown as “The COBOL word starting in column 21 contained more than 30 characters”. Column name
shown in the error message will differ based on the position the paragraph name referred.

Remember that
Paragraph names include only A-Z, a-z ,0-9 and -(Hyphen) and the length should be less than 30
characters.

5. What will happen if your COBOL program has around 30 blank lines between two
lines of the code?

A. No issue will happen with the compilation as compiler just skips all blank lines. Any number of blank
lines can be placed between your code which doesn’t create any issue with the compilation. Spaces are
normally placed to increase the readability of the program.

6. What is the use of REDEFINE in COBOL ?


A. Main usage is efficient use of SPACE as it allows different fields to use the same storage space.

Redefines in COBOL is mainly used to have the same storage space for different fields. Redefines in
COBOL will be handy to maintain same value when multiple variables are used for same purpose and are
used across any program.

The important points related to Redefines in COBOL:


1.Allows different fields to use the same storage space
2.The definition should be done at the same level
3.The declaration should immediately follow the first declared field
4.Size and kind of data can be different
5.Multiple redefinitions possible.

Probable issues of using Redefines in COBOL:


1.If a numeric field is redefined with a Alphabetic field, then there might be a chance to get data issues due to
incorrect format and that will in turn causes S0C7 abend

2.It might be difficult to track the value and make sure it has the correct value if too many redefinitions exist.
It’s also difficult to find out the error with out debugging during job abend if the problem is with any of the
redefined fields.

1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DESCRIPTION.
05 WS-DATE1 VALUE '20140831'.
10 WS-YEAR PIC X(4).
10 WS-MONTH PIC X(2).
10 WS-DATE PIC X(2).
05 WS-DATE2 REDEFINES WS-DATE1 PIC 9(8).

PROCEDURE DIVISION.
DISPLAY "WS-DATE1 : "WS-DATE1.
DISPLAY "WS-DATE2 : "WS-DATE2.

STOP RUN.

Output:
WS-DATE1 : 20140831
WS-DATE2 : 20140831

7.What is PRESERVE ON command?


A. Whenever any VB file is edited, there is a chance of trailing spaces getting removed and that will
cause the record length change.

To avoid that PRESERVE ON command should be issued when ever any VB file is opened for edit.

8.Difference between Fixed Block and Variable Block in COBOL ?

Fixed Block(FB):

All the records in a file are of same length

FB file can be defined in COBOL program as below

FD FILE01
RECORDING MODE F
BLOCK 0
LABEL RECORD STANDARD.
COPY Layout-name.

There can be unused space at the end of each record and causes wastage of space.

Variable Block(VB):

Record length is not fixed and it can vary with in the defined maximum length.
VB file can be defined in COBOL as below

FD FILE01
RECORDING MODE V
BLOCK 0
RECORD VARYING FROM 1 TO 5000 DEPENDING ON FILE-LTH
LABEL RECORD STANDARD.
COPY Layout-name.

FILE-LTH can be found by using LENGTH function as below

COMPUTE FILE-LTH = FUNCTION LENGTH(FILE01)

VB file can reduce the amount of space required as record length can vary for each record and there will be no
unused space like FB file.

In JCL, the record length should be 4 bytes more than maximum length of the file. If the record
length defined in Cobol is 5000 then the LRECL in JCL will be 5004.

Length:

This will give length of the field, not the length of the data in the field. If the field is defined as pic x(10) and the
content is ‘AAA’, the length returned by the function is 10.

By looking at the COBOL program, we can say whether the file is FB or VB based on the Recording Mode.
If the RECORDING MODE is F then it is fixed length file
If the RECORDING MODE is V then it is variable length file

9. Difference between Static call and Dynamic call?


A. For STATIC call we use Program name directly in the CALL statement as below
For DYNAMIC call we use working storage variable to store program name and use that variable in the
CALL statement.

Static or Dynamic call is identified by CALL statement.

For STATIC call we use Program name directly in the CALL statement as below

CALL ‘PGMNAME’ USING arguments

For DYNAMIC call we use working storage variable to store program name and use that variable in the CALL
statement.
EX:
01 WS-SUBPGM PIC X(8) VALUE ‘SUBPGM’.

CALL WS-SUBPGM USING arguments

In a static call only one load will be available. The called subprogram must be link-edited with the calling
program to form a single load module .
If there is any change to sub program then we have to compile main program also.

In Dynamic call, sub program and main program will have separate load modules. If there is any change to sub
program, then there is NO need to compile main program. Only sub program compilation is fine.

When to use:
If the sub program need to be changed frequently then it’s better to use Dynamic option. With this we no need
to compile main program each time.

If sub program is called multiple times in the same program then it’s better to use static call.
If dynamic call is used here, then the sub program will be copied every time when it is called in the main
program.
So performance wise static call is better in this scenario.

10. What is REVERSE Function in COBOL ?


A. It is used to read Alphanumeric field in the REVERSE order.

Ex:
If you want read Variable Name with the value ‘STRING IN REVERSE ORDER’,
then you can define another variable with the same length and can use REVERSE function to get the reverse
value of the string
New-Name = FUNCTION REVERSE(Name)
After this
New-name will have the value ‘REDRO ESREVER NI GNIRTS’
Other option is
First find the length of the string and assign that to a variable
then use PERFORM to read one character starting from the length value reducing the variable by 1 till it
reaches ZERO
this way we read last character first and by repeating the same process, reverse value of the field can be
achieved.
Instead of PERFORM ,we can even use INSPECT as well for the same.

Part-III

1. COBOL interview question related to finding last 100 records?


A. One approach is to find the count of total records in the file and assign that value to one variable (say
X). Then find X-100 to get last hundred records number (say Y). You have to read the file till it becomes
Y=X by increasing Y value 1 by 1. It will make sure the file is read 100 times to read last 100 records.

2. COBOL interview question related to INITIALIZE?


A. The INITIALIZE statement will initialize each field that is defined with a PIC clause, except FILLER fields.
FILLER fields will remain LOW-VALUES or nulls.

Numeric fields will be initialized by default to Zeroes.


Alphanumeric fields will be initialized by default to Spaces.

But we can change that and initialize according to our need by using REPLACING command.
We tend to initialize whole copybook many times in a program and that will cause considerable
overhead as initialize will create one MOVE statement for each initialization.
If there are 200 fields in a copybook, then 200 MOVE statements are added by the system to initialize
and it adds considerable overhead to the performance.

3. How to READ a record which was READ already?


A. In a sequential file, each READ will read the next record in sequence. So it’s not possible to
read a record again which was read already.

One option is to CLOSE the file and OPEN it again to start reading again from the starting of the
file.
For DYNAMIC access files, sequential and random access is possible.

For Sequential access, we need to use READ NEXT and READ PREVIOUS. But position needs to
be set by START command.

For Random access READ is used. We can set the file indicator position randomly by START and
from there reading is done sequentially.

4. What are different ways a COBOL program can receive data from JCL?
A. We can pass values from JCL to COBOL in two ways. One way is via PARM and another way
via SYSIN.

In the JCL, We need to use PARM=’Parameter value’ on the EXEC statement. The same can be
defined in COBOL in Linkage section.
When the parameter value is passed from JCL to COBOL it is preceded with a two-byte binary
value that specifies the length of the data string.
Linkage section can be defined as below

LINKAGE SECTION.
01 PARM-BUFFER.
05 PARM-LENGTH PIC S9(4) COMP.
05 PARM-NAME PIC X(100).

The maximum length that can be passed via PARM is 100 bytes.

Via SYSIN:

We can either provide the data directly in SYSIN or as a file. We have to use ACCEPT to read that in COBOL.

If no data is passed via SYSIN and ACCEPT is used in COBOL, then program will abend.
To avoid this we can declare in JCL as below
//SYSIN DD DUMMY

5. What are different ways a file can be opened?


A. Sequential file can be opened in 4 ways as below
Input (Only to Read)
Output (Only to Write, after removing existing record if File not Empty)
I-O (Read and Rewrite)
Extend (Append records)

If the file is opened in Input mode:


The possible options are OPEN, READ and CLOSE

If the file is opened in Output mode:


The possible options are OPEN, WRITE and CLOSE

If the file is opened in I-O mode:


The possible options are OPEN, READ, REWRITE and CLOSE

If the file is opened in EXTEND mode: (Random or Dynamic access not allowed)
The possible options are OPEN, WRITE and CLOSE.
To Replace a record in a file:

We need to use REWRITE to replace a record on a file and the file to be opened in I-O mode.

To DELETE a record from a file:

We cannot use DELETE option on a sequential file.

6.Difference between GOBACK, STOP RUN and EXIT PROGRAM?


A.
STOP RUN:
It sops the execution of a program and is the last statement of Main Program.
GOBACK and STOP RUN perform similar operation when it is coded in the main program.

If STOP RUN is issued in the called program or sub program, then the control will never come back to the
called program or main program. So never use STOP RUN in sub program or called program.

GO BACK:
Go back is used in sub-program. It returns control to the calling program.
But GOBACK statement ends the application if it is used in the main program.

EXIT PROGRAM:
It is used to come out of a program that has been called by another program.
It is same as GOBACK if used in sub-program.
But if it is issued in a main program, the program ignores this to continue with the processing or it may abend
as well.
So it should never be used in main program or calling program.

7. What is the difference between INCLUDE and COPY?


A.
Both does the same work of expanding the layout defined in copybook or Dclgen..
But the only difference is
members used with INCLUDE will get expanded at PRE-COMPILATION stage and
members used with COPY will get expanded only at COMPILATION stage.

So we normally use INCLUDE to expand all DB2 related members.


COPY is used to expand COBOL file layouts.

You cannot use COPY to expand DB2 members, but you can use INCLUDE to expand COBOL file layouts.

If you use COPY to expand DB2 DCLGEN members, your program will fail at Pre-compilation stage saying
most of the host variables are not defined. This happens because COPY members will get expanded only at
Compilation stage, that is after Pre-compilation.

If you keep any of the host variable in any cobol copy book then you should expand that also with INCLUDE
only. Otherwise your program will fail at Pre-compilation stage itself due to missing host variable declaration.

DCLGEN : Declaration Generator


It generates SQL and source language declarations.
It produces SQL DECLARE TABLE statement along with COBOL data declaration if DB2 is used along with
COBOL.Most of the fields in data declarations are used as host variables.

8. A COBOL COPYBOOK has changed. How IMPACT analysis can be done?


A.
If a file layout is altered (added few new fields), do we have to re-compile all programs which
use that file?
The answer will depend on whether the record length is getting changed or not because of the layout change.

If the record length is getting changed:

We definitely have to re-compile all programs which refer this layout.


If we don’t recompile, the program still try to refer the file with old record length and it will fail.
So it’s always recommended to recompile all programs which refer the layout if record length changes.
This applies same even for VB files.
Record length not getting changed:
Though layout got changed due to addition of few new fields, it’s NOT mandatory for us to recompile all
Fcontioprograms which refer the layout.
We can only recompile all programs which refer the newly added fields.

As record length is same, programs will not face any issue while referring the file with new layout.

Anyway it will not do any harm if all programs are recompiled in this case as well.

9. COBOL interview question on reading table elements in reverse order ?


Find the table size and move that value to a variable and use that as a sub script to read
last element of table first.
Read process to be repeated till the sub script becomes zero and this way the table
elements can be read in reverse order.
This information helps to answer COBOL interview question on reading array elements
in reverse order.
10.
Working storage variables:
01 WSS-VAR
88 WSS-MARK1 PIC 9(2) VALUE 40
88 WSS-MARK2 PIC 9(2) VALUE 80
Procedure division statements:
SET WSS-MARK1 TO LOW-VALUES.
DISPLAY WSS-VAR.

What will be the output?

Answer:
The code will not execute as there are multiple errors
1.88 level variables should not have any Picture clause associated
2.LOW-VALUES is not allowed to be used with SET operator
Correct declaration will be as below:
01 WSS-VAR PIC 9(2).
88 WSS-MARK1 VALUE 40.
88 WSS-MARK2 VALUE 80.

Procedure division:
SET WSS-MARK1 TO TRUE
DISPLAY ‘WSS-VAR:’ WSS-VAR

The output of this code will be 40 as WSS-MARK1 was set as True.


This information will help to understand how SET operator works and to answer
COBOL interview questions related to 88 level item and SET operator.

10. What is the difference between SEARCH and SEARCH ALL?


A.
SEARCH
1)Linear/sequential search
2)No sorting order because its seq. order
3)access is slow

SEARCH ALL
1) Binary search
2) ASCENDING/DESCENDING KEY clause to be used & data loaded in this order) before using SEARCH
ALL.
3) Accesses is faster
4) only ‘=’ operator can be used.

Part-IV

1. What Is The Difference Between Index And Subscript?


A.
Subscript:
Numbers of occurrences of Array element is called as Subscript.
-Subscript will not refer memory position but array-element position.
-It always start from 1
- Subscript won't be useful for data searching or sorting

Working-storage Section.
01 WS-STUDENT-Table.
05 STUDENT-Row OCCURS 2 TIMES.
10 WS-Name PIC A(03).
10 STUDENT-Column OCCURS 2 TIMES.
04 WS-Marks PIC X(02).

PROCEDURE DIVISION.
MOVE 'RAM6040SAM8090' TO WS-STUDENT-TABLE.
DISPLAY 'WS-TABLE : ' WS-STUDENT-Table.
DISPLAY ' STUDENT-Row (1): ' STUDENT-Row (1).
DISPLAY 'STUDENT-Column (1, 1): ' STUDENT-Column (1, 1).
DISPLAY 'STUDENT-Column (1, 2): ' STUDENT-Column (1, 2).
DISPLAY ' STUDENT-Row (2): ' STUDENT-Row (2).
DISPLAY 'STUDENT-Column (2, 1): ' STUDENT-Column (2, 1).
DISPLAY 'STUDENT-Column (2, 2): ' STUDENT-Column (2, 2).

O/P:
WS-TABLE: RAM6040SAM8090
STUDENT-Row (1): RAM6040
STUDENT-Column (1, 1): 60
STUDENT-Column (1, 2): 40
STUDENT-Row (2): SAM8090
STUDENT-Column (2, 1): 80
STUDENT-Column (2, 2): 90

Index:
Index is the disposition of the element in array.
- It indicates the offset from the beginning of the table
- An Index is the value of OFFSET or DISPLACEMENT from the starting of the array.
-An index can only be modified using PERFORM, SEARCH & SET.
-Need to have index for a table in order to use SEARCH, SEARCH ALL.
- Index can be used in Table data searching and sorting.

WORKING-STORAGE SECTION.
01 WS-STUDENT-Table.
05 STUDENT-Row OCCURS 2 TIMES INDEXED BY I.
10 WS-Name PIC A(03).
10 STUDENT-Column OCCURS 2 TIMES INDEXED BY J.
04 WS-Marks PIC X(02).

PROCEDURE DIVISION.
MOVE 'RAM6040SAM8090' TO WS-STUDENT-Table.
PERFORM A-PARA VARYING I FROM 1 BY 1 UNTIL I >2
STOP RUN.

A-PARA.
PERFORM C-PARA VARYING J FROM 1 BY 1 UNTIL J>2.

C-PARA.
DISPLAY STUDENT-Column (I, J).

O/P:
60
40
80
90

2. What Is Binary Search?


A. Search on a sorted array. Compare the item to be searched with the item at the center. If it matches,
fine else repeat the process with the left half or the right half depending on where the item lies.

3. What is the use of Level 77 /88 /66?


A.
77 Level – used to declare variable as independent and it can’t be subdivided or grouped.
88 Level – used for condition names.
66 Level – used for RENAMES clause.

4. What does the IS NUMERIC clause establish?


A. IS NUMERIC can be used on alphanumeric items, signed numeric & packed decimal items and
unsigned numeric & packed decimal items.

IS NUMERIC returns TRUE if the item only consists of 0-9. However, if the item being tested is a signed
item, then it may contain 0-9, + and - .

Example1,

MOVE '15623' TO WS-TEXT.


IF WS-TEXT IS NUMERIC
DISPLAY 'NUMBER'
ELSE
DISPLAY 'TEXTUAL CHARACTERS’
END-IF.

Output:
NUMBER

Example2,
MOVE 'HE123' TO WS-TEXT
IF WS-TEXT IS NUMERIC
DISPLAY 'NUMBER'
ELSE
DISPLAY 'TEXTUAL CHARACTERS’
END-IF.

Output:
TEXTUAL CHARACTERS

5. How do references the following file formats from COBOL programs:


A.
Fixed Block File - Use

ORGANISATION IS SEQUENTIAL.
RECORDING MODE IS F
BLOCK CONTAINS 0 .

Fixed Unblocked - Use

ORGANISATION IS SEQUENTIAL.
RECORDING MODE IS F
do not use BLOCK CONTAINS

Variable Block File - Use

ORGANISATION IS SEQUENTIAL.
RECORDING MODE IS V.
BLOCK CONTAINS 0.
Do not code the 4 bytes for record length in FD ie JCL rec length will be max rec length in pgm + 4

Variable Unblocked - Use

ORGANISATION IS SEQUENTIAL.
RECORDING MODE IS V
do not use BLOCK CONTAINS.
Do not code 4 bytes for record length in FD ie JCL rec length will be max rec length in pgm + 4.

ESDS VSAM file - Use

ORGANISATION IS SEQUENTIAL.

KSDS VSAM file - Use

ORGANISATION IS INDEXED
RECORD KEY IS

ALTERNATE RECORD KEY IS RRDS File - Use

ORGANISATION IS RELATIVE
RELATIVE KEY IS
Printer File - Use

ORGANISATION IS SEQUENTIAL.
RECORDING MODE IS F
BLOCK CONTAINS 0.

6. How is sign stored in Packed Decimal fields and Zoned Decimal fields?
A.
Packed Decimal fields: Sign is stored as a hex value in the last nibble (4 bits) of the storage.
Zoned Decimal fields: As a default, sign is over punched with the numeric value stored in the last bite.

How is sign stored in a COMP field ?


A. In the most significant bit. Bit is ON if -ve, OFF if +ve.

7. What is the difference between NEXT SENTENCE and CONTINUE?


A.
NEXT SENTENCE gives control to the verb following the next period.
CONTINUE gives control to the next verb after the explicit scope terminator.

What is a scope terminator?


A. Scope terminator is used to mark the end of a verb e.g. EVALUATE, END-EVALUATE; IF, END-
IF.

8. What is file status 92 & 39?


A.
Status 92: Logic error. e.g., a file is opened for input and an attempt is made to write to it.
Status 39: Mismatch in LRECL or BLOCKSIZE or RECFM between COBOL pgm & the JCL (or the
dataset label). will get file status 39 on an OPEN.

9. What is AMODE(24), AMODE(31), RMODE(24) and RMODE(ANY)?


A. These are compile/link edit options. Basically AMODE stands for Addressing mode and
RMODE for Residency mode.

AMODE(24) - 24 bit addressing;

AMODE(31) - 31 bit addressing

AMODE(ANY) - Either 24 bit or 31 bit addressing depending on RMODE.

RMODE(24) - Resides in virtual storage below 16 Meg line. Use this for 31 bit programs that call
24 bit programs.
RMODE(ANY) - Can reside above or below 16 Meg line.

10. How can we tell if a module is being called DYNAMICALLY or STATICALLY?


A.
The ONLY way is to look at the output of the linkage editor (IEWL)or the load module itself.

If the module is being called DYNAMICALLY then it will not exist in the main module, if it is
being called STATICALLY then it will be seen in the load module.

Calling a working storage variable, containing a program name, does not make a DYNAMIC call.

This type of calling is known as IMPLICITE calling as the name of the module is implied by the
contents of the working storage variable.

You might also like