Chapter 10 Quiz: Tables and Arrays

Test your understanding of COBOL tables, the OCCURS clause, SEARCH, SEARCH ALL, indexes, and related topics. Each question has one correct answer unless otherwise stated. Try to answer each question before revealing the answer.


Question 1

At which level number can the OCCURS clause not be specified in the WORKING-STORAGE SECTION?

A) 02 B) 05 C) 01 D) 10

Show Answer **C) 01** The OCCURS clause cannot be specified at the 01 level in the WORKING-STORAGE SECTION. It can appear at levels 02 through 49. To create a table, define the 01-level group item and then apply OCCURS to a subordinate level.

Question 2

What is the first valid subscript value for accessing a COBOL table element?

A) 0 B) 1 C) It depends on the compiler D) The value specified in the FROM clause

Show Answer **B) 1** COBOL tables are 1-indexed. The first element is always element 1, not element 0. Accessing element 0 is invalid and may cause a runtime error or unpredictable behavior.

Question 3

Given the following definition, what is the total memory occupied by WS-EMPLOYEE-TABLE?

       01  WS-EMPLOYEE-TABLE.
           05  WS-EMP-ENTRY  OCCURS 50 TIMES.
               10  WS-EMP-ID    PIC 9(5).
               10  WS-EMP-NAME  PIC X(20).
               10  WS-EMP-SAL   PIC 9(7)V99.

A) 34 bytes B) 1,700 bytes C) 1,600 bytes D) 50 bytes

Show Answer **B) 1,700 bytes** Each entry is 5 + 20 + 9 = 34 bytes. With 50 occurrences: 34 * 50 = 1,700 bytes. Note that PIC 9(7)V99 occupies 9 bytes (7 digits + 2 decimal digits), not 10, because the V (implied decimal point) does not take any storage.

Question 4

What is the classic technique for initializing table data with VALUE clauses in pre-COBOL 2002 programs?

A) Using INITIALIZE with a VALUE phrase B) Using REDEFINES to overlay a data area with VALUE clauses as a table structure C) Using SET with VALUE D) Using MOVE CORRESPONDING

Show Answer **B) Using REDEFINES to overlay a data area with VALUE clauses as a table structure** The REDEFINES technique defines raw data with FILLER items containing VALUE clauses, then REDEFINES that same memory as a table structure. This is the standard pre-COBOL 2002 approach and is still the most common technique in production COBOL.

Question 5

Which statement is used to manipulate an index defined with INDEXED BY?

A) ADD B) MOVE C) SET D) COMPUTE

Show Answer **C) SET** Indexes cannot be used with arithmetic statements (ADD, SUBTRACT, MULTIPLY, DIVIDE, COMPUTE) or MOVE. They are manipulated exclusively through the SET statement (SET idx TO value, SET idx UP BY n, SET idx DOWN BY n), and through SEARCH and PERFORM VARYING which manage the index automatically.

Question 6

What value does an index internally store?

A) The occurrence number (1, 2, 3, ...) B) The byte displacement (offset) from the start of the table C) A pointer to the table entry D) The subscript value multiplied by the entry size

Show Answer **B) The byte displacement (offset) from the start of the table** Unlike subscripts, which store occurrence numbers and require runtime multiplication to compute offsets, indexes store the byte displacement directly. This makes table access faster because no multiplication is needed. When you SET an index UP BY 1, COBOL simply adds the entry size to the displacement.

Question 7

How many levels of OCCURS nesting does the COBOL standard permit?

A) 3 B) 5 C) 7 D) No limit

Show Answer **C) 7** The COBOL standard allows up to 7 levels of OCCURS nesting, creating up to 7-dimensional tables. In practice, tables beyond 3 dimensions are uncommon.

Question 8

In a two-dimensional table accessed as WS-CELL(WS-ROW, WS-COL), which subscript represents the outermost OCCURS?

A) WS-COL (the rightmost subscript) B) WS-ROW (the leftmost subscript) C) Either one -- the order does not matter D) It depends on the compiler

Show Answer **B) WS-ROW (the leftmost subscript)** In COBOL multi-dimensional table references, the leftmost subscript corresponds to the outermost OCCURS level, and the rightmost subscript corresponds to the innermost OCCURS level. The subscripts are listed outermost-to-innermost, left to right.

Question 9

Before using the SEARCH statement, what must you do?

A) Sort the table B) SET the index to 1 (or the desired starting position) C) OPEN the table D) INITIALIZE the index

Show Answer **B) SET the index to 1 (or the desired starting position)** The SEARCH statement begins scanning from the current index value. If you do not SET the index before SEARCH, it may be pointing past the end of the table from a previous operation, causing AT END to execute immediately. Always SET the index to 1 (or another valid starting position) before each SEARCH.

Question 10

How many WHEN clauses can a SEARCH statement have?

A) Exactly one B) One or more C) At most two D) Unlimited, but only the first match is used

Show Answer **B) One or more (D is also partially correct in that only the first matching WHEN executes)** The SEARCH statement allows multiple WHEN clauses. At each table position, all WHEN conditions are evaluated in order, and the first matching WHEN clause executes. Then the SEARCH terminates. This differs from SEARCH ALL, which allows only one WHEN clause.

Question 11

What happens when the AT END clause of a SEARCH statement is triggered?

A) The program terminates B) The index is reset to 1 C) The statements in the AT END clause execute D) An error condition is raised

Show Answer **C) The statements in the AT END clause execute** AT END is not an error condition. It simply means the SEARCH scanned all remaining entries without finding a match. The associated statements execute (typically displaying a "not found" message or setting a flag), and then control passes to the next statement after END-SEARCH. The index value is undefined after AT END executes.

Question 12

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

A) The table must have an ASCENDING KEY IS or DESCENDING KEY IS clause B) The table data must be sorted on the key field(s) C) Only equality conditions (=) are allowed in the WHEN clause D) The index must be SET to 1 before the SEARCH ALL

Show Answer **D) The index must be SET to 1 before the SEARCH ALL** Unlike SEARCH (sequential), SEARCH ALL (binary search) automatically manages the index. You do not need to SET it before executing SEARCH ALL. The other three statements (A, B, C) are all genuine requirements for SEARCH ALL.

Question 13

How many WHEN clauses does SEARCH ALL allow?

A) One or more B) Exactly one C) At most two D) Unlimited

Show Answer **B) Exactly one** SEARCH ALL permits only one WHEN clause. If you need to search for different values, you must use separate SEARCH ALL statements. This is because binary search requires a single, well-defined comparison at each step. For multiple conditions in one search, use SEARCH (sequential) instead.

Question 14

For a table with ASCENDING KEY defined on two fields, what must the WHEN clause of SEARCH ALL contain?

A) A test on either key field B) A test on the first key field only C) A test on both key fields connected by AND D) A test on both key fields connected by OR

Show Answer **C) A test on both key fields connected by AND** When a table has compound ascending or descending keys, the WHEN clause of SEARCH ALL must test ALL key fields, connected by AND. Omitting any key field will result in a compilation error.

Question 15

What is the maximum number of comparisons SEARCH ALL needs for a table of 1,000 entries?

A) 1,000 B) 500 C) 10 D) 100

Show Answer **C) 10** SEARCH ALL uses binary search, which requires at most log2(n) comparisons. log2(1000) is approximately 9.97, rounded up to 10. This is dramatically fewer than the 1,000 comparisons potentially needed by sequential SEARCH.

Question 16

In OCCURS DEPENDING ON, what is the "ODO subject"?

A) The table item with the OCCURS clause B) The data item that controls how many occurrences are active C) The maximum number of occurrences D) The index associated with the table

Show Answer **B) The data item that controls how many occurrences are active** In `OCCURS 1 TO 50 TIMES DEPENDING ON WS-COUNT`, WS-COUNT is the ODO subject. It determines the current effective size of the table. The table item with the OCCURS clause is called the ODO object.

Question 17

Which of the following is a valid restriction on OCCURS DEPENDING ON?

A) The ODO subject must be alphanumeric B) The ODO subject can be defined inside the ODO object C) Only the last item at a given level can have OCCURS DEPENDING ON D) OCCURS DEPENDING ON cannot be used with INDEXED BY

Show Answer **C) Only the last item at a given level can have OCCURS DEPENDING ON** Items defined after an ODO object in the same group have unpredictable positions because the preceding item's size is variable. Therefore, the ODO object must be the last item at its level. The ODO subject must be a numeric integer (not alphanumeric) and must NOT be inside the ODO object. OCCURS DEPENDING ON can be combined with INDEXED BY.

Question 18

What does FUNCTION LENGTH return for a group item containing an OCCURS DEPENDING ON?

A) The maximum possible length B) The minimum possible length C) The current length based on the ODO subject value D) The average length

Show Answer **C) The current length based on the ODO subject value** FUNCTION LENGTH returns the current logical length of the group item, which varies based on the current value of the ODO subject. If the ODO subject is 5, FUNCTION LENGTH returns the header size plus 5 times the entry size. If you change the ODO subject to 10, FUNCTION LENGTH will return a larger value.

Question 19

What is the result of executing ADD 1 TO WS-TBL-IDX where WS-TBL-IDX is defined via INDEXED BY?

A) The index is incremented by 1 occurrence B) A compilation error C) A runtime error D) The index is incremented by 1 byte

Show Answer **B) A compilation error** Indexes defined with INDEXED BY cannot be used in arithmetic statements (ADD, SUBTRACT, MULTIPLY, DIVIDE, COMPUTE). You must use `SET WS-TBL-IDX UP BY 1` instead. This is a common mistake for programmers new to COBOL indexes.

Question 20

What type of data item is defined with USAGE IS INDEX?

A) A table subscript B) A standalone index data item for saving/restoring index values C) A binary integer D) A floating-point number

Show Answer **B) A standalone index data item for saving/restoring index values** USAGE IS INDEX creates an index data item that is not tied to any specific table. It can hold index values copied from table indexes via SET. This is useful for saving an index position, performing other operations, and then restoring the saved position later.

Question 21

In the SEARCH statement, what does the VARYING clause do?

A) Changes the search order to descending B) Allows you to simultaneously increment another index or subscript C) Varies the search criteria D) Specifies a variable number of WHEN clauses

Show Answer **B) Allows you to simultaneously increment another index or subscript** The VARYING clause in SEARCH causes another index or integer data item to be incremented in step with the table's primary index. This is useful when you have parallel tables and need to find an entry in one table while tracking the corresponding position in another.

Question 22

What happens when SEARCH ALL is used on data that is not properly sorted?

A) COBOL automatically sorts the data first B) A runtime error is raised C) The results are unpredictable -- entries may not be found or wrong entries may be returned D) SEARCH ALL falls back to sequential search

Show Answer **C) The results are unpredictable -- entries may not be found or wrong entries may be returned** COBOL does NOT verify that data is sorted before performing SEARCH ALL. The binary search algorithm assumes sorted order. With unsorted data, the algorithm may jump to the wrong part of the table and miss valid entries, or it may return incorrect matches. This is one of the most dangerous mistakes in COBOL table programming because it produces silent, intermittent errors.

Question 23

Given PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10, how many times does the loop body execute?

A) 9 B) 10 C) 11 D) 0

Show Answer **B) 10** The loop executes with WS-I values 1, 2, 3, ..., 10. When WS-I becomes 11, the UNTIL condition (WS-I > 10) is true, and the loop terminates. So the body executes 10 times.

Question 24

Which search method should you prefer for a table of 5,000 entries that is already sorted?

A) SEARCH (sequential) for simplicity B) SEARCH ALL (binary) for performance C) PERFORM VARYING with IF statements D) Direct subscript access

Show Answer **B) SEARCH ALL (binary) for performance** For 5,000 sorted entries, SEARCH ALL requires at most about 13 comparisons (log2(5000) ~ 12.3), while SEARCH could require up to 5,000. If the table is already sorted and you have defined the appropriate KEY IS clause, SEARCH ALL is the clear choice for large tables.

Question 25

What compiler option in IBM Enterprise COBOL enables runtime subscript range checking?

A) BOUNDS B) SSRANGE C) CHECK D) SUBSCRIPT

Show Answer **B) SSRANGE** The SSRANGE compiler option in IBM Enterprise COBOL enables runtime checking of subscript and index values. When an out-of-range access is detected, the program receives a runtime error. This is invaluable during development and testing.

Question 26

Which of the following is a valid use of the SET statement with indexes?

A) SET WS-IDX TO WS-IDX + 1 B) SET WS-IDX UP BY 1 C) SET WS-IDX = 5 D) SET WS-IDX ADD 1

Show Answer **B) SET WS-IDX UP BY 1** The correct syntax for incrementing an index is `SET index-name UP BY integer`. `SET WS-IDX TO WS-IDX + 1` is invalid because indexes cannot appear in expressions. `SET WS-IDX = 5` uses incorrect syntax (should be `SET WS-IDX TO 5`). `SET WS-IDX ADD 1` is not valid COBOL syntax.

Question 27

In a multi-dimensional table stored in memory, which elements are contiguous?

A) Elements that share the same first (leftmost) subscript B) Elements that share the same last (rightmost) subscript C) Elements at the same position in different dimensions D) It varies by compiler

Show Answer **A) Elements that share the same first (leftmost) subscript** COBOL stores multi-dimensional tables in row-major order. All elements of the first "row" (same outermost subscript) are stored contiguously, followed by all elements of the second "row," and so on. This means varying the rightmost (innermost) subscript accesses contiguous memory, which is more cache-friendly.

Question 28

What is the COBOL equivalent of a "hash map" or "dictionary" from other languages?

A) A table with SEARCH ALL and ASCENDING KEY B) COBOL does not have a direct equivalent; lookup tables with SEARCH or SEARCH ALL are the closest C) A table with OCCURS DEPENDING ON D) A REDEFINES structure

Show Answer **B) COBOL does not have a direct equivalent; lookup tables with SEARCH or SEARCH ALL are the closest** COBOL does not have built-in hash maps, dictionaries, or associative arrays. The closest equivalents are sorted lookup tables with SEARCH ALL (O(log n) access) or direct-access tables where the key serves as the subscript (O(1) access, but only works for small, sequential integer keys). For very large datasets, database access (DB2, VSAM) is typically used instead.

Question 29

When should you choose a subscript over an index for table access?

A) When performance is critical B) When you need to display the occurrence number or use it in calculations C) When using SEARCH ALL D) When the table has more than 100 entries

Show Answer **B) When you need to display the occurrence number or use it in calculations** Subscripts are regular numeric data items that can be displayed, used in arithmetic, and passed to other routines. Indexes cannot be displayed or used in arithmetic. If you need the occurrence number for anything other than table access, use a subscript. For performance-critical access and SEARCH operations, use indexes.

Question 30

What is the output of the following code if WS-TABLE contains values 10, 20, 30, 40, 50?

       SET WS-IDX TO 3
       SEARCH WS-TABLE-ENTRY
           AT END
               DISPLAY "END"
           WHEN WS-TABLE-VALUE(WS-IDX) = 20
               DISPLAY "FOUND 20"
           WHEN WS-TABLE-VALUE(WS-IDX) = 30
               DISPLAY "FOUND 30"
       END-SEARCH

A) FOUND 20 B) FOUND 30 C) END D) FOUND 20 followed by FOUND 30

Show Answer **B) FOUND 30** The index is SET to 3 before the SEARCH, so scanning begins at position 3. Position 3 contains the value 30. The first WHEN (checking for 20) does not match, but the second WHEN (checking for 30) does match. SEARCH terminates with "FOUND 30". Note: SEARCH does not start from position 1 unless you explicitly SET the index to 1. This is why setting the index before SEARCH is so important.

Scoring Guide

Score Level
27-30 Expert -- You have thorough command of COBOL tables
23-26 Proficient -- Solid understanding with minor gaps
18-22 Competent -- Good foundation; review the topics you missed
13-17 Developing -- Re-read sections 10.6-10.8 and practice the exercises
Below 13 Beginning -- Work through the chapter again with the code examples