COBOL - FAQs


1. What is a scope terminator? Give examples.

Scope terminator is used to mark the end of a verb e.g. EVALUATE, END-EVALUATE; IF, END-IF.

2. How do you do in-line PERFORM? - GS

PERFORM ... ...

END PERFORM

3. 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 (used from various other places in the program), it would be better to put the code in a separate para and use PERFORM paraname rather than in-line perform.

4. What is the difference between CONTINUE & NEXT SENTENCE ?

CONTINUE is like a null statement (do nothing) , while NEXT SENTENCE transfers control to the next sentence (!!) (A sentence is terminated by a period)

5. What does EXIT do ?

Does nothing ! If used, must be the only sentence within a paragraph in OSVS COBOL. In COBOL II you can have other statements along with EXIT.

6. Can I redefine an X(100) field with a field of X(200)?

Yes. Redefines just causes both fields to start at the same location. For example:

01 WS-TOP PIC X(1)

01 WS-TOP-RED REDEFINES WS-TOP PIC X(2).

If you MOVE ‘12’ to WS-TOP-RED,

DISPLAY WS-TOP will show 1 while

DISPLAY WS-TOP-RED will show 12.

This works only if the level number is 01. If the respective level numbers were say 05, then you will get a severe error in compilation.

7. Can I redefine an X(200) field with a field of X(100) ?

Yes.

8. What do you do to resolve SOC-7 error? - GS

Basically you need to correct the offending data.

Many times the reason for SOC7 is an un-initialized 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 last instruction 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 datasets (SYSABOUT etc ) in the JCL.

If none of these are helpful, use judgement and DISPLAY to localize the source of error.

Some installtion might have batch program debugging tools. Use them.

9. 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 byte.

10. How is sign stored in a comp-3 field? - GS

It is stored in the last nibble. For example if your number is +100, it stores hex 0C in the last byte, hex 1C if your number is 101, hex 2C if your number is 102, hex 1D if the number is -101, hex 2D if the number is -102 etc...

11. How is sign stored in a COMP field ? - GS

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

12. What is the difference between COMP & COMP-3 ?

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

13. What is COMP-1? COMP-2?

COMP-1 - Single precision floating point. Uses 4 bytes.

COMP-2 - Double precision floating point. Uses 8 bytes.

14. How do you define a variable of COMP-1? COMP-2?

No picture clause to be given. Example 01 WS-VAR USAGE COMP-1.

15. How many bytes does a S9(7) COMP-3 field occupy ?

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

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

16. How many bytes does a S9(7) SIGN TRAILING SEPARATE field occupy ?

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

17. How many bytes will a S9(8) COMP field occupy ?

4 bytes.

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

99999999

19. What is COMP SYNC?

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.

20. What is the maximum size of a 01 level item in COBOL I? in COBOL II?

In COBOL II: 16777215

21. 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 ie 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 ie 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).

22. What are different file OPEN modes available in COBOL?

Open for INPUT, OUTPUT, I-O, EXTEND.

23. What is the mode in which you will OPEN a file for writing? - GS

OUTPUT, EXTEND

24. 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.

25. 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.

26. What is file status 92? - GS

Logic error. e.g., a file is opened for input and an attempt is made to write to it.

27. What is file status 39 ?

Mismatch in LRECL or BLOCKSIZE or RECFM between your COBOL pgm & the JCL (or the dataset label). You will get file status 39 on an OPEN.

28. What is Static, Dynamic linking?

In static linking, the called subroutine is link-edited into the calling program , while in dynamic linking, the subroutine & the main program will exist as separate load modules. You choose static/dynamic linking by choosing either the DYNAM or NODYNAM link edit option. (Even if you choose NODYNAM, a CALL identifier (as opposed to a CALL literal), will translate to a DYNAMIC call).

A statically called subroutine will not be in its initial state the next time it is called unless you explicitly use INITIAL or you do a CANCEL. A dynamically called routine will always be in its initial state.

29. What is AMODE(24), AMODE(31), RMODE(24) and RMODE(ANY)? ( applicable to only MVS/ESA Enterprise Server).

These are compile/link edit options.

AMODE - Addressing mode. RMODE - Residency mode.

AMODE(24) - 24 bit addressing. AMODE(31) - 31 bit addressing. AMODE(ANY) - Either 24 bit or 31 bit addressing depending on RMODE.

RMODE(24) - Resides in virtual storage below 16 Meg line. Use this for 31 bit programs that call 24 bit programs. (OS/VS Cobol pgms use 24 bit addresses only).

RMODE(ANY) - Can reside above or below 16 Meg line.

30. What compiler option would you use for dynamic linking?

DYNAM.

Db2 Faqs

1. In the WHERE clause what is BETWEEN and IN? - GS

BETWEEN supplies a range of values while IN supplies a list of values.

2. Is BETWEEN inclusive of the range values specified? - GS

Yes.

3. What is 'LIKE' used for in WHERE clause? What are the wildcard characters? - GS

LIKE is used for partial string matches. ‘%’ ( for a string of any character ) and ‘_’ (for any single character ) are the two wild card characters.

4. When do you use a LIKE statement?

To provide partial search facility e.g. to search employee by name, you need not specify the complete name, using LIKE, you can search for partial string matches.

5. What is the meaning of underscore ( ‘_’ ) in the LIKE statement? - GS

Match for any single character.

6. What do you accomplish by GROUP BY .... HAVING clause? - GS

Can think of the HAVING as a ‘WHERE’ clause on the GROUP

7. What is a cursor? why should it be used? - GS

When multiple of rows are to be retrieved with embedded SQL, a cursor should be used.

8. Where would you specify the DECLARE CURSOR statement? - GS

See answer to next question.

9. How do you specify and use a cursor in a COBOL program? - GS

Use DECLARE CURSOR statement either in working storage or in procedure division(before open cursor), to specify the SELECT statement. Then use OPEN, FETCH rows in a loop and finally CLOSE.

10. How would you retrieve rows from a DB2 table in embedded SQL? - GS

Either by using the single row SELECT statements,or by using the CURSOR.

11. What is the COBOL picture clause for a DB2 column defined as DECIMAL(11,2)? - GS

PIC S9(9)V99 COMP-3.

Note: In DECIMAL(11,2), 11 indicates the size of the data type and 2 indicates the precision.

12. What is DCLGEN ? - GS

DeCLarations GENerator: used to create the host language copy books for the table definitions. Also creates the DECLARE table.

13. What are the contents of a DCLGEN? - GS

1. EXEC SQL DECLARE TABLE statement which gives the layout of the table/view in terms of DB2 datatypes.

2. A host language copy book that give the host variable definitions for the column names.

14. Is it mandatory to use DCLGEN? If not, why would you use it at all? - GS

It is not mandatory to use DCLGEN.

Using DCLGEN, helps detect wrongly spelt column names etc. during the precompile stage itself ( because of the DECLARE TABLE ). DCLGEN being a toll, would generate accurate host variable definitions for the table reducing chances of error.

15. Is DECLARE TABLE in DCLGEN necessary? Why it used?

It not necessary to have DECLARE TABLE statement in DCLGEN. This is used by the pre-compiler to validate the table-name, view-name, column name etc.

16. What is EXPLAIN? - GS

EXPLAIN is used to display the access path as determined by the optimizer for a SQL statement. It can be used in SPUFI (for single SQL statement ) or in BIND step (for embedded SQL ).

17. What do you need to do before you do EXPLAIN?

Make sure that the PLAN_TABLE is created. Do RUNSTATS. ( Any thing else? )

18. Where is the output of EXPLAIN stored? - GS

In userid.PLAN_TABLE

19. EXPLAIN has output with MATCHCOLS = 0. What does it mean? – GS

While doing the accesspath, objects accessed have matching cols as 0, in other words index scan or TS scan is used depending on access type R or I

20. How do you do the EXPLAIN of a dynamic SQL statement?

Using Spufi. At bind time, accesspath are not defined. So all permutation and combinations of SQLs need to be examined separately in spufi.

21. How do you simulate the EXPLAIN of an embedded SQL statement in SPUFI/QMF? Give an example with a host variable in WHERE clause.)

22. What are the isolation levels possible ? - GS

CS: Cursor Stability – A read lock is released as soon as access is moved to next page

RR: Repeatable Read – Lock is released only after commit or end of execution

23. Where do you specify them ?

ISOLATION LEVEL is a parameter for the bind process.

24. I use CS and update a page. Will the lock be released after I am done with that page?

No

25. What is ALTER ? - GS

SQL command used to change the definition of DB2 object.

26. What are PACKAGES ? - GS

They contain executable code for SQL statements. Can contain SQL statements for only one DBRM.

Non Executable accesspath instruction for a DBRM.

27. What are the advantages of using a PACKAGE?

In Online Systems – Where there is normally one plan used, You do not have to bind the plan again. So downtime is reduced.

If packages are used than binding changed program is required, however in other case you will have to bind whole plan.

28. What is RUNSTATS? - GS

A DB2 utility used to collect statistics about the data values in tables which can be used by the optimizer to decide the access path. These statistics are stored in DB2 catalog tables and is critical in determining accesspaths for a SQL

29. When will you chose to run RUNSTATS?

After a load, or after mass updates, inserts, deletes, or after REORG. And Periodically

30. Give some example of statistics collected during RUNSTATS?

Col Count – Number of unique values in a column (if high index access is used), Number of columns populated.

31. In SPUFI suppose you want to select max. of 1000 rows , but the select returns only 200 rows. What are the 2 sqlcodes that are returned? - GS

100 ( for successful completion of the query ), 0 (for successful COMMIT if AUTOCOMMIT is set to Yes).

32. How would you print the output of an SQL statement from SPUFI? - GS

Print the output dataset.

33. How do you pull up a query which was previously saved in QMF ? - GS

34. How do you select a row using indexes? - GS

Specify the indexed columns in the WHERE clause.

35. Lot of updates have been done ona table due to which indexes have gone haywire. What do you do? – GS

Reorg

36. What are split indexes? ( exact question is not known ) GS

Ramesh, I think they were talking about what do you do if index splits occur due to mass inserts/updates. I think the answer is a REORG is required.

37. What happens when you say OPEN CURSOR?

If there is an ORDER BY clause, rows are fetched, sorted and made available for the FETCH statement. Other wise simply the cursor is placed on the first row.

In case of Open Cursor

If Cursor statement is complex (i.e. multiple tables accessed, access path use multiple indexes etc.) than materialization happenes and a temporary table is created.

In other case cursor is simply placed on first row depending on Order By clause

38. Is DECLARE CURSOR executable?

No.

39. Can you have more than one cursor open at any one time in a program ? - GS

Yes.

40. How would you find out the total number of rows in a table? - GS

Use SELECT COUNT(*) ...

41. How do you eliminate duplicate values in SELECT? - GS

Use SELECT DISTINCT ...

42. How do you find the maximum value in a column? - GS

Use SELECT MAX(...

43. What does the sqlcode of -818 pertain to? - GS

This is generated when the consistency tokens in the DBRM and the load module are different.

44. What is UNION,UNION ALL? - GS

UNION : eliminates duplicates

UNION ALL: retains duplicates

Both these are used to combine the results of different SELECT statements.

45. Suppose I have five SQL SELECT statements connected by UNION/UNION ALL, how many times should I specify UNION to eliminate the duplicate rows? - GS

Once.

46. What is dynamic SQL? - GS
Dynamic SQL is a SQL statement created at program execution time.

47. What else is there in the PLAN apart from the access path? - GS

PLAN has the executable code for the SQL statements in the host program

48. When is the access path determined for dynamic SQL? - GS

At run time, when the PREPARE statement is issued.

49. Suppose I have a program which uses a dynamic SQL and it has been performing well till now. Off late, I find that the performance has deteriorated. What happened? - GS

Probably RUN STATS is not done and the program is using a wrong index due to incorrect stats.

Probably RUNSTATS is done and optimizer has chosen a wrong access path based on the latest statistics.

50. Apart from cursor, what other ways are available to you to retrieve a row from a table in embedded SQL? - GS

Single row SELECTs.

51. How do you retrieve the data from a nullable column? - GS

Use null indicators. Syntax INTO :HOSTVAR:NULLIND

52. What is the picture clause of the null indicator variable? - GS

S9(4) COMP.

53. What does it mean if the null indicator has -1, 0, -2

-1 : the field is null

0 : the field is not null

-2 : the field value is truncated

54. How do you insert a record with a nullable column?

To insert a NULL, move -1 to the null indicator

To insert a valid value, move 0 to the null indicator

55. Some questions on concatenate, substring features of DB2. - GS

56. What is IMAGECOPY ? - GS

It is full backup of a DB2 table which can be used in recovery.

57. When do you use the IMAGECOPY? - GS

To take routine backup of tables

After a LOAD with LOG NO

58. What is a clustering index ? - GS

A mandatory index defined on a partitioned table space. Causes the data rows to be stored in the order specified in the index. Obviously a table can have only one clustering index.

59. What are correlated subqueries? - GS

.

One in which the lower level nested select refers back to the table in the higher level.

60. What are the issues related with correlated subqueries? - GS

61. What is sqlcode -922 ?

62. What is sqlcode -811?

63. What is a DBRM, PLAN ?

DBRM: DataBase Request Module, has the SQL statements extracted from the host language program by the SQL precompile.

PLAN: A result of the BIND process. It has the executable code for the SQL statements in the DBRM.

64. What happens to the PLAN if index used by it is dropped?
Plan is marked as invalid. The next time the plan is invoked, it is recreated.

65. What is the difference between primary key & unique index?

66. What is QUIESCE?

A QUIESCE flushes all DB2 buffers on to the disk. This gives a correct snapshot of the database and should be used before any IMAGECOPY to maintain consistency.

67. Are views updatable ?

No comments: