Professional Documents
Culture Documents
Cobol Randy
Cobol Randy
REVISION LIST
Divisions.......................................................................................................................................................1
Levels...........................................................................................................................................................2
Data Types....................................................................................................................................................4
Procedure Division Commands....................................................................................................................6
Performs.....................................................................................................................................................11
Usage..........................................................................................................................................................13
Table Handling...........................................................................................................................................16
Subscripts and Indexes...............................................................................................................................17
Sorting........................................................................................................................................................19
File Handling..............................................................................................................................................20
Subroutines.................................................................................................................................................21
Compiler Options.......................................................................................................................................23
Character Handling.....................................................................................................................................24
Misc............................................................................................................................................................27
Divisions
1
Levels
Example:
01 WS-MARITAL-STATUS PIC X.
88 SINGLE VALUE “Y”.
88 MARRIED VALUE ‘N’.
01 WS-REPLY PIC X.
88 REPLY-YESVALUE “Y”.
88 REPLY-N0 VALUE “N”.
01 WS-MARKS PIC 999.
88 PASSED
VALUE 40 THRU 100.
88 FAILED
VALUE 0 THRU 39.
8. What is the difference between level 77 and 01? And which is more
efficient?
Level 77 can be used to describe independent elementary items in the Working
Storage or Linkage Sections. Level 77 cannot use Redefines.
Level 01 can be used to describe both elementary and group items. Any item
described in Level 01, the system is putting on Double-Word boundary and
inserts slack bytes if necessary
2
9. How many different level numbers can be used in COBOL to describe a
record?
01-49.
Syntax:
<Level> <DataName1> REDEFINES <DataName2>
Example:
01 Sales-Record.
02 Sales-Type Pic X.
02 Sales-By-Unit.
03 Qty Pic 9(4).
03 Unit-Price Pic 9(8)V99.
02 Total-Sales REDEFINES Sales-By-Unit.
03 Amount Pic 9(10)V99.
03 Filler Pic X(2).
3
Data Types
Symbols used
Numeric 9 PSV
Alphabetic A B
Alphanumeric X 9A
Numeric Edited 9 P V and at least one of the editing symbols
B / Z 0 + - * , . CR DB $
Alphanumeric Edited X 9AB0/
Example:,
77 WS-MESSAGE PIC X(30).
77 WS-NUMBER PIC 9999.
16. What are all the possible Editing Characters used in COBOL. Explain
them with examples?
Numeric Data
Z Zero Suppression
* Asterisk
$ Currency Sign
- Minus Sign
+ Plus Sign
CR DB Credit Debit Sign
Period (.)
4
Comma ,)
Blank (‘b’)
Zero (0)
Slash (/)
Blank When Zero
Inserts blanks when data value is zero
Alpha Numeric Data
Blank, Zero,Slash Insertion
Note:
Mutually Exclusive PIC Clauses
CR and DB
$+_ Z*
V.
Example:
5
Procedure Division Commands
Imperative Statement
Directs program to take specific action during execution
ADD 1 TO A.
MOVE 2 TO A.
Conditional Statement
Directs program to examine the truth of a condition and take subsequent action
depending on truth
IF A > B
DISPLAY “A IS GREATER”
ELSE
DISPLAY “B IS GREATER’
END-IF.
Compiler-directing Statement
Directs COBOL compiler and no corresponding statement is generated for these
in the object program
COPY
USE
Explicit Scope Terminators
Statements used to terminate the scope of a COBOL verb explicitly
END-IF
END-PERFORM
6
1. Conditional Transfer statements
Based on result of condition check
IF
EVALUATE
GOTO..DEPENDING ON
2. Unconditional Transfer
PERFORM
GOTO
Example:
GOTO RECEIPT-PARA,
ISSUE-PARA,
ADJUSTMENT-PARA
DEPENDING ON TRAN-TYPE.
Equivalent to
IF TRAN-TYPE = 1
GOTO RECEIPT-PARA.
IF TRAN-TYPE = 2
GOTO ISSUE-PARA.
IF TRAN-TYPE = 3
GOTO ADJUSTMENT-PARA.
SUBTRACT
[CORR]
[Identifier-1 / Literal-1] [Identifier-2 / Literal-2]
[FROM / GIVING] Identifier-3, iodentifier-4, ...
MULTIPLY
{Identifier-1 BY Identifier-2
7
[ , Identifier-3]... Literal - 1}
[GIVING Identifier-4 [ , Identifier-5]...]
DIVIDE {Identifier-1 / Literal-1}
INTO[BY] Identifier-2 { ,Identifier-3}....
[GIVING Identifier-4 [ ,Identifier-5]...]
[REMAINDER Identifier-6].
30. What are the different forms of EVALUATE statement? Give the Syntax
and Example
Syntax:
EVALUATE Subject-1 [ALSO Subject-2]...
{WHEN Object-1 [ALSO Object-2]....
{Imperative-Statement1}...}
{WHEN OTHER
{Imperative-Statement2}...]
[END-EVALUATE.]
8
Example:
EVALUATE TRUE
WHEN MONTH = 4 OR 6 OR 9 OR 11
MOVE 30 TO NO-OF-DAYS
WHEN MONTH = 2
MOVE 28 TO NO-OF-DAYS
WHEN OTHER
MOVE 31 TO NO-OF-DAYS
END-EVALUATE.
36. What are all the arithmetic Verbs and Arithmetic Operations?
Arithmetic Verbs
9
Add
Subtract
Multiply
Divide
Compute
ARITHMETIC OPERATORS
Addition +
Subtraction -
Multiplication *
Division /
Exponentiation **
HIERARCHY
( ) ** * OR / + OR - From Left to Right
10
Performs
PERFORM
PERFORM [Procedure-Name-1
END-PERFORM.
PERFORM THRU
PERFORM [Procedure-Name-1
[{Thru} Procedure-Name-2]]
[Imperative-Statement-1
END-PERFORM.]
PERFORM UNTIL
PERFORM [Procedure-Name-1
[{THRU} Procedure-Name-2]]
[WITH TEST {BEFORE AFTER} UNTIL cond-1]
[Imperative-Statement-1
END-PERFORM.]
E.g.,
PERFORM COUNT-PARA
WITH TEST AFTER
UNTIL WS-COUNT > 10.
11
42. Read the following code and tell how many times will B-PARA be
executed ?
01 ws-n pic 9(2) value zero.
a-para
Move 5 to ws-n.
perform b-para ws-n times.
B-PARA.
move 10 to ws-n.
12
Usage
USAGE COMP1
One word in floating point form
Number represented in hexadecimal
Picture clause cannot be specified
Suitable for arithmetic operations
COMP-2 USAGE
Same as COMP-1 except that data represented internally in two words
Increases precision of the data. Picture clause cannot be specified.
COMP-3 USAGE
Internal Representation In Packed Decimal Form
Each digit and sign occupy 1/2 byte
Hexadecimal number C or F denotes positive sign
Hexadecimal number D denotes negative sign
13
data item whose usage is display and then have that particular data item edited
in the format desired.
48. What is the maximum length of a field you can define using COMP-3?
10 Bytes (S9(18) COMP-3).
49. How is sign stored in Packed Decimal fields and Zoned Decimal fields?
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.
55. How many bytes does a S9(7) SIGN TRAILING SEPARATE field occupy ?
Will occupy 8 bytes (one extra byte for sign).
57. What is the maximum value that can be stored in S9(8) COMP?
99999999
58. What is the maximum size of a 01 level item in COBOL I? in COBOL II?
In COBOL II: 16777215
14
Causes the item to be aligned on natural boundaries. Can be SYNCHRONIZED
LEFT or RIGHT.
For binary data items, the address resolution is faster if they are located at word
boundaries in the memory. For example, on main frame the memory word size
is 4 bytes. This means that each word will start from an address divisible by 4.
If my first variable is x(3) and next
one is s9(4) comp, then if you do not specify the SYNC clause, S9(4) COMP will
start from byte 3 ( assuming that it starts from 0 ). If you specify SYNC, then
the binary data item will start from address 4. You might see some wastage of
memory, but the access to this
computational field is faster.
15
Table Handling
62. What is the maximum number of dimensions that an array can have in
COBOL?.
SEVEN in COBOL - 85 and THREE in COBOL - 84
16
Subscripts and Indexes
17
70. What is binary search?
Search on a sorted array. Compare the item to be searched with the item at the
middle. If it matches, fine else repeat the process with the left half or the right
half depending on where the item lies.
71. My program has an array defined to have 10 items. Due to a bug, I find
that even if the program access the 11th item in this array, the program
does not ABEND. What is wrong with it?
Must use compiler option SSRANGE if you want array bounds checking. Default
is NOSSRANGE.
18
Sorting
74. How do you sort in a COBOL program? Give sort file definition, sort
statement syntax and meaning. -
Syntax:
file-1 is the sort work-file 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 out-file 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.
75. How do you define a sort file in JCL that runs the COBOL program?
Use the SORTWK01, SORTWK02,..... DD names in the step. Number of sort
Datasets depends on the volume of data being sorted, but a minimum of 3 is
required.
19
File Handling
77. How do you reference the following file formats from COBOL programs:
Fixed Block File - Use ORGANISATION IS SEQUENTIAL. Use RECORDING
MODE IS F, BLOCK CONTAINS 0 .
Fixed Unblocked - Use ORGANISATION IS SEQUENTIAL. Use RECORDING
MODE IS F, do not use BLOCK CONTAINS
Variable Block File - Use ORGANISATION IS SEQUENTIAL. Use RECORDING
MODE IS V, BLOCK CONTAINS 0. Do not code the 4 bytes for record length in FD
i.e. JCL REC length will be max rec. length in pgm + 4
Variable Unblocked - Use ORGANISATION IS SEQUENTIAL. Use RECORDING
MODE IS V, do not use BLOCK CONTAINS. Do not code 4 bytes for record length
in FD i.e. 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. Use RECORDING MODE IS
F, BLOCK CONTAINS 0. (Use RECFM=FBA in JCL DCB).
79. What is the mode in which you will OPEN a file for writing? -
OUTPUT, EXTEND
80. In the JCL, how do you define the files referred to in a subroutine ?
Supply the DD cards just as you would for files referred to in the main program.
81. Can you REWRITE a record in an ESDS file? Can you DELETE a record
from it?
Can rewrite(record length must be same), but not delete.
20
Subroutines
85. What is the difference between static call and dynamic call in COBOL?
A statically called program is link-edited into the same load module as the calling
program; a static call is faster than a dynamic call. A static call is the preferred
method if your application does not require the services of the dynamic call.
You cannot delete a statically called program, but you can delete a dynamically
called program. Using a dynamic call and then a CANCEL statement to delete the
dynamically called program after it is no longer needed in the application (and
not after each call to it) might require less storage than using a static call.
Dynamically called modules are those that are not bound with the calling
program at link edit time (IEWL for IBM) and so are loaded from the program
library (job lib or step lib) associated with the job. For DYNAMIC calling of a
module the DYNAM compiler option must be chosen, else the linkage editor will
not generate an executable as it will expect null address resolution of all called
modules. A Statically called module is one that is bound with the calling module
at link edit, and therefore becomes part of the executable load module.
87. PGM-A calls PGM-B. What will happen when PGM-B issues STOP RUN?
The control will not come back to PGM-A after the execution of PGM-B and hence
PGM-B will stop abruptly.
21
88. PGM-A calls PGM-B recursively. What will happen to the Data Items
declared in PGM-B?
The status of Data Items will get changed whenever the PGM-B is called. Thus
when control leaves a subroutine, it is not likely to be in the same state in which
it was entered. If the same sub routine is called again, it will be entered in its
last used state.
Note:
If you want to have the subroutine in its initial state, the subroutine should be
called after having cancelled its current state by means of a CANCEL statement.
22
Compiler Options
89. What compiler option would you use for dynamic linking?
DYNAM.
91. What care has to be taken to force program to execute above 16-MB
line?
Make sure that link option is AMODE=31 and RMODE = ANY.
Compile option should never have SIZE(MAX).
BUFSIZE can be 2K, efficient enough.
RMODE(31) indicates that the program can be loaded either below or above the
line.
RMODE=ANY indicates that the program can be run in either 24 bit (below)or 31
bit memory (above).
These are compile/link edit options.
RMODE(24) - Resides in virtual storage below 16-MB line. Use this for 31 bit
programs that call 24 bit programs. (OS/VS Cobol programs use 24 bit
addresses only).
RMODE(ANY) - Can reside above or below 16 MB line.
23
Character Handling
94. How will you move the part of character field to other character field?
By Reference modification
e.g.,.
To move the first 4 Chars of a variable ABC to XYZ:
MOVE ABC (1:4) TO XYZ
24
Abends
95. What are the causes for S0C1, S0C4, S0C5, S0C7, and S0CB ABENDs?
S0C1 - May be due to
1.Missing or misspelled DD name
2.Read/Write to unopened DataSet
3.Read to DataSet opened output
4.Write to DataSet opened input
5.Called subprogram not found
S0C4 - May be due to
1.Missing Select statement (during compile)
2.Bad Subscript/index
3.Protection Exception
4.Missing parameters on called subprogram
5.Read/Write to unopened file
6.Move data from/to unopened file
S0C5 - May be due to
1.Bad Subscript/index
2.Close an unopened DataSet
3.Bad exit from a perform
4.Access to I/O area (FD) before read
S0C7 - May be due to
1.Numeric operation on non-numeric data
2.Un-initialize working-storage
3.Coding past the maximum allowed sub script
S0CB - May be due to
1.Division by Zero
Many times the reason for SOC7 is an uninitialized numeric item. Examine that
possibility first.
Many installations provide you a dump for run time ABENDs ( it can be
generated also by calling some subroutines or OS services thru assembly
language). These dumps provide the offset of the lastinstruction at which the
ABEND occurred. Examine the compilation
output XREF listing to get the verb and the line number of the source code at
this offset. Then you can look at the source code to find the bug. To get
capture the runtime dumps, you will have to define some Data Sets
(SYSABOUT etc ) in the JCL.
If none of these are helpful, use judgement and DISPLAY to localise the source
of error.
Some installation might have batch program debugging tools. Use them.
25
A S0C1 occurs if the CPU attempts to execute binary code that isn't a valid
machine instruction; e.g. if you attempt to execute data. A S0C4 is a memory
protection violation. This occurs if a program attempts to access storage beyond
the areas assigned to it.
26
Misc.
98. What is the Structured programming and how do you identify it?
The difference between regular programming approach and structured
programming is that structured programs get rid of GO TO statements and the
whole programs can be represented in a Top-Down design. This design is
possible because of existence of 3 basic structures:
c. Iteration Structure: Within iteration structure the execution of the function will
be reiterated over and over again until condition becomes true. The structure
has one entry point and one exit point. All these structures have one entry point
and one exit point. Because of this if the programs is written by using only these
structures, the whole programs will have one entry point and one exit point.
27
OS/VS Cobol pgms can only run in 24 bit addressing mode, VS Cobol II pgms
can run either in 24 bit or 31 bit addressing modes.
Report writer is supported only in OS/VS Cobol.
USAGE IS POINTER is supported only in VS COBOL II.
Reference modification e.g.: WS-VAR(1:2) is supported only in VS COBOL II.
EVALUATE is supported only in VS COBOL II.
Scope terminators are supported only in VS COBOL II.
OS/VS Cobol follows ANSI 74 stds while VS COBOL II follows ANSI 85 stds.
Under CICS Calls between VS COBOL II programs are supported.
Note: In using COBOL on PC we have only flat files and the programs can access
only limited storage, whereas in VS COBOL II on M/F the programs can access
up-to 16MB or 2GB depending on the addressing and can use VSAM files to
make I/O operations faster.
105. What's a LDS(Linear Data Set) and what's it used for ?(VSAM)
LDS is a VSAM DataSet in name only. It has unstructured 4k (4096 bytes) fixed
size CIs which do not contain control fields and therefore from VSAM's
standpoint they do not contain any logical records. There is no FreeSpace, and
no access from Cobol. Can be accessed by DB2 and IMS fast path DataSet. LDS
is essentially a table of data maintained on disk. The 'table entries' must be
created via a user program and can only be logically accessed via a user
program. When passed, the entire LDS must be mapped into storage, then data
is accessed via base and displacement type processing.
106. What are the steps you go through while creating a COBOL program
executable?
DB2 precompiler (if embedded SQL used),
CICS translator (if CICS pgm),
Cobol compiler,
Link editor.
*********************************************************
*********************************************************
28
29