You are on page 1of 5

cobol good interview questions: ques1: Can we reverse the string in cobol? ans1: IDENTIFICATION DIVISION. PROGRAM-ID. MID.

ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 STR1 PIC X(7) VALUE IS 'MANOJ'. 01 STR2 PIC X(1). 01 STR3 PIC X(7) VALUE SPACE. *01 STR4 PIC X(7). 01 STRLEN PIC 9(1). 01 CTR1 PIC 9(1) VALUE IS 1. PROCEDURE DIVISION. INSPECT STR1 TALLYING STRLEN FOR CHARACTERS. DISPLAY STRLEN. PERFORM UNTIL STRLEN < 1 MOVE STR1(STRLEN:1) TO STR2 DISPLAY STR2 STRING STR3 DELIMITED BY SPACE STR2 DELIMITED BY SPACE INTO STR3 COMPUTE STRLEN = STRLEN - 1 DISPLAY STR3 END-PERFORM. DISPLAY STR3. STOP RUN. or 01 name pic x(4). 01 rname pic x(4). move name(1:1) to rname(4:1). move name(2:1) to rname(3:1). move name(3:1) to rname(2:1). move name(4:1) to rname(1:1). ques2:Where do use LOW-VALUE and HIGH-VALUE in COBOL. Also explain which kind of values they represent? ans2:One of the use cases of HIGH-VALUE and LOW-VALUE are in the Binary Search u sing SEARCH ALL. If the table to be searched is only partially populated, the SE ARCH ALL may not work correctly. If the table is ordered in ascending sequence t hen, to get the SEARCH ALL to function correctly, the unpopulated elements must be filled with HIGH-VALUES. If the table is in descending sequence, the unpopula ted elements should be filled with LOW-VALUES. and LOW-VALUE represents X'00' and HIGH-VALUE represents X'FF'. These equate to the

lower and upper limits of the collating sequence. All other values fall in betw een. There are many uses. Say you wanted to know who had the highest and lowest sales for the month. Many people have specific "first time code" to read the first re cord and move the sales amount to the HIGH-SALE and LOW-SALE fields. Define the fields as 05 HIGH-SALE PIC X(5) VALUE LOW-VALUES. and 05 LOW-SALE PIC X(5) VALUE HIGH-VALUES.

Then simply compare the input sales amount to HIGH-SALE and LOW-SALE and move it to HIGH-SALE when it is higher than the current value and to LOW-SALE when it i s lower than the current value ques3:There are 2 files. These 2 files contains some common (duplicate) records. How do you copy these common (matching) records into third file. Suggest the an swer using COBOL as well as through JCL. ans3://sortin dd dsn=a.b.file1,dsn=shr // dd dsn=a.b.file2,dsn=shr //sortout dd dsn=a.d.file3,dsn=old //sysin dd * sort fields=(1,3,ch,a) sum fields=none,xsum /*

Using JCL : Step 1: using pgm SORT, sort and merge the two files SORTIN DD file 1 (i/p file 1) SORTIN DD file 2 (i/p file 2)

SORTOUT DD file 3 (o/p file containing the sorted and merged recs o f both the i/p files) Step 2: Using SUM FIELDS = NONE,XSUM : the duplicates will be eliminated and wri tten in to the file defined in the SORTXSUM DD step Using COBOL : Use the MERGE verb along with the OUTPUT PROCEDURE. In the OUTPUT PROCEDURE, cod e the logic for eliminating the duplicates and writing them in to an output file .

ques4:How do you delete specific record using COBOL? ans4:You can use SORT: copy first 49 record to a file SORT FIELDS=COPY,STOPAFT=49 then copy the rest of the records starting from 51 to another file SORT FIELDS=COPY,SKIPREC=50 then concatinate them to output file.

ques5:What is inspect in COBOL ? ans5:INSPECT is used with TALLYING option to count designated characters. eg: INSPECT WS-A TALLYING WS-COUNT FOR ALL $ INSPECT is used with REPLACING option to change designated characters. eg: INSPECT WS-B REPLACING LEADING BY 0 With INSPECT, TALLYING and REPLACING can be used together as well. eg: INSPECT WS-A TALLYING WS-COUNT FOR ALL $ REPLACING ALL $ BY * To convert all uppercase letters to lowercase, INSPECT can be used with CONVERTI NG. But this doesnt work for all versions of COBOL. eg: INSPECT WS-NAME CONVERTING ABCDEFGHIJKLMNOPQRSTUVWXYZ TO abcdefghijklmnopqrstuvwxyz ques6:what is file status 39 in cobol? ans6:Mismatch in LRECL or BLOCKSIZE or RECFM between your COBOL pgm & the JCL (o r the dataset label). You will get file status 39 on an OPEN. ques7:What is difference between comp & comp-4? ans7: For all of you out who have not heard about comp-4. Wake up. Comp-4 does exist a nd it is just like comp - ie. it is a half-word (2 bytes). This might help those of you who are confused about comp fields: COMP COMP-1 COMP-2 COMP-3 COMP-4 half word 1 to 4 = 2bytes, 5 to 9= 4 bytes 10 to 18 = 8 bytes single word 4 bytes double word 8 bytes (use n/2 +1) where n=length of field half word same as COMP

ques8:difference between include and copy?

ques9:skip alternate records? ans9:Read input file If not End-of-file Add 1 to ws-counter If ws-counter = 2 write record to output file move 0 to ws-counter

end-if end-if end-read ques10:Reformat Output file---i have a file having 1000 records and each record having 8 fields, but i want to output having 1,3,5,7 fields only, then how can i achieve this criteria?? ans10:in sort jcl, //sysin dd * sort fields=copy outrec fields=(starting position in o/p file:starting field in i/p file,length ,starting position in o/p file:starting field in i/p file,length,..........) /* // e.g. assume i have 8 fields in one rec of i/p file each field length 5 bytes, as he ask i want 1,3,5,7 fields only, see //sysin dd * sort fields=copy outrec fields=(1:1,5,6:11,15,11:21,25,16:31,35) /* // ques11:Explain the difference between an internal and an external sort, the pros and cons, internal sort syntax etc? ans11: ques12:stoprun,goback and cancel.

q&a 13: ,THERE ARE 7 SECTION FOR DATA DIVISION: 1.FILE SECTION. 2.WORKING-STORAGE SECTION. 3.SCREEN SECTION. 4.REPORT SECTION. 5.COMMUNICATION SECTION. 6.LOCAL STORAGE SECTION. 7.LINKAGE SECTION. ques13:How to read,write records form bottom from a sequential file in cobol? ans13:It is possible through jcl. For example if the input file contains 80 reco rd length then In sort, INREC=(1:1,80,81:SEQNUM,6,ZD),SORT FIELDS=(81,6,ZD,D),OUTREC=(1:1,80) 1) Using inrec we need to generate the sequential number starts from position 8 1. For example 1) First 1-80 bytes contains the actual record and from position 81 w e need to generate sequential number using inrec i.e 000100, 000200 upto 50 reco

rds. 2) After generating all those numbers, sort we takes place in descend ing order. 3) Using outrec we have to move the first 80 bytes to the output file . After doing this sort in first step in jcl, you can read the output file in seco nd step and write it in the output file generated in second step. ques14:

You might also like