Quiz: Table Handling and Searching

Multiple Choice

1. Which of the following levels can the OCCURS clause appear on?

a) 01 level only b) 77 level only c) 02 through 49 levels d) Any level from 01 through 49

2. What is the primary performance advantage of indexes over subscripts?

a) Indexes use less memory b) Indexes store byte displacements, avoiding runtime multiplication c) Indexes can be used with DISPLAY statements d) Indexes allow alphanumeric subscript values

3. Before executing a SEARCH statement, you must:

a) Sort the table in ascending order b) SET the index to 1 (or the desired starting position) c) Move zero to all table entries d) Open the file containing the table data

4. Which of the following is NOT a requirement for SEARCH ALL?

a) The table must have an ASCENDING KEY or DESCENDING KEY clause b) The data must actually be sorted by the key c) The WHEN clause must test for equality d) The index must be SET to 1 before the search

5. What does OCCURS DEPENDING ON accomplish?

a) Creates a table whose maximum size is determined at runtime b) Creates a variable-length table where the logical size varies c) Dynamically allocates memory for table entries d) Allows the table to grow beyond its defined maximum

6. In a multi-dimensional table defined as OCCURS 5 TIMES containing OCCURS 3 TIMES, how many total innermost elements exist?

a) 3 b) 5 c) 8 d) 15

7. What happens if you use SEARCH ALL on an unsorted table?

a) The program will abend with a SOC7 b) The compiler will reject the program c) The search may produce incorrect results silently d) The search automatically sorts the table first

8. Which statement is used to convert an index value to an ordinary numeric data item?

a) MOVE idx TO ws-num b) COMPUTE ws-num = idx c) SET ws-num TO idx d) ADD idx TO ws-num

9. How many levels of nested OCCURS does the COBOL standard allow?

a) 3 b) 5 c) 7 d) Unlimited

10. In a SEARCH ALL statement, how many WHEN clauses are permitted?

a) One b) Two c) As many as needed d) At least one, with optional ALSO clauses

True or False

11. You can use the VALUE clause on an item that has an OCCURS clause in standard COBOL.

12. The SEARCH statement automatically resets the index to 1 before beginning the search.

13. SEARCH ALL can use OR conditions in the WHEN clause.

14. The DEPENDING ON data item in an OCCURS DEPENDING ON clause must be defined before the OCCURS item in the record.

15. An index name defined with INDEXED BY can be used directly in a DISPLAY statement.

Short Answer

16. Explain the difference between O(n) and O(log n) search performance. For a table of 10,000 entries, approximately how many comparisons would each require in the worst case?

17. A colleague has written a program that loads 500 entries into a table and uses SEARCH ALL, but intermittently fails to find entries that exist. What is the most likely cause, and how would you diagnose and fix it?

18. Describe three defensive programming practices that should always be applied when working with COBOL tables.

19. When would you choose serial search (SEARCH) over binary search (SEARCH ALL) even if the table is sorted?

20. Calculate the total memory consumed by the following table definition:

01  PRICE-TABLE.
    05  PRICE-REGION  OCCURS 6 TIMES.
        10  REGION-CODE  PIC X(2).
        10  PRICE-TIER   OCCURS 4 TIMES.
            15  TIER-CODE    PIC X(1).
            15  TIER-PRICE   PIC 9(5)V99.

Answer Key

  1. c — OCCURS can appear on levels 02-49, never on 01, 66, 77, or 88.
  2. b — Indexes contain byte displacements, so the compiler does not need to multiply (subscript - 1) * element-size at runtime.
  3. b — The index must be positioned before SEARCH begins, as the search starts at the current index position.
  4. d — SEARCH ALL manages the index internally; you do NOT need to SET it to 1.
  5. b — OCCURS DEPENDING ON creates a table with a fixed maximum allocation but a variable logical size determined by the DEPENDING ON value.
  6. d — 5 x 3 = 15 total innermost elements.
  7. c — The binary search algorithm relies on sorted data; unsorted data causes silent incorrect results.
  8. c — SET is the only proper way to convert between indexes and data items.
  9. c — The COBOL standard allows up to 7 levels of nested OCCURS.
  10. a — SEARCH ALL permits only one WHEN clause.
  11. False — Standard COBOL prohibits VALUE on OCCURS items, though many compilers allow it as an extension.
  12. False — SEARCH begins at the current index position; you must SET it to 1 yourself.
  13. False — SEARCH ALL WHEN clauses can only use AND to combine conditions, not OR.
  14. True — The DEPENDING ON object must precede the OCCURS item in the record definition.
  15. False — Indexes contain byte displacements and cannot be used directly in DISPLAY; use SET to convert to a numeric item first.
  16. O(n) serial search requires up to 10,000 comparisons (average 5,000). O(log n) binary search requires at most ceil(log2(10,000)) = 14 comparisons. Binary search is approximately 357 times more efficient on average for this size.
  17. The most likely cause is that the table is not properly sorted by the ASCENDING KEY field. SEARCH ALL uses binary search, which produces incorrect results on unsorted data. Diagnosis: add a sort-verification loop after loading. Fix: ensure the input file is sorted, or sort the table after loading.
  18. (1) Always validate subscript/index values before accessing table entries. (2) Always check for table overflow when loading entries (compare count against maximum). (3) Always verify sort order before using SEARCH ALL. Other valid answers: verify the table is loaded before first lookup, handle AT END conditions, enable SSRANGE during development.
  19. Choose SEARCH over SEARCH ALL when: the table is very small (under 20 entries, where the overhead of binary search setup exceeds savings), when you need complex conditions (OR, ranges, partial matches) that SEARCH ALL cannot express, or when you need to find the FIRST match in physical order (SEARCH ALL may find any matching entry in a table with duplicates).
  20. Innermost: TIER-CODE (1) + TIER-PRICE (7) = 8 bytes per tier. PRICE-TIER level: 4 x 8 = 32 bytes. REGION-CODE (2) + 32 = 34 bytes per region. Total: 6 x 34 = 204 bytes.