Quiz — Chapter 24: Nested Programs

Multiple Choice

1. What is a nested (contained) program in COBOL?

A) A program that calls itself recursively B) A complete COBOL program defined within another program's source code C) A program that uses deeply nested IF statements D) A program stored in a sublibrary of the main library


2. Which statement is mandatory for every nested program?

A) STOP RUN B) EXIT PROGRAM C) END PROGRAM program-name D) GOBACK


3. By default (without COMMON), which programs can a directly contained program call?

A) Its parent program B) Its sibling programs C) Its own directly contained programs D) Any program in the compilation unit


4. What does the COMMON attribute on a PROGRAM-ID do?

A) Makes the program's data visible to all other programs B) Makes the program callable by its siblings (programs at the same nesting level) C) Makes the program reusable from external callers D) Shares the program's WORKING-STORAGE with its parent


5. What does the GLOBAL clause on a data item do?

A) Makes the item visible to all programs in the system B) Makes the item visible to the defining program and all programs contained within it C) Makes the item visible to the defining program's parent D) Makes the item available across separate compilation units


6. If a nested program defines a local variable with the same name as a GLOBAL variable in its parent, what happens?

A) A compile-time error occurs B) The GLOBAL variable takes precedence C) The local variable takes precedence (shadows the GLOBAL) D) Both are accessible with qualification


7. How many compilation units are produced when you compile a source file containing one outer program with three nested programs?

A) Four (one for each program) B) Three (one for each nested program) C) One (the entire file is a single compilation unit) D) Two (the outer program plus a combined nested unit)


8. Which is a disadvantage of nested programs compared to external subprograms?

A) They cannot pass parameters B) They cannot use WORKING-STORAGE C) They cannot be independently compiled or deployed D) They cannot use the CALL statement


9. In which direction does GLOBAL data flow in the nesting hierarchy?

A) Upward only (child to parent) B) Downward only (parent to children) C) Both upward and downward D) Sideways only (between siblings)


10. Which scenario is MOST appropriate for nested programs?

A) A date validation routine used by 50 programs across the system B) Internal formatting utilities specific to one program C) A shared error logging service D) A module that needs to be rewritten in Java next year


True or False

11. A nested program can call external subprograms (programs outside the nesting structure).

12. GLOBAL can be applied to file definitions (FD), not just data items.

13. The COMMON attribute makes a nested program's data visible to its siblings.

14. A program nested three levels deep can access a GLOBAL item defined at the outermost level.

15. Nested programs require separate link-edit steps for each contained program.


Short Answer

16. Explain the difference between the COMMON attribute and the GLOBAL clause. Give one use case for each.

17. A developer has a 2,000-line COBOL program that they want to organize using nested programs. The program contains five logical sections: input processing, validation, calculation, output formatting, and error handling. All five sections write to the same audit log file. How would you structure this as a nested program, and which items would you make GLOBAL?

18. Draw the calling scope diagram for this structure. Show which programs can call which other programs:

MAIN-PROG
├── PROCESS-A
│   └── HELPER-A1
├── PROCESS-B
├── UTILITY (COMMON)

19. What is GLOBAL shadowing? Give an example of when it might cause a bug, and how to prevent it.

20. A team is debating whether to implement a new calculation module as a nested program or an external subprogram. The module is currently used by only one caller but there are plans to use it from two additional programs next quarter. What would you recommend and why?


Answer Key

1. B — A complete COBOL program defined within another program's source code.

2. C — END PROGRAM program-name is mandatory for every nested program.

3. C — A contained program can call its own directly contained programs by default.

4. B — COMMON makes the program callable by its siblings.

5. B — GLOBAL makes the item visible to the defining program and all programs contained within it.

6. C — The local variable takes precedence (shadows the GLOBAL).

7. C — The entire file is a single compilation unit.

8. C — Nested programs cannot be independently compiled or deployed.

9. B — GLOBAL data flows downward only, from parent to children.

10. B — Internal formatting utilities specific to one program are the ideal use case for nested programs.

11. True — Nested programs can call external subprograms just like any other program.

12. True — GLOBAL can be applied to FD entries, file status items, and working-storage items.

13. False — COMMON affects call visibility (who can call the program), not data visibility. GLOBAL affects data visibility.

14. True — GLOBAL data is visible to all programs directly or indirectly contained in the program that defines it, regardless of nesting depth.

15. False — Nested programs compile as a single compilation unit and are link-edited together automatically.

16. COMMON controls program visibility — it determines which programs can call which other programs. A COMMON program can be called by its siblings. Use case: a shared formatting routine that multiple sibling processing programs need to call. GLOBAL controls data visibility — it determines which data items can be accessed by which programs. A GLOBAL item is visible to all contained programs. Use case: a shared output file or counter that multiple nested programs need to access without parameter passing.

17. Structure: MAIN-PROGRAM contains five nested programs (INPUT-PROC, VALIDATE, CALCULATE, FORMAT-OUTPUT, HANDLE-ERROR). Make the audit log file GLOBAL (FD), the file status field GLOBAL, and any shared counters (records processed, errors found) GLOBAL. All five nested programs should be COMMON so they can call HANDLE-ERROR when needed. The audit log file should be opened and closed by the outer program.

18.

MAIN-PROG can call: PROCESS-A, PROCESS-B, UTILITY
PROCESS-A can call: HELPER-A1, UTILITY (COMMON)
PROCESS-B can call: UTILITY (COMMON)
HELPER-A1 can call: (nothing shown, but could call external programs)
UTILITY can call: (nothing shown)
PROCESS-A CANNOT call: PROCESS-B (sibling, not COMMON)
PROCESS-B CANNOT call: PROCESS-A, HELPER-A1

19. GLOBAL shadowing occurs when a nested program defines a local variable with the same name as a GLOBAL variable in its parent. The local definition takes precedence, hiding the GLOBAL one. Bug example: a developer adds a local WS-STATUS to INNER-A, intending it as a temporary variable. They do not realize OUTER has a GLOBAL WS-STATUS that INNER-A was previously modifying. Now INNER-A modifies only its local copy, and OUTER's GLOBAL status is never updated — a subtle behavior change. Prevention: use distinctive naming for GLOBAL items (e.g., prefix with G-) and document all GLOBAL items prominently.

20. Recommend an external subprogram. Even though only one caller exists today, the plans for two additional callers next quarter make independent deployment and reusability important. Converting from nested to external later would require restructuring the source file and the build process. Starting external is easier than migrating later. If the module were unlikely to ever be shared, nested would be appropriate.