IBM COBOL Interview Questions and Answers
Can a SEARCH be applied to a table which does not have an INDEX defined?
Ans: No, the table must be indexed.
What is the difference between SEARCH and SEARCH ALL?
Ans:
SEARCH - is a serial search.
SEARCH ALL - is a binary search & the table must be sorted ( ASCENDING/DESCENDING KEY clause to be used & data loaded in this order) before using SEARCH ALL.
What should be the sorting order for SEARCH ALL?
Ans: It can be either ASCENDING or DESCENDING. ASCENDING is default. If you want the search to be done on an array sorted in descending order, then while defining the array, you should give DESCENDING KEY clause. (You must load the table in the specified order).
What is binary search?
Ans: 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.
Can a SEARCH be applied to a table which does not have an INDEX defined?
Ans: No, the table must be indexed.
A table has two indexes defined. Which one will be used by the SEARCH verb?
Ans: The index named first will be used, by Search.
What are the different rules applicable to perform a binary SEARCH?
Ans: The table must be sorted in ascending or descending order before the beginning of the SEARCH. Use OCCURS clause with ASC/DESC KEY IS dataname1 option
The table must be indexed. There is no need to set the index value. Use SEARCH ALL verb
How does the binary search work?
Ans: First the table is split into two halves and in which half, the item need to be searched is determined. The half to which the desired item may belong is again divided into two halves and the previous procedure is followed. This continues until the item is found. SEARCH ALL is efficient for tables larger than 70 items.
What is the difference between a binary search and a sequential search? What are the pertinent COBOL commands?
Ans: In a binary search the table element key values must be in ascending or descending sequence. The table is 'halved' to search for equal to, greater than or less than conditions until the element is found. In a sequential search the table is searched from top to bottom, so (ironically) the elements do not have to be in a specific sequence. The binary search is much faster for larger tables, While sequential Search works well with smaller ones. SEARCH ALL is used for binary searches; SEARCH for sequential.
2. How do you sort in a COBOL program? Give sort file definition, sort statement syntax and meaning.
Ans: 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.
3. How do you define a sort file in JCL that runs the COBOL program?
Ans: Use the SORTWK01, SORTWK02,..... did names in the step. Number of sort datasets depends on the volume of data being sorted, but a minimum of 3 is required.
4. Explain the difference between an internal and an external sort. The pros & cons & internal sort syntax .?
Ans: An external sort is not coded as a COBOL program; it is performed through JCL and PGM=SORT. One can use IBM utility SYNCSORT for external sort process. It is understandable without any code reference. An internal sort can use two different syntaxes: 1.) USING, GIVING sorts are comparable to external sorts with no extra file processing; 2) INPUT PROCEDURE, OUTPUT PROCEDURE sorts allow for data manipulation before and/or after the sort.
What is the difference between performing a SECTION and a PARAGRAPH?
Ans: Performing a SECTION will cause all the paragraphs that are part of the section, to be performed. Performing a PARAGRAPH will cause only that paragraph to be performed.
What kind of error is trapped by ON SIZE ERROR option?
Ans: 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.
What is the point of the REPLACING option of a copy statement?
Ans: 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 .
When is a scope terminator mandatory?
Ans: Scope terminators are mandatory for in-line PERFORMS and EVALUATE statements. For readability, it's recommended coding practice to always make scope terminators explicit.
Can you use REDEFINES clause in the FILE SECTION?
Ans: No
How will you define your record descriptions in the FILE SECTION if you want to use three different record descriptions for the same file?
Ans: FD filename DATA RECORDS ARE rd01, rd02, rd03. 01 rd01 PIC X(n). 01 rd02 PIC X(n). 01 rd03 PIC X(n).
When will you open a file in the EXTEND mode?
Ans: When an existing file should be appended by adding new records at its end. EXTEND mode opens the file for output, but the file is positioned following the last record on the existing file.
What does a CLOSE WITH LOCK statement do?
Ans: The statement closes an opened file and it prevents the file from further being opened by the same program.
Which mode of opening is required when REWRITE is used?
Ans: I-O mode
Why is it necessary that the file be opened in I-O mode for REWRITE?
Ans: Before the REWRITE is performed, the record must be read from the file. Hence REWRITE includes an input operation and an output operation. Therefore, the file must be opened in I-O mode.
Which clause can be used instead of checking for FILE STATUS = 10?
Ans: FILE STATUS 10 is the end of file condition. Hence AT END clause can be used.
What is the format of a simple SORT verb? What kinds of files can be sorted using SORT?
Ans: SORT workfile ON ASC/DESC KEY key1, ASC/DESC KEY key2 ... USING inputfile GIVING outputfile Only sequential files can be sorted in this way.
Can a SEARCH be applied to a table which does not have an INDEX defined?
Ans: No, the table must be indexed.
What is the difference between SEARCH and SEARCH ALL?
Ans:
SEARCH - is a serial search.
SEARCH ALL - is a binary search & the table must be sorted ( ASCENDING/DESCENDING KEY clause to be used & data loaded in this order) before using SEARCH ALL.
What should be the sorting order for SEARCH ALL?
Ans: It can be either ASCENDING or DESCENDING. ASCENDING is default. If you want the search to be done on an array sorted in descending order, then while defining the array, you should give DESCENDING KEY clause. (You must load the table in the specified order).
What is binary search?
Ans: 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.
Can a SEARCH be applied to a table which does not have an INDEX defined?
Ans: No, the table must be indexed.
A table has two indexes defined. Which one will be used by the SEARCH verb?
Ans: The index named first will be used, by Search.
What are the different rules applicable to perform a binary SEARCH?
Ans: The table must be sorted in ascending or descending order before the beginning of the SEARCH. Use OCCURS clause with ASC/DESC KEY IS dataname1 option
The table must be indexed. There is no need to set the index value. Use SEARCH ALL verb
How does the binary search work?
Ans: First the table is split into two halves and in which half, the item need to be searched is determined. The half to which the desired item may belong is again divided into two halves and the previous procedure is followed. This continues until the item is found. SEARCH ALL is efficient for tables larger than 70 items.
What is the difference between a binary search and a sequential search? What are the pertinent COBOL commands?
Ans: In a binary search the table element key values must be in ascending or descending sequence. The table is 'halved' to search for equal to, greater than or less than conditions until the element is found. In a sequential search the table is searched from top to bottom, so (ironically) the elements do not have to be in a specific sequence. The binary search is much faster for larger tables, While sequential Search works well with smaller ones. SEARCH ALL is used for binary searches; SEARCH for sequential.
2. How do you sort in a COBOL program? Give sort file definition, sort statement syntax and meaning.
Ans: 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.
3. How do you define a sort file in JCL that runs the COBOL program?
Ans: Use the SORTWK01, SORTWK02,..... did names in the step. Number of sort datasets depends on the volume of data being sorted, but a minimum of 3 is required.
4. Explain the difference between an internal and an external sort. The pros & cons & internal sort syntax .?
Ans: An external sort is not coded as a COBOL program; it is performed through JCL and PGM=SORT. One can use IBM utility SYNCSORT for external sort process. It is understandable without any code reference. An internal sort can use two different syntaxes: 1.) USING, GIVING sorts are comparable to external sorts with no extra file processing; 2) INPUT PROCEDURE, OUTPUT PROCEDURE sorts allow for data manipulation before and/or after the sort.
What is the difference between performing a SECTION and a PARAGRAPH?
Ans: Performing a SECTION will cause all the paragraphs that are part of the section, to be performed. Performing a PARAGRAPH will cause only that paragraph to be performed.
What kind of error is trapped by ON SIZE ERROR option?
Ans: 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.
What is the point of the REPLACING option of a copy statement?
Ans: 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 .
When is a scope terminator mandatory?
Ans: Scope terminators are mandatory for in-line PERFORMS and EVALUATE statements. For readability, it's recommended coding practice to always make scope terminators explicit.
Can you use REDEFINES clause in the FILE SECTION?
Ans: No
How will you define your record descriptions in the FILE SECTION if you want to use three different record descriptions for the same file?
Ans: FD filename DATA RECORDS ARE rd01, rd02, rd03. 01 rd01 PIC X(n). 01 rd02 PIC X(n). 01 rd03 PIC X(n).
When will you open a file in the EXTEND mode?
Ans: When an existing file should be appended by adding new records at its end. EXTEND mode opens the file for output, but the file is positioned following the last record on the existing file.
What does a CLOSE WITH LOCK statement do?
Ans: The statement closes an opened file and it prevents the file from further being opened by the same program.
Which mode of opening is required when REWRITE is used?
Ans: I-O mode
Why is it necessary that the file be opened in I-O mode for REWRITE?
Ans: Before the REWRITE is performed, the record must be read from the file. Hence REWRITE includes an input operation and an output operation. Therefore, the file must be opened in I-O mode.
Which clause can be used instead of checking for FILE STATUS = 10?
Ans: FILE STATUS 10 is the end of file condition. Hence AT END clause can be used.
What is the format of a simple SORT verb? What kinds of files can be sorted using SORT?
Ans: SORT workfile ON ASC/DESC KEY key1, ASC/DESC KEY key2 ... USING inputfile GIVING outputfile Only sequential files can be sorted in this way.