Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 38 ยท Projects and Practice

Practice Problems and Interview Questions

Test your COBOL skills with graded practice exercises on tables, files, subprograms and reports, a debugging checklist, and the interview questions about PICTURE, COMP-3, SEARCH, file status, JCL and abends.

You now know the language, the file handling and the mainframe world around it. This last lesson is for making it stick. It has three parts: exercises graded from easy to hard, a debugging checklist, and the questions interviewers actually ask for COBOL roles, with short answers. Do the exercises on a real compiler if you can (GnuCOBOL is free); the stand-ins on this page help you check your thinking.

Part 1: Exercises

Write each program in free or fixed format as you prefer. Hints follow the list, not solutions, so you can try first.

Level 1: warm-up

  1. Read 10 numbers into a table with OCCURS 10 TIMES, then display the largest, the smallest and the average (use ROUNDED). See tables.
  2. Print the multiplication table of any number from 1 to 12 using PERFORM VARYING and an edited picture so columns line up.
  3. Count vowels, consonants and spaces in a text line with INSPECT ... TALLYING.

Level 2: files and tables

  1. Copy a sequential file, dropping records whose first character is *. Print the number of records read and written. (Sequential files)
  2. Load a state-tax table of 50 entries from a file into OCCURS 50 with INDEXED BY, then look up codes read from the keyboard using SEARCH ALL; print an error for unknown codes. (SEARCH)
  3. Read a sales file with region and month and fill a 2-level table REGION (4) x MONTH (12); print totals per region, per month and the grand total. (Two-level tables)
  4. Add file status checking to exercise 4 and make the program end with RETURN-CODE 8 when the input is empty and 16 when the file is missing. (File status)

Level 3: batch problems

  1. Sort a customer file by state and then by descending balance with SORT, and print a report with a subtotal per state. (Sort and merge)
  2. Merge two sorted files into one and report keys that occur in both. (Merge)
  3. Create an indexed file of products, then write a program with a menu to add, read, update and delete by product code. Use FILE STATUS and INVALID KEY everywhere. (Indexed files)
  4. Write a subprogram DATECHK that receives a date YYYYMMDD and returns 0 for valid or 1 to 3 for invalid year, month or day (remember leap years). Call it from a driver with a table of test dates. (CALL)
  5. Extend the payroll project with page breaks, validation and an error file.

Level 4: challenge

  1. Write a recursive program that walks a parts hierarchy file (part, parent) and prints an indented explosion. (Recursion)
  2. Finish the account update including the correct handling of a new account followed by transactions on it, and a control report that balances input and output totals.
  3. Generate JSON from the payroll results with JSON GENERATE and write one object per employee. (JSON)

Hints

  • Exercise 1: initialize the maximum with the first element, not with zero, or negative inputs break it. The counter must be wide enough for 11 to end the loop.
  • Exercise 3: INSPECT line TALLYING n FOR ALL "a" "e" "i" "o" "u"; compare the lowercased text with FUNCTION LOWER-CASE.
  • Exercise 5: the table must be loaded in key order; add a check that stops the program if the file is out of sequence.
  • Exercise 8: the output procedure sees records in key order, so a control break works as in the payroll project.
  • Exercise 11: days in month 2 depend on FUNCTION MOD: divisible by 4 and not by 100, or divisible by 400.
  • Exercise 13: per-call data belongs in LOCAL-STORAGE; the base case is a part with no children.

Check your numeric understanding

Two things trip up almost every beginner: what MOVE does with numbers that do not fit, and what an edited picture prints. The stand-ins below model both so you can predict results before you run a real compiler.

Python Stand-in: MOVE numeric to a numeric field (truncation and alignment)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You should read: 12345.678 becomes digits 34567 (value 345.67), because the leading 12 and the trailing 8 do not fit; 7.5 becomes 00750; -42.0 into an unsigned field becomes 42.00; and 0.999 into PIC 9V9 becomes 0.9. Nothing is rounded unless you use COMPUTE ... ROUNDED, and no error occurs unless you use ON SIZE ERROR (and only arithmetic statements have it).

Python Stand-in: what edited pictures print

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two columns show zero suppression with spaces (Z) and with asterisks (*, the cheque-protection style). A 9 in an edited picture always prints a digit, so ZZZ,ZZ9.99 shows 0.00 for zero rather than blanks.

Debugging checklist

When a program misbehaves, run through these steps in order. They cover the large majority of real bugs.

Check the data first

PIC and USAGE Does every field match the file layout?
Record length Compare FD with LRECL
Initialize Zero and space your fields

Most bad output is bad data, not bad logic.

1 / 3

Part 2: Interview questions

Language basics

  1. What are the four divisions? Identification (program name), Environment (files and machine), Data (all data descriptions) and Procedure (the logic). Environment and Data hold configuration and descriptions; only the Procedure Division has executable statements.
  2. What is the difference between PIC 9, PIC X and PIC A? Numeric digits only, any character, letters and spaces only. Arithmetic needs numeric items; an X field that contains digits is still text.
  3. What does PIC S9(5)V99 mean? A signed number with five integer digits and two decimals, where V is an implied decimal point that takes no storage.
  4. Explain COMP, COMP-3 and DISPLAY. DISPLAY stores one character per digit, COMP binary, COMP-3 packed decimal (two digits per byte, sign in the last nibble). Packed is compact and exact for money. See mainframe environment.
  5. What are level numbers 01, 05, 77, 88 and 66? 01 a record or standalone item, 05 and others subordinate fields, 77 an independent elementary item, 88 a condition name (a boolean test on a value), 66 a RENAMES regrouping.
  6. MOVE versus COMPUTE? MOVE copies data with alignment, padding and truncation rules; COMPUTE evaluates an arithmetic expression.
  7. What is the difference between PERFORM THRU and an inline PERFORM? PERFORM A THRU B runs a range of paragraphs and needs care that nothing jumps out; an inline PERFORM ... END-PERFORM keeps the statements together and is preferred.

Tables and files

  1. Subscript versus index? A subscript is an ordinary numeric item that is multiplied by the entry length at each use; an index (INDEXED BY) holds a displacement, is set with SET, and is faster. See indexes and SEARCH.
  2. SEARCH versus SEARCH ALL? Linear scan, no order needed, any condition; binary search, needs sorted keys and an equality test.
  3. Sequential, indexed and relative organization? In order from first to last; by unique key through an index; by record number. VSAM equivalents: ESDS, KSDS, RRDS.
  4. What is FILE STATUS? A two-character field updated after each I/O statement: 00 success, 10 end of file, 22 duplicate key, 23 not found, 35 file not found. See file status.
  5. AT END versus INVALID KEY? The first handles end of file when reading sequentially; the second handles key problems on keyed files.
  6. How do you sort in COBOL? SORT with USING/GIVING or INPUT/OUTPUT PROCEDURE (with RELEASE and RETURN), the sort work file described by SD; MERGE for already-sorted files. See sorting.

Programs and structure

  1. CALL by reference versus by content? The callee gets the address of the caller's item (it can change it) versus a temporary copy. CALL.
  2. Static versus dynamic call? A literal name usually links the subprogram in at link-edit time; a data-name call loads it at run time and allows CANCEL.
  3. What is a copybook? A source file included with COPY (optionally with REPLACING) so record layouts are written once. See copybooks.
  4. WORKING-STORAGE, LOCAL-STORAGE and LINKAGE? Allocated once; allocated on each entry; describes data owned by the caller. See recursion.
  5. What is RETURN-CODE? A special register that the program sets and JCL tests after the step.

Mainframe

  1. What is JCL and what do JOB, EXEC and DD do? Job Control Language: describe the job, run a program or procedure, connect datasets. See JCL.
  2. What causes S0C7, S0C4 and S806? Invalid numeric data, an invalid address (often a bad subscript or CALL), and a missing load module.
  3. Batch versus online? Jobs run unattended in bulk; CICS transactions answer user requests in a second. CICS.
  4. What is a cursor in embedded SQL, and what does SQLCODE +100 mean? A way to read a multi-row result row by row; no more rows (or no row found). Embedded SQL.
  5. What is pseudo-conversational programming? The task ends between screens and passes state in a COMMAREA, so no resources are held while the user thinks.

Judgment questions

  1. A report shows wrong totals on the last department only. Where do you look? At the control break: the final group needs a break after the read loop ends.
  2. A program works on a PC and fails on the mainframe with garbage in numbers. Why might that be? Character conversion of packed or binary fields during file transfer, ASCII versus EBCDIC comparison, or a record layout mismatch.
  3. How would you safely change a 30-year-old program? Capture the current output as a baseline, add tests, change one thing at a time, compare outputs, and keep the old version. See modernization.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is stored when 12345.678 is moved to a field PIC 9(3)V99?

    MOVE aligns on the decimal point and truncates: high-order digits 12 and the low-order 8 are lost, no rounding.
  2. 2. A SEARCH ALL fails to find a key that you know is in the table. What is the likeliest cause?

    Binary search depends on order; SEARCH ALL needs no SET, and equality is the only test it allows.
  3. 3. Which is the safest way to read the last record of a file in a read-ahead loop?

    The read-ahead pattern makes the loop test always reflect the latest READ.
  4. 4. What should a failed OPEN lead to in a production batch program?

    Report the status and set a return code so the scheduler can react.

Summary

  • Practice by level: tables and loops, files and status codes, sort and merge, subprograms, then complete batch programs.
  • Know how MOVE truncates and what edited pictures print; most numeric bugs start there.
  • Debug data first, flow second, environment last; print file statuses.
  • For interviews, be ready to explain PICTURE and USAGE, tables, file organizations and status codes, CALL, copybooks, JCL basics and the common abends.

This is the end of the COBOL tutorial. Keep building: write the projects on a real compiler, read other people's programs, and add Db2 or CICS practice if your goal is a mainframe role.

Next: What Is COBOL and Why Does It Still Matter?