Skip to main content

COBOL Interview Questions

Q: What is the biggest difference between PIC 9.99 and PIC 9v99?

·       PIC 9.99 contains a decimal point, which means that it is a four-length field.

·      PIC 9v99 consists of an inferred or assumed decimal position, which means that it is a three-length numeric field.

Q: My program that uses an array, configured to have 10 items. Because of some problem, I found out that the program is accessing 11th element in the list, and the program is not abending. What's wrong with that?

To limit array boundaries, we should test that SSRANGE is set to compiler option. This option is set to NOSSRANGE by Default.

Q: Can we redefine X(100) field using X(200) field?

Yes. Redefines just allows both fields to begin at the same location. It won’t throw any syntax error but gives you compilation error.

Q: When do we get “RECORD NOT FOUND” or file status 23?

An attempt has been made to access a record (for indexed, relative files), that does not exist in the file.

or

START/READ operation has been tried on an optional input file that is not present.

Q: What is COBOL and COBOL full form/stands for?

·         COBOL stands for Common Business-Oriented Language.

·         A High-level language-it needs to be translated into a machine language.

·         Expressly built for the administrative data processing.

Q: Name the sections which we use in Data Division?

·         FILE SECTION

·         WORKING-STORAGE SECTION

·         LOCAL-STORAGE SECTION

·         LINKAGE SECTION.

Q: Where we define Data Area and LDA in COBOL?

Under Environment Division, In the SPECIAL-NAMES section

·         the LDA must be specified as LOCAL-DATA.

·         the data area as DATA-AREA.

Q: Under which division and how we define files in COBOL?

under Environment division we define files

ENVIRONMENT DIVISION.

   INPUT-OUTPUT SECTION.

   FILE-CONTROL.

   SELECT FILEN ASSIGN TO Database-file

      ORGANIZATION IS SEQUENTIAL.

      ACCESS IS SEQUENTIAL.

Q: Different types of file access methods?

·         Sequential Access

·         Random Access

·         Dynamic Access

Q: what are the different file modes available in COBOL?

INPUT, OUTPUT, I-O, EXTEND

Q: What is Redefine clause in as400?

The Redefines clause refers to same storage but with a different data definition. When one or more data items are not used back to back, the same storage may be used for another data item.

For example:

01 WS-STUDENT PIC X(5)

01 WS-STUDENT2 REDEFINES WS-STUDENT PIC X(4)

Q: What is the purpose of the EVALUATE statement?

Evaluate is just like a case statement and will be used to replace nested ifs. The difference between EVALUATE and the case is that no 'break' is required for EVALUATE, i.e. control comes out of the EVALUATE as soon as match is detected.

Q: Different types of perform statements in COBOL?

·         PERFORM

·         PERFORM..THRU

·         PERFORM..UNTIL

·         PERFORM..TIMES

Q: Read the following code.

01 WS-X PIC 9(2) value zero.

PARA-ONE. MOVE 5 TO WS-X.

PERFORM PARA-TWO WS-X TIMES.

PARA-TWO. MOVE 10 TO WS-X.

How many times will PARA-TWO be executed?

Just 5 times. It will not consider the value of 10 that is initialized in the loop.

Q: Important error-handling phrases and clauses, For input-output operation.

·         AT END phrase

·         INVALID KEY phrase

·         NO DATA phrase

·         USE AFTER EXCEPTION/ERROR declarative procedure

·         FILE STATUS clause.

Q: Give an example of command terminators?

END-IF, END-EVALUATE

Q: Name the divisions in a COBOL program.

·         IDENTIFICATION DIVISION

·         ENVIRONMENT DIVISION

·         DATA DIVISION

·         PROCEDURE DIVISION.

Q: What are the different data types available in COBOL?

·         Alpha-numeric (X)

·         Alphabetic (A)

·         Numeric (9).

Q: What's the 77 level used for?

Item of the elementary level. It cannot be subdivided into other items / on its own.

Q: What is 88 level used for?

For defining condition names.

Q: What is level 66 used for?

For RENAMES clause.

Q: What is the difference between the rename clause and the redefine clause in COBOL?

The Redefines clause is used to refer to a same storage area with a different data definition . If one or more data items are not used at the same time, the same storage may be used for another data item. As a result, the same storage can be transferred to different data items.

The rename clause is used to indicate a different name to existing data items. It is used to re-group data names and assign them a new name. Level 66 is reserved for renames.

Q: Define COPYBOOK, in which division we write COPYBOOK with an example? 

We define copybook with data structure. Copybook describes the structure of the records. If a similar data structure is used in many programs, we should use copybooks instead of writing the same data structure again. We use COPY statement in Working-Storage Section which is under Data Division. 

Copybook inside a COBOL program −

DATA DIVISION.

WORKING-STORAGE SECTION.

COPY Copybookname.

Q: Why do we code S9(4) COMP, instead of COMP-3 knowing that it will occupy less space.

Here, S9(4) COMP is a small integer, so two words are equal to 8 bytes. Totally it will occupy 2byte (4 characters), While in s9(4) comp-3 as one word is equivalent to 1/2 byte.4 characters equivalent to 2 bytes and sign will occupy 1/2 bytes, it will occupy 3 bytes altogether.

Q: What is the IS NUMERIC clause?

·    IS NUMERIC can be applied, to alphanumeric items, signed numeric & packed decimal items and unsigned numeric . 

·         IS NUMERIC returns TRUE when it only consists of 0-9.

·         If the item being checked is a signed item though, it can contain 0-9, + and-

Q:  What is the difference between performing an SECTION and performing a PARAGRAPH?

Performing an SECTION will cause all the paragraphs that fall under the section.

Performing a PARAGRAPH will cause only that paragraph to be performed.

Q: Can I redefine an X (200) field with a field of X(100) ?

Yes.

Q: What guidelines should be followed to write a structured COBOL program?

1) Use the 'EVALUATE' statement to construct the cases.

2) Use the scope terminals for nesting.

3) Use the in-line Perform statement to write 'do' constructions.

4) Use Test Before and Test After in the Perform statement to write Do-While Constructions.

Q: What does EXIT do?

The EXIT statement indicates the end of the called program and returns control to the calling program.

Q: How is Sign handled in Decimal Packed Fields and Decimal Zoned Fields?

Packed Decimal fields: Sign in the last nibble (4 bits) of the storage is stored as a hex value. 

Zoned Decimal fields: By default, the numeric value stored in the last bite is over-punched with symbol.

Q: Give few advantages of REDEFINES clause.

·     REDEFINE variables from one PICTURE clause to another PICTURE clause using the same memory location.

·  Using REDEFINES, we can Initialize the variable itself in the WORKING-STORAGE section.

·        We can REDEFINE a Single Variable into so many sub-variables.

Q: How is sign stored in a comp-3 field?

Sign is stored in last nibble.

Q: How is sign stored in a COMP field?

In the most significant bit. Bit is on if -ve, off if +ve.

Q: What is the difference between COMP & COMP-3?

COMP is a binary storage format while COMP-3 is packed decimal format.

Q: What is COMP-1? COMP-2?

COMP-1 Single precision floating point which uses 4 bytes.

COMP-2 Double precision floating point which uses 8 bytes.

Q: How do you define a variable of COMP-1/COMP-2 with an example?

No picture clause to be given.

Example 01 WS-VAR USAGE COMP-1.

                01 WS-VAR2 USAGE COMP-2.

Q: How many bytes does a S9(7) COMP-3 field occupy?

Will take 4 bytes. Sign is stored as hex value in last nibble.

General formula is INT((n/2) + 1)), where n=7 in this example.

Q: How many bytes does S9(7) SIGN TRAILING SEPARATE field occupy?

Will occupy 8 bytes (one extra byte for sign).

Q: Name different file access modes available in COBOL? In which modes are used to write to the file.

Different Open modes for files are INPUT, OUTPUT, I-O and EXTEND. Of which Output and Extend modes are used to write new records to the file.

Q: What is file status 92?

Logic error. A file is opened for input and an attempt is made to write to it.

Q: Explain NEXT and CONTINUE verbs for file handling.

The Continue Clause is used in cases where the EOF condition is not present. Records can be accessed in a file over and over again. Whereas the indexed file is accessed sequentially in the next verb, the next record command will be used when sequentially accessing the index clause.

Q: What is difference between binary search and sequential search?

·        SEARCH ALL is used for binary searches; SEARCH for sequential searches

·     Binary Search is much faster for larger tables, While Sequential Search works  good with smaller ones.

·  The table element key values must be sorted in ascending or descending  sequence during the binary search. The table is 'halved' to be searched for equal to, greater than or less than the conditions until an element is found.

·   The table is searched from top to bottom in a sequential search, so that the elements do not have to be in a specific sequence.

Q: What is the difference between PERFORM ... WITH TEST AFTER and PERFORM ... WITH TEST BEFORE?

If TEST BEFORE is defined, each repeated execution of the mentioned PERFORM range shall be checked at the beginning of each condition.

If TEST AFTER is stated, the condition will be checked at the end of each repeated PERFORM range execution. The set is executed at least once with Check AFTER.

It will execute blocks of statements once, even though the condition is incorrect for the first time.

Q: Which is the default, TEST BEFORE or TEST AFTER for a PERFORM statement?

TEST BEFORE

Q: How do you code an in-line PERFORM?

PERFORM ... ... END-PERFORM.

Q: When would you use in-line perform?

When the body of the perform will not be used in other paragraphs. If the body of the perform is a generic type of code, it would be better to put the code in a separate Para and use PERFORM para-name rather than in-line perform.

Q: In an EVALUTE statement is the order of the WHEN clauses significant?

Yes, evaluation of the when clause proceeds from top to bottom

Q: How do you come out of an EVALUATE statement?

After the execution of one of the when clauses, the control is automatically passed on to the next sentence after the EVALUATE statement. There is no need of any extra code.

Q: What is the default INITIALIZE value, and which keyword is used to override the default.

INITIALIZE sets the alphabetic and alphanumeric field spaces. Initialize numeric fields with setting Zero. FILLER OCCURS DEPENDING ON items are left untouched. To override those defaults, you can use the REPLACING option.

Q: What’s SET TO TRUE all about, anyway?

88 levels can be set rather than moving their associated values to the related data item.

Q: What is the function of the STRING delimiter?

The STRING delimiter causes the send field to be terminated and another field to be started.

Q: What's the function of the UNSTRING delimiter?

When a delimiter is found in the sending field, the current receiving field is switched to the next one.

Q: How will you count the number of characters in a null-terminated string?

MOVE 0 TO char-count

INSPECT null-terminated-string TALLYING char-count FOR CHARACTERS BEFORE X"00"

Q: 77 COUNTR PIC 9 VALUE ZERO.

01 DATA-2 PIC X(11). . .

INSPECT DATA-2 TALLYING COUNTR FOR LEADING "0" REPLACING FIRST "A" BY "2" AFTER INITIAL "C"

If DATA-2 is 0000ALABAMA, what will DATA-2 and COUNTER be after the execution of INSPECT verb?

Counter=4. Data-2 will not change as the Initial 'C' is not found.

Q: What is ON SIZE ERROR phrase.

The outcomes of arithmetic operations may be greater than the fixed-point field 

to carry them, or you may have tried to divide by zero

Q: What kind of error is trapped by ON SIZE ERROR option?

·         Fixed-point overflow.

·         Zero raised to the zero power.

·         Division by 0.

·          Zero raised to a negative number.

·         A negative number raised to a fractional power.

Q: What is the point of the REPLACING option of a copy statement?

REPLACING allows for the same copy to be used more than once in the same code by changing the replace value. COPY xxx REPLACING BY .

Q: what is COMPUTE statement in cobol?

In a single expression, the compute statement can be used to write more than one   arithmetic statement where other arithmetic operations can not be carried out.

Q: When is a scope terminator mandatory?

Scope terminators are mandatory for in-line PERFORMS and EVALUATE statements. For readability, it's recommended coding practice to always make scope terminators explicit.

Q: Can you use REDEFINES clause in the FILE SECTION?

No

Q: How will you define your record descriptions in the FILE SECTION if you want to use three different record descriptions for the same file?

FD FILENAME

DATA RECORDS ARE RD01, RD02, RD03.

01 RD01 PIC X(N).

01 RD02 PIC X(N).

01 RD03 PIC X(N).

Q: Different file organization methods?

•Sequential File Organization

•Indexed Sequential File Organization

•Relative File Organization

Q: When will you open a file in the EXTEND mode?

Extend mode is used to add a record to a sequential file. Records will be inserted at the end of this mode. If the file access mode is Random or Dynamic, the file extension mode cannot be used.

Q: What does a CLOSE WITH LOCK statement do?

The statement closes an opened file and it prevents the file from further being opened by the same program.

Q: Which mode of opening is required when REWRITE is used?

I-O mode

Q: Why is it necessary to open the file in REWRITE I-O mode?

The record must be read from the file before the REWRITE is done. Therefore, REWRITE includes an input operation and an output operation. The file must therefore be opened in I-O mode.

Q: Which clause can be used instead of a FILE STATUS = 10?

FILE STATUS value 10 is end of file condition. Hence AT END clause can be used.

Q: How do you place the indexed file at a particular point so that subsequent sequential file operations may begin from that point?

Use START

START filename KEY IS EQ/GT/LT.. dataname

INVALID KEY ...

Q: What are the access mode requirements of START statement?

Access mode must be SEQUENTIAL or DYNAMIC.

Q: What is the use of the LINKAGE SECTION?

The linkage section is used to transfer data from one program to another program or to transfer data from a program to a PROC.

Q: What is Subscript?

Subscript is used in procedure Division to indicate which specific item has to be accessed from the array. Subscript is used with an identifier that is defined along with OCCURS clause.

EX: MOVE ID(2) TO STUDENT.

Q: If you were passing a table via linkage, which is preferable - a subscript or an index?

An index cannot be passed via linkage. The index is not part of calling programs working storage. Indexing uses binary offsetting. Subscripts make use of the occurrence value.

Q: What is the difference between a subscript and an index in a table definition?

Subscript refers to the array occurrence while index is the displacement (in no of bytes) from the beginning of the array. The index can only be changed by using PERFORM, SEARCH & SET. You need to have a table index to use SEARCH, SEARCH ALL Cobol statements.

Q: What is the maximum value that can be stored in S9(8) COMP?

99999999

Q: What happens when we move a comp-3 field to an edited (say z(9).zz-)

Editing characters are to be used with usage clause data items as the default display. When you try to display a data item that is used as a computational item, it does not display the desired display format because the data item is stored as a packed decimal item. So if you want to edit this particular data item, you need to switch it to a data item whose use is displayed and edit that particular data item in the desired format.

Q: How we retrieve data area in COBOL?

By using ACCEPT verb.

Q: How we update data area in COBOL?

By using DISPLAY verb.

Q: What is the difference between an External and a Global Variable 's?

Global variables can only be accessed by the batch program, while external 

variables can be retrieved from any batch program in the same system library.

Q: What is the Importance of GLOBAL clause According to new standards of COBOL

When any data name, file-name , Record-name, condition name or Index defined in an Including Program can be referenced by a directly or indirectly in an included program, Provided the said name has been declared to be a global name by GLOBAL Format of Global Clause is01 data-1 PIC 9(5) IS GLOBAL.

Q: What is the Purpose of POINTER Phrase in STRING command

The Purpose of POINTER phrase is to specify the leftmost position within receiving field where the first transferred character will be stored

Q: How we get current date from system with century?

By using Intrinsic function, FUNCTION CURRENT-DATE

Q: Which statement will you use to move non-null characters from a null-terminated String?

UNSTRING null-terminated-string DELIMITED BY X"00" INTO target-area

COUNT IN char-count. (There are other methods, such as 1) using PERFORM 2) using SEARCH 3) using INSPECT and MOVE etc...)

Q: What is the format of a simple SORT verb? What kinds of files can be sorted using SORT?

SORT workfile ON ASC/DESC KEY key1, ASC/DESC KEY key2 ...

USING inputfile GIVING outputfile

Only sequential files can be sorted in this way.

Q: What are the different rules of SORT that needs to be considered?

The input and output files must remain closed because SORT opens them and closes during the operation. The work file must have a SELECT clause. The work file must have sort description of  SD entry in the FILE SECTION. Input and Output files must have FD entries.

Q:  What are the access mode requirements of START statement?

Access mode must be SEQUENTIAL or DYNAMIC

Q: What are the opening mode requirements of START statement?

Files must be opened in the INPUT or I-O mode.

Q: What is the LINKAGE SECTION used for?

The linkage section is used to pass data from one program to another program or to pass data from a PROC to a program. It is part of a called program that 'links' or maps to data items in the calling program's working storage. It is the part of the called program where these share items are defined.

Q: What is an in line PERFORM? When would you use it? Anything else to say about it?

The statements between PERFORM and END-PERFORM is called IN line. The COBOL equivalent is to PERFORM or PERFORM THRU a paragraph. In line PERFORMs work as long as there are no internal GO TOs, not even to an exit. The in line PERFORM for readability should not exceed a page length - often it will reference other PERFORM paragraphs. When the body of the Perform will not be used in other paragraphs. If the body of the Perform is a generic type of code (used from various other places in the program), it would be better to put the code in a separate para and use PERFORM para-name rather than in-line perform.

Q: What is the use of EVALUATE statement? How do you come out of an EVALUATE statement?

Evaluate is like a case statement and can be used as a replacement for nested ifs. The difference between EVALUATE and case is that for EVALUATE no 'break' is needed, i.e. control comes out of the EVALUATE as soon as one match is made, no need to write extra code. EVALUATE can be used in place of the nested statements IF THEN ELSE.

Q: What are different forms of EVALUATE statement?

EVALUATE EVALUATE SQLCODE ALSO FILE-STATUS

WHEN A=B AND C=D WHEN 100 ALSO '00'

Imperative statement

WHEN (D+X)/Y = 4 WHEN -305 ALSO '32'

imperative statement

WHEN OTHER WHEN OTHER

imperative statement

END-EVALUATE

 

EVALUATE SQLCODE ALSO A=B EVALUATE SQLCODE ALSO TRUE

WHEN 100 ALSO TRUE WHEN 100 ALSO A=B

imperative statement

WHEN -305 ALSO FALSE WHEN -305 ALSO (A/C=4)

imperative statement

END-EVALUATE

Q: What is an explicit scope terminator?

The scope terminator supports its previous verb, e.g. If... END-IF, in such a way that all statements between the IF verb and its scope terminator are grouped together. Other common COBOL verbs include READ, PERFORM, EVALUATE, SEARCH and STRING.

Q: Explain call by context by comparing it to other calls.

The parameters passed in a call by context are protected from modification by the called program. In a normal call they can be modified.

Q: What will happen if you code GO BACK instead of STOP RUN in a stand-alone COBOL program i.e. a program which is not calling any other program.

Both give the same results when a program is not calling any other program.

Q: What is the Importance of GLOBAL clause in COBOL

If any data name, file name, record name, condition name or index defined in an Including Program can be referenced directly or indirectly in an included program, provided GLOBAL Format of Global Clause is01 data-1 PIC 9(5) IS GLOBAL has declared that name to be a global name.

Q: How do you sort in a COBOL program? Give sort file definition, sort statement syntax and meaning.

Syntax:

SORT file-1 ON ASCENDING/DESCENDING KEY key.

USING file-2

GIVING file-3.

USING can be substituted by INPUT PROCEDURE IS para-1 THRU para-2

GIVING can be substituted by OUTPUT PROCEDURE IS para-1 THRU para-2.

file-1 is the sort workfile and must be described using SD entry in FILE SECTION.

file-2 is the input file for the SORT and must be described using an FD entry in FILE SECTION and SELECT clause in FILE CONTROL.

file-3 is the outfile from the SORT and must be described using an FD entry in FILE SECTION and SELECT clause in FILE CONTROL.

file-1, file-2 & file-3 should not be opened explicitly.

INPUT PROCEDURE is executed before the sort and records must be Released to the sort work file from the input procedure.

OUTPUT PROCEDURE is executed after all records have been sorted. Records from the sort work file must be Returned one at a time to the output procedure.

Q: What is the maximum length of a field you can define using COMP-3?

10 Bytes (S9(18) COMP-3).

 

 

 

 

 

Comments

  1. Awesome Saritha. Excellent work. Let's cover all mainframe skills and make it big.

    ReplyDelete

Post a Comment