You are on page 1of 10

Cobol 1. How do you call subprograms in COBOL?

Subprograms are called using CALL statement. If Program A calls program B and pass parameters x,y then Call statement can be written in program A as CALL B USING X,Y. Similarly in Prog B, we should have procedure division as PRODCEDURE DIVISION USING X,Y.

2. What is difference between static call and dynamic call?


STATIC Call: 1) Identified by Call literal. Ex: CALL PGM1. 2) Default Compiler option is NODYNAM and so all the literal calls are considered as static calls 3) If the subprogram undergoes change, sub program and main program need to be recompiled 4) Sub modules are link edited with main module. 5) Size of load module will be large 6) Fast,Less flexible. 7) Sub-program will not be in initial stage the next time it is called unless you explicitly use INITIAL or you do a CANCEL after each call. DYNAMIC CALL 1) Identified by Call variable and the variable should be populated at run time. 01 WS-PGM PIC X(08). Move PGM1 to WS-PGM CALL WS-PGM 2) If you want convert the literal calls into DYNAMIC, the program should be compiled with DYNAM option. By default, call variables and any un-resolved calls are considered as dynamic 3) If the subprogram undergoes change, recompilation of subprogram is enough. 4) Sub modules are picked up during run time from the load library. 5) Size of load module will be less.

6) Slow compared to Static call. More flexible. 7) Program will be in initial state every time it is called. Please note difference between call identifier and call literal. CALL identifier, where identifier is a data item that contains the name of a nonnested subprogram when the program is run, always results in the target subprogram being loaded when it is called. Also, the name of the shared library must match the name of the target entry point. CALL literal, where literal is the explicit name of a nonnested subprogram being called, can be resolved statically or dynamically. If the NODYNAM compiler option is in effect, either static or dynamic linking can be done. If DYNAM is in effect, CALL literal is resolved in the same way as CALL identifier: the target subprogram is loaded when it is called, and the name of the shared library must match the name of the target entry point. These call definitions apply only in the case of a COBOL program calling a nonnested program. When a COBOL program calls a nested program, the CALL is resolved by the compiler without any system intervention. Your COBOL programs can call a subprogram that is linked into the same executable module as the caller (static linking) or that is provided in a shared library (dynamic linking). COBOL for AIX also provides for run-time resolution of the target subprogram from a shared library. If you link the target program statically, it is part of the executable module of the caller and is loaded with the caller. If you link it dynamically or resolve the call at run time, it is provided in a library and is loaded either when the caller is loaded or when it is called. Static linking and dynamic linking are done for COBOL CALL literal subprograms only. Run-time resolution is always done for COBOL CALL identifier and is done for CALL literal when the DYNAM option is in effect.

3. How would you write static call?


Following example shows how static call can be written PROCESS NODYNAM NODLL IDENTIFICATION DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 RECORD-2 PIC X. (6) 01 RECORD-1. (2) 05 PAY PICTURE S9(5)V99. 05 HOURLY-RATE PICTURE S9V99. 05 HOURS PICTURE S99V9. ...

PROCEDURE DIVISION. CALL "SUBPROG" USING RECORD-1. (1) CALL "PAYMASTR" USING RECORD-1 RECORD-2. (5) STOP RUN. For calling subprogram SUBPROG, CALL literal is used. Moreover program needs to be compile with NODYNAM OPTION. Also program needs to be statically link edited like below and link card should INCLUDE subprogm also in link editing. this will cause, subprogram to be link edited in the main program itself.

Cobol Table handling


1. Runs scored by 15 players into 25 matches needs to be stored into array. What kind of array is required to store the same - single or multidimensional? Define an array. Array can be defined as 01 PLAYER-DATA. 05 PLAYER-NAME OCCURS 15 TIMES INDEXED BY PL-INDEX PIC X(25). 10 MATCH-RUNS OCCURS 25 TIMES INDEXED BY RUNINDEX. 15 MATCH RUN PIC 9(3). To store runs of 25 matches for 15 players, two dimensional array will be required. PLAYER-NAME stores name of player which occurs 15 times and it has been defined as X(25). Also for each player, MATCH-RUNS has been defind which occurs 25 times. 2. In above two dimensional array, if runs scored by SACHIN in each match needs to be displayed, how it can be done? Using Search Serial search can be used. First Player Name = "SACHIN" is found using PL-INDEX. Once it is found then using PL-INDEX, all the run scored by SACHIN can be found out. SET PL-INDEX TO 1 SET RUN-INDEX TO 1 SEARCH PLAYER-DATA WHEN PLAYER-NAME(PL-INDEX) = "SACHIN DISPLAY 'PLAYER FOUND' AT END PERFORM P100-EXIT

"

END-SEARCH SET PL-INDEX DOWN BY 1. PERFORM UNTIL RUN-INDEX < 25 DISPLAY PLAYER-NAME (PL-INDEX,RUN-INDEX) END-PERFORM. P1000-EXIT. EXIT. Using Perform loop This task is quite easy using perform loop SET PL-INDEX TO 1 SET RN-INDEX TO 1 PERFORM UNTIL END-OF-TABLE IF PL-INDEX = 15 SET END-OF-TABLE TO TRUE IF PLAYER-NAME(PL-INDEX) = "SACHIN PERFORM UNTIL RUN-INDEX < 25 DISPLAY PLAYER-NAME (PL-INDEX,RUN-INDEX) END-PERFORM SET END-OF-TABLE TO TRUE END-IF END-PERFORM 3. What is sequential search and Binary search ? In Sequential search, table elements are searched sequentially using SEARCH verb. When table element is found, control comes out of search. If table element is not found, action defined at AT END verb is performed. In Binary search, table must be ordered(sorted in ascending or descending) on the KEYS specified in the occurs clause.The test in when condition must be always equal condition(same is not true with Sequential search. In sequential search we can have unequal conditions in WHEN). Also we can have only one when condition in binary search (Whereas in sequential search, we can have mutilple when conditions) In binary search data is search using SEARCH-ALL verb. Table will be devided into two parts and key will be tested. If the key to be searched lies in first part, then second part will be elliminated and search will continue in first part.Process "

will repeat until key is found or no specified key found in table. If no specified key found, then action specified at AT END clause is performed. 4. Sequential search or Binary search - When to use what? Serial search is used if table to be searched is relatively small. Binary search is more efficient if size of table to be searched in big. Also for binary search, table needs to have key and data must be in sorted order on a key. If the data is not like this , then serial search is performed. 5. Can SEARCH and SEARCH ALL be used to find records in a sequential file? SEARCH and SEARCH all cannot be used to find data in sequental file directly. They can be used with tables which are declared in the program in DATA division. However, file records can be loaed into table and they can be searched. 6. What is difference between index and subscript ? Index and subscript are used for refering a data element into table. A subscript is a working storage data definition item, typically an interger PIC (999) where a value must be moved to the subscript and then incremented or decremented by ADD TO and SUBTRACT FROM statements. Subscript represents an occurence number of a table within a table. An index is a register item that exists outside the program's working storage. You SET an index to a value and SET it UP BY value and DOWN BY value. An index is displacement from the start of table based upon length of table element. index values can be changed by SET, PERFORM and SEARCH. 7. A table needs to be defined to store the names of students in the class. Maximum number of students in class can be 100. Table needs to be defined to store names of only those students who have taken admissions. Number of students who have taken admission can be retrieved from WS-ADM-COUNT. Define an table. Table can be defined in following way 01. WS-ADM-COUNT PIC 9(2). 01. WS-STUDENTS. 05. WS-STUDENT-NAME OCCURS 1 TO 100 TIMES DEPENDING ON WS-ADM-COUNT PIC X(25).

With above definition, WS-STUDENTS will be variable length table. Depending upon value in WS-ADM-COUNT, occurences of WS-STUDENT-NAME will be set. if WS-ADM-COUNT has value 50, then above table will have data for 50 occurences or allocate data into storage for 50 occurences. 8. How to set an WS-INDEX to 25? How to set WS-INDEX to WS-INDEX-2 To set WS-INDEX to 25 -> SET WS-INDEX TO 25 To set WS-INDEX to WS-INDEX-2 -> SET WS-INDEX DOWN BY 2. 9. If 10 Dimension table needs to be defined in COBOL? Define any 10 dimensional table? Tables upto 7 dimensions can be defined in COBOL 10. A two dimensional array has been defined in following way. What it does? 01. WS-TABLE OCCURS 10 TIMES PIC X(3). 05 WS-TABLE2 OCCURS 5 TIMES PIC X(5). Above definition is wrong, since OCCURS clause cannot be specified at 01 level.If two dimensional array is required, above defintion can be modified as 01. WS-TABLE-GROUP. 05 WS-TABLE OCCURS 10 TIMES PIC X(3). 10 WS-TABLE2 OCCURS 5 TIMES PIC X(5).

Cobol File handling

1. Explain how files are handled in COBOL program?


Files must be declared in in INPUT-OUTPUT SECTION (In ENVOIRNMENT DIVISION) under FILE CONTROL.Logical file name is assgined using SELECT clause to file. Also organization (Whether indexed, sequential) and access mode (Seqential,random, dynamic) specified here. Then in FILE SECTION of DATA DIVISION, FD file descriptor is specified for the file where record length , type of record (FB,VB) is specified along with record layout. Then in prodcedure division file is opened as input,input-output,output mode.

records can be read using READ and can be written using WRITE/REWRITE statement. Files can be closed using CLOSE statment in PROCEDURE DIVISION. 2. A MASTER-FILE has been opened in I-O mode.Following are the records in the file. CAPGEMINI, PUNE ACCENTURE,PUNE INFOSYS,PUNE COGNIZAT,PUNE. And following COBOL statements are exected in procedure division. READ MASTER-FILE READ MASTER-FILE MOVE 'WIPRO,PUNE' TO MASTER-REC WRITE MASTER-FILE FROM MASTER-REC. Then what will be the contents of the file? Since MASTER-FILE has been opened in I-O mode and WRITE statement is trying to write in the same file, we will get invalid file status and no WRITE operations will be performed. Files which are delcared as I-O, only REWRITE statment can write the records. Hence contenets of the file will be same.

3. A MASTER-FILE has been opened in I-O mode.Following are the records in the file.

CAPGEMINI, PUNE ACCENTURE,PUNE INFOSYS,PUNE COGNIZAT,PUNE. And following COBOL statements are exected in

procedure division. READ MASTER-FILE READ MASTER-FILE MOVE 'WIPRO,PUNE' TO MASTER-REC REWRITE MASTER-FILE FROM MASTER-REC. REWRITE MASTER-FILE FROM MASTER-REC. Then what will be the contents of the file?
After execution of above statements of contents of the file will be CAPGEMINI, PUNE WIPRO,PUNE INFOSYS,PUNE COGNIZAT,PUNE. File has been opened in input output mode. And first two records are read. After that pointer now points to the second record in the file. Now if REWRITE operation is performed, pointer will point to the second record and second record will be overwritten. After rewrite, pointer will still point to the second record in the file and same record will be overwritten.

4. Program A has opened a file in output mode. Program A calls Program B. Since file has already opened in Program A, can program B direcly write into that file?
If program B has to write into the files, it cannot directly write into the file. In both Programs Prog A and Prog B, file should be defined as external in FD. Also in both the programs, file should be defined in FILE CONTROL using SELECT clause. PROG A opens a file and calls prog B. Since file is already opened in PROG A, no open statement required in PROG B. And Then Prog B can write into the file.

5. If records needs to be deleted from a ps file, Which one of the following verbs can be used? DELETE ERASE
None of the above verbs can be used to delete the record from the PS file.

6. Can 88 levels be defined in 01 level under FD?88


Levels can be defined under 01 - file layout under FD.

7. In a physically sequential file, after reading 100 records , if again first records needs to be read, then which one of the following needs to be done? A.Address of the first record from a file needs to be stored into pointer using ADDRESS OF keyword. After reading 100 records, point to the address stored into pointer using SET verb. In this way records can be read from the start again. B. Close the file, OPEN it again and read it from the start.
COBOL does not support address handling for the files. so first option is not possible. Second option is easy. Since it is sequential file and records need to be read sequentially, closing the file, oepning it and reading the records from the start is the only option available.

8. Can COPY statement is used to copy all the content of one file to another file?

COPY statement cannot be used for file handling. If all the conetnts from one file needs to be copied to another file, the source file needs to be opened in

INPUT mode and target file needs to be opened as OUTPUT. Records from INPUT file needs to be read one by one and at a same time, those should be written in OUTPUT file.

9. If ORGANIZATION is defined as SEQUENTIAL and ACCESS MODE is defined as INDEXED in SELECT clasue, then the file must be one of these -> KSDS, RRDS,LDS,ESDS,PS?
If ORGANIZATION IS SEQUENTIAL then the ACCESS MODE must be SEQUENTIAL ONLY. And these properties are for PS file only. For KSDS, ORGANIZATION should be INDEXED and access mode can be defined as SEQUENTIAL, RANDOM, DYNAMIC. For ESDS, ORGANIZATION should be sequential and access mode can be defined as sequential. for RRDS, ORGANIZATION should be relative and access mode can be defined as SEQUENTIAL, RANDOM, DYNAMIC.

10. If the records need to be written at the end of file, then how it can be done?
If the records needs to be added at the end of file, then files should be opened in EXTEND mode.

11. 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).

You might also like