Chapter 2 Exercises: COBOL Program Structure

These exercises are organized into five tiers of increasing cognitive complexity, from basic recall to creative application. Work through them in order; each tier builds on the knowledge tested in previous tiers.


Tier 1: Recall (Questions 1-7)

Test your memory of key facts, terminology, and rules.


Exercise 1. List the four COBOL divisions in the correct order. For each division, write one sentence describing its purpose.


Exercise 2. Fill in the following table from memory:

Area Name Columns Purpose
? 1-6 ?
? 7 ?
? 8-11 ?
? 12-72 ?
? 73-80 ?

Exercise 3. List all the characters that can appear in the Indicator Area (column 7) and state what each one means.


Exercise 4. Name the two sections of the ENVIRONMENT DIVISION and the paragraphs that belong to each section.


Exercise 5. Name at least five sections that can appear in the DATA DIVISION.


Exercise 6. What are the four levels in the PROCEDURE DIVISION hierarchy (from largest to smallest)?


Exercise 7. List the special level numbers in COBOL (those outside the 01-49 range) and state the purpose of each.


Tier 2: Understand (Questions 8-13)

Demonstrate that you understand the concepts, not just the vocabulary.


Exercise 8. Explain the difference between a SENTENCE and a STATEMENT in the PROCEDURE DIVISION. Provide an example that contains one sentence with three statements.


Exercise 9. Why is the ENVIRONMENT DIVISION considered the most "platform-dependent" part of a COBOL program? What would you typically need to change when moving a program from one system to another?


Exercise 10. Explain the difference between WORKING-STORAGE SECTION and LOCAL-STORAGE SECTION. In what scenario would the difference matter?


Exercise 11. Why do experienced COBOL programmers recommend using explicit scope terminators (like END-IF) instead of relying on the period? Provide an example of a bug that could result from relying on periods.


Exercise 12. Explain why COBOL has both Area A and Area B. What purpose does this physical layout distinction serve? Name three things that must start in Area A and three things that must start in Area B.


Exercise 13. Describe the purpose of the LINKAGE SECTION. How does data get "into" the LINKAGE SECTION? Why are VALUE clauses (except on 88-level items) not allowed in the LINKAGE SECTION?


Tier 3: Apply (Questions 14-21)

Write code, identify issues, and apply the rules you have learned.


Exercise 14. Write the minimum valid COBOL program. It should compile and execute, displaying "HELLO, WORLD" on the console. Use only the required divisions and the minimum number of paragraphs.


Exercise 15. Write a complete IDENTIFICATION DIVISION that includes all available paragraphs (PROGRAM-ID, AUTHOR, INSTALLATION, DATE-WRITTEN, DATE-COMPILED, SECURITY) for a hypothetical bank account reconciliation program.


Exercise 16. Write a FILE-CONTROL paragraph that defines the following three files: - A sequential input file called TRANS-FILE assigned to "TRANS.DAT" - An indexed master file called ACCOUNT-FILE assigned to "ACCTMAST.DAT" with primary key ACCT-NUMBER and an alternate key ACCT-NAME (with duplicates allowed), using dynamic access - A line sequential report file called EXCEPTION-RPT assigned to "EXCEPT.TXT"

All files must include FILE STATUS clauses.


Exercise 17. Write a WORKING-STORAGE SECTION that includes: - A file status variable - An end-of-file flag with condition names - A counter for records processed - A date field with REDEFINES to access year, month, and day separately - A table (array) that can hold 50 product codes (each 8 characters) - At least two constants with VALUE clauses


Exercise 18. The following program has seven structural errors. Find and correct all of them:

       IDENTIFICATION DIVISION
       PROGRAM-I.     BROKEN.
       ENVIROMENT DIVISION.
           CONFIGURATION SECTION.
       SOURCE-COMPUTER. IBM-PC.
       OBJECT-COMPUTER. IBM-PC.
       PROCEDURE DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
           01  WS-RESULT   PIC 9(5) VALUE ZEROS.
       PROCEDURE DIVISION.
               0000-MAIN.
       DISPLAY "RESULT: " WS-RESULT
           STOP RUN

Exercise 19. Write a PROCEDURE DIVISION with at least three sections, each containing at least two paragraphs. The program should: 1. Initialize a counter to zero 2. Use a PERFORM loop to count from 1 to 10 3. Display the final count 4. Include proper EXIT paragraphs for each section


Exercise 20. Convert the following fixed-format code to free format. List every change you make and explain why:

000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID.    CONVERT.
000300*THIS IS A COMMENT
000400 DATA DIVISION.
000500 WORKING-STORAGE SECTION.
000600 01  WS-MSG    PIC X(60) VALUE
000700-    "THIS IS A LONG STRING THAT MUST BE CONTINUED".
000800 PROCEDURE DIVISION.
000900 0000-MAIN.
001000D    DISPLAY "DEBUG: STARTING"
001100     DISPLAY WS-MSG
001200     STOP RUN.

Exercise 21. Write a SPECIAL-NAMES paragraph that: - Defines a class condition called VALID-POSTAL that accepts digits 0-9, uppercase letters A-Z, and the space character - Changes the decimal point to a comma (European format) - Maps the console to a mnemonic name OPERATOR-DISPLAY


Tier 4: Analyze (Questions 22-30)

Analyze code for correctness, style, and structure. Evaluate design decisions.


Exercise 22. The following two programs produce the same output. Analyze them and explain which has better structure and why. Consider readability, maintainability, and adherence to COBOL conventions.

Program A:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PROGA.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A PIC 9 VALUE 0.
       01  B PIC 9 VALUE 0.
       01  C PIC 99 VALUE 0.
       PROCEDURE DIVISION.
       X. MOVE 5 TO A. MOVE 3 TO B.
          ADD A B GIVING C. DISPLAY C.
          STOP RUN.

Program B:

       IDENTIFICATION DIVISION.
       PROGRAM-ID.    PROGB.
       AUTHOR.        J. SMITH.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-FIRST-NUMBER     PIC 9     VALUE ZEROS.
       01  WS-SECOND-NUMBER    PIC 9     VALUE ZEROS.
       01  WS-SUM              PIC 99    VALUE ZEROS.
       PROCEDURE DIVISION.
       0000-MAIN.
           PERFORM 1000-INITIALIZE
           PERFORM 2000-CALCULATE
           PERFORM 3000-DISPLAY-RESULT
           STOP RUN
           .
       1000-INITIALIZE.
           MOVE 5 TO WS-FIRST-NUMBER
           MOVE 3 TO WS-SECOND-NUMBER
           .
       2000-CALCULATE.
           ADD WS-FIRST-NUMBER WS-SECOND-NUMBER
               GIVING WS-SUM
           .
       3000-DISPLAY-RESULT.
           DISPLAY WS-SUM
           .

Exercise 23. A colleague proposes the following paragraph naming convention for a new project. Evaluate it. What are its strengths and weaknesses? Suggest improvements.

INIT-PROGRAM
READ-FILE
PROCESS-RECORD
WRITE-REPORT
CLOSE-FILES
ERROR-HANDLER

Exercise 24. You are reviewing a 2,000-line COBOL program and notice that the PROCEDURE DIVISION uses sections but no EXIT paragraphs. The programmer says EXIT paragraphs are unnecessary. Write a paragraph arguing for or against EXIT paragraphs, citing specific technical scenarios where their presence (or absence) matters.


Exercise 25. Examine the following IF statement and explain exactly what will happen when WS-VALUE is 0, when it is 5, and when it is 10. Pay careful attention to the periods.

           IF WS-VALUE > 0
               DISPLAY "POSITIVE".
           IF WS-VALUE > 5
               DISPLAY "GREATER THAN FIVE"
           END-IF.

Exercise 26. A program uses PERFORM 2000-PROCESS THRU 2999-PROCESS-EXIT. The 2000-PROCESS section contains paragraphs 2000-START, 2100-VALIDATE, 2200-CALCULATE, and 2999-PROCESS-EXIT. A new developer adds a paragraph called 2050-LOG-ENTRY between 2000-START and 2100-VALIDATE. What happens? Is this safe? What does this tell you about using PERFORM THRU?


Exercise 27. Compare the following two approaches to organizing a PROCEDURE DIVISION. For each approach, list two advantages and two disadvantages. Which would you choose for a 500-line program? A 5,000-line program?

Approach 1: Paragraphs only

       PROCEDURE DIVISION.
       0000-MAIN.             ...
       1000-INITIALIZE.       ...
       2000-PROCESS.          ...
       3000-FINALIZE.         ...
       8000-READ-FILE.        ...

Approach 2: Sections with paragraphs

       PROCEDURE DIVISION.
       0000-MAIN-CONTROL SECTION.
       0000-MAIN.             ...
       0000-EXIT.             EXIT.
       1000-INITIALIZATION SECTION.
       1000-INIT-START.       ...
       1100-OPEN-FILES.       ...
       1000-INIT-EXIT.        EXIT.

Exercise 28. The DATA DIVISION below has several style and structural issues. Identify at least five problems and rewrite the section following best practices.

       WORKING-STORAGE SECTION.
       77  X           PIC X.
       77  CTR         PIC 9999.
       01  DATE        PIC 9(8).
       01  REC.
           05  F1      PIC X(10).
           05  F2      PIC 9(5).
           05  F3      PIC X(20).
       01  FLAG        PIC X VALUE "N".
       01  TOTAL       PIC 9(7)V99.

Exercise 29. A program has the following ENVIRONMENT DIVISION. Identify what each clause and entry accomplishes. Then identify which parts are essential and which could be safely removed for a program that only reads one sequential file.

       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. IBM-MAINFRAME WITH DEBUGGING MODE.
       OBJECT-COMPUTER. IBM-MAINFRAME
           PROGRAM COLLATING SEQUENCE IS STANDARD-1.
       SPECIAL-NAMES.
           DECIMAL-POINT IS COMMA
           CLASS VALID-CODE IS "A" THRU "Z" "0" THRU "9"
           ALPHABET STANDARD-1 IS STANDARD-1.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT INPUT-FILE
               ASSIGN TO "INPUT.DAT"
               ORGANIZATION IS LINE SEQUENTIAL
               FILE STATUS IS WS-STATUS.
           SELECT SORT-FILE
               ASSIGN TO "SORT.TMP".
       I-O-CONTROL.
           SAME RECORD AREA FOR INPUT-FILE SORT-FILE.

Exercise 30. You have inherited a COBOL program where the entire PROCEDURE DIVISION is one single paragraph with 500 lines of code, containing deeply nested IF statements (some 8 levels deep) with periods scattered throughout. Describe a systematic approach to refactoring this into a well-structured program. What steps would you take? What risks exist? How would you validate that the refactored program produces the same results?


Tier 5: Create (Questions 31-38)

Design and write complete programs or significant program components.


Exercise 31. Write a complete COBOL program (all four divisions) that: - Reads a sequential file of student records (ID, name, three exam scores) - Calculates the average score for each student - Displays a pass/fail message (average >= 60 is pass) - At the end, displays the total number of students, number passing, and number failing

Use proper section/paragraph organization, naming conventions, and all the best practices discussed in this chapter.


Exercise 32. Write a complete COBOL program skeleton (all four divisions, all paragraphs defined but with minimal logic) for a library book checkout system. The program should handle: - A book master file (indexed by ISBN, alternate key by title) - A patron master file (indexed by patron ID) - A transaction file (sequential input: checkout and return transactions) - An overdue report (sequential output)

Define all FD entries with complete record layouts. Include at least 10 well-named paragraphs in the PROCEDURE DIVISION.


Exercise 33. Write both a fixed-format and free-format version of a program that: - Defines three constants in WORKING-STORAGE - Uses a SPECIAL-NAMES paragraph with at least two definitions - Contains a PROCEDURE DIVISION with at least two sections - Includes debugging lines that display variable values

Highlight every syntactic difference between the two versions with comments.


Exercise 34. Design a WORKING-STORAGE SECTION for an invoice processing program. Include: - Invoice header fields (invoice number, date, customer info) - Up to 50 line items using an OCCURS clause (each with item code, description, quantity, unit price, line total) - Tax calculations at multiple rates (state, county, city) - Grand total fields - Report formatting lines (header, detail, subtotal, total) - All appropriate flags, counters, and status fields

Use descriptive prefixed names and organize the fields into logical groups with comments.


Exercise 35. Write a main program and a subprogram. The main program should: - Define an employee record in WORKING-STORAGE - Call the subprogram, passing the employee record and a return code - Display the return code after the call

The subprogram should: - Receive the parameters via the LINKAGE SECTION - Validate the employee record (check that ID is numeric, name is not blank, salary is positive) - Set the return code to 0 for valid or 1 for invalid

Demonstrate proper use of the LINKAGE SECTION and PROCEDURE DIVISION USING clause.


Exercise 36. Write a COBOL program that demonstrates the use of all the following DATA DIVISION features in a single program: - FILE SECTION with FD - WORKING-STORAGE with 01, 05, 77, and 88 level items - REDEFINES - OCCURS clause - VALUE clauses with various data types - FILLER items - Condition names (88-level) with multiple values and ranges

The program should compile and run, displaying output that proves each feature works correctly.


Exercise 37. Create a coding standards document for a hypothetical COBOL development team. The document should cover: - Division and section organization rules - Paragraph naming conventions - Data naming conventions (prefixes, lengths, style) - Comment standards - Period usage rules - Indentation standards - Rules for when to use sections vs. paragraphs

Write at least 15 specific, enforceable rules with rationale for each.


Exercise 38. Write a COBOL program that serves as a "structure template" -- a boilerplate program that a developer could copy and fill in for any new batch processing program. The template should include: - All four divisions with standard structure - Placeholder sections for initialization, processing, output, and finalization - Error handling infrastructure - Standard file I/O patterns - Comprehensive comments explaining what to customize - Examples of both sequential and indexed file handling

This template should be practical enough that a COBOL developer could use it to start any new batch program within minutes.


Answer Key Note

Solutions for selected exercises (8, 9, 11, 14, 17, 21, 25, and 30) are provided in code/exercise-solutions.cob. For the remaining exercises, consult with your instructor or compare your answers with the chapter material. Many of these exercises, particularly in Tiers 4 and 5, have multiple valid solutions -- what matters is that your answer demonstrates understanding of the principles and follows the best practices discussed in the chapter.