Chapter 3 Quiz: Data Types, Variables, and the PICTURE Clause
Test your understanding of COBOL data types and the PICTURE clause. Each question is followed by a hidden answer -- try to answer before revealing it.
Question 1
What is the difference between an elementary item and a group item in COBOL?
Show Answer
An **elementary item** is a data item that is not further subdivided and must have a PICTURE clause. It directly holds data. A **group item** contains subordinate data items and does NOT have a PICTURE clause. When referenced as a whole, a group item is treated as an alphanumeric field whose size is the sum of all its subordinate elementary items. Group items are defined by having subordinate items at higher level numbers beneath them.Question 2
What are the three fundamental data classes in COBOL, and which PICTURE symbols define them?
Show Answer
The three fundamental data classes are: 1. **Alphabetic** (PIC A) -- can contain letters (A-Z, a-z) and spaces only. 2. **Alphanumeric** (PIC X) -- can contain any character (letters, digits, special characters, spaces). 3. **Numeric** (PIC 9) -- can contain digits 0-9 only. This is the only class that supports arithmetic operations. Only numeric fields (PIC 9, with optional S, V, P) can be used in arithmetic statements.Question 3
What is the purpose of the V in PIC S9(5)V99? Does it occupy storage?
Show Answer
The `V` represents an **implied (assumed) decimal point**. It tells the compiler where the decimal point is positioned for the purpose of decimal alignment during arithmetic operations. The `V` does **NOT** occupy any storage. A field defined as `PIC S9(5)V99` uses 7 bytes in DISPLAY usage (5 integer digits + 2 decimal digits), not 8. The value 12345.67 is stored as `1234567` -- the compiler tracks the decimal position internally.Question 4
What happens when you move a negative value to a field defined as PIC 9(5) (without the S)?
Show Answer
The **sign is lost**. The field stores only the absolute value. For example, moving -500 to `PIC 9(5)` stores `00500`. This is a common COBOL bug. The `S` (sign) must be included in the PICTURE clause for any field that may hold negative values. The correct definition would be `PIC S9(5)`.Question 5
How many bytes of storage does PIC S9(7)V99 COMP-3 require? Show the calculation.
Show Answer
**5 bytes.** The calculation is: 1. Count total digit positions: 7 (integer) + 2 (decimal) = 9 digits 2. Add 1 for the sign nibble: 9 + 1 = 10 nibbles 3. Each byte holds 2 nibbles: 10 / 2 = **5 bytes** The formula is: `bytes = (total_digits + 1) / 2`, rounded up to the nearest whole number.Question 6
What is the difference between PIC $Z(5)9.99 and PIC $$$,$$9.99?
Show Answer
- `PIC $Z(5)9.99` uses a **fixed dollar sign**. The `$` always appears at the leftmost position of the field, regardless of the value. For the value 42.50, it displays as `$ 42.50`. - `PIC $$$,$$9.99` uses a **floating dollar sign**. The `$` moves (floats) to the position immediately before the first significant digit. For the value 42.50, it displays as ` $42.50`. The `$` symbols also suppress leading zeros like `Z` does.Question 7
What is the output when the value 0 (zero) is moved to each of these edited fields?
a) PIC Z(6)9.99
b) PIC Z(6).ZZ
c) PIC Z(6)9.99 BLANK WHEN ZERO
Show Answer
a) `PIC Z(6)9.99` displays: ` 0.00` -- The `9` forces at least one digit to appear, and the decimal point and trailing zeros show. b) `PIC Z(6).ZZ` displays: ` ` (all spaces) -- When ALL digit positions use `Z`, a zero value produces all spaces, including the decimal point position. c) `PIC Z(6)9.99 BLANK WHEN ZERO` displays: ` ` (all spaces) -- The `BLANK WHEN ZERO` clause forces the entire field to spaces when the value is zero, overriding the `9` that would normally force a digit to show.Question 8
What is a level 88 condition name? Give an example and explain why it is useful.
Show Answer
A **level 88 condition name** defines a named condition associated with specific values of a data item. It allows you to give meaningful names to data values, making code highly readable. Example: 01 WS-ACCOUNT-STATUS PIC X(1).
88 ACCT-ACTIVE VALUE "A".
88 ACCT-CLOSED VALUE "C".
88 ACCT-FROZEN VALUE "F".
Instead of writing `IF WS-ACCOUNT-STATUS = "A"`, you write `IF ACCT-ACTIVE`. This is useful because:
1. It makes code self-documenting -- you do not need to remember what "A" means.
2. If the code value changes, you only update the 88-level definition, not every IF statement.
3. It supports multiple values and ranges (THRU), making complex validation simple.
4. You can use `SET ACCT-ACTIVE TO TRUE` to set the value.
Question 9
What is the difference between COMP (BINARY), COMP-3 (PACKED-DECIMAL), and DISPLAY usage?
Show Answer
- **DISPLAY** (default): Each digit occupies one byte using the character encoding (ASCII or EBCDIC). Human-readable in memory dumps. A `PIC 9(5)` field uses 5 bytes. - **COMP/BINARY**: Value stored as a pure binary integer. Storage depends on digit count: 1-4 digits = 2 bytes (halfword), 5-9 digits = 4 bytes (fullword), 10-18 digits = 8 bytes (doubleword). Efficient for integer operations and subscripts. - **COMP-3/PACKED-DECIMAL**: Two digits stored per byte using BCD encoding, with the last nibble reserved for the sign. Storage = (digits + 1) / 2 bytes. Preferred for financial arithmetic because it provides exact decimal representation without floating-point rounding errors.Question 10
Why should you never use COMP-1 or COMP-2 for financial calculations?
Show Answer
COMP-1 (single-precision float) and COMP-2 (double-precision float) use **binary floating-point** representation, which cannot exactly represent many decimal fractions. For example, the value 0.1 (ten cents) cannot be stored exactly in binary floating-point -- it becomes a repeating binary fraction like 0.0001100110011... This leads to tiny rounding errors that accumulate over many operations, potentially causing balances to be off by pennies or more. COMP-3 (packed decimal) stores decimal values exactly -- 0.10 is stored as `01 0C` with no loss of precision. For financial calculations, always use COMP-3.Question 11
What is the purpose of the REDEFINES clause? Give an example.
Show Answer
The **REDEFINES clause** allows two or more data descriptions to share the same storage. It is COBOL's equivalent of a union -- the same bytes in memory are interpreted differently depending on which name you use. Example: 01 WS-DATE-NUMERIC PIC 9(8) VALUE 20260210.
01 WS-DATE-PARTS REDEFINES WS-DATE-NUMERIC.
05 WS-YEAR PIC 9(4).
05 WS-MONTH PIC 9(2).
05 WS-DAY PIC 9(2).
Both `WS-DATE-NUMERIC` and `WS-DATE-PARTS` occupy the same 8 bytes. You can reference the date as a single number (20260210) or as individual fields (year=2026, month=02, day=10). REDEFINES is commonly used for multi-format records where a type code determines which interpretation to use.
Question 12
How many bytes does each of the following fields occupy?
a) PIC S9(4) COMP
b) PIC S9(9) COMP
c) PIC S9(5) COMP-3
d) PIC X(15)
e) PIC S9(13)V99 COMP-3
Show Answer
a) `PIC S9(4) COMP` = **2 bytes** (halfword: 1-4 digits) b) `PIC S9(9) COMP` = **4 bytes** (fullword: 5-9 digits) c) `PIC S9(5) COMP-3` = **3 bytes** ((5+1)/2 = 3 nibble-pairs) d) `PIC X(15)` = **15 bytes** (1 byte per character, always DISPLAY) e) `PIC S9(13)V99 COMP-3` = **8 bytes** (15 digits total; (15+1)/2 = 8)Question 13
What is the difference between level 66 (RENAMES) and REDEFINES?
Show Answer
Both provide alternative views of the same storage, but they differ in key ways: - **REDEFINES** redefines an entire data item at the same level. It creates a completely new description of the same storage. The redefining item must immediately follow the redefined item. Example: redefining a date numeric as a group with year/month/day. - **Level 66 (RENAMES)** creates an alternative grouping of a *range* of contiguous elementary items within an existing record. It uses `THRU` to span multiple fields. Level 66 must appear after the last data item in the record. Example: renaming fields 3 through 5 of a record as a single group. RENAMES is more restrictive (must be contiguous, within same 01-level) but is useful for creating cross-field groupings. REDEFINES is more general purpose.Question 14
What is the JUSTIFIED RIGHT clause, and when would you use it?
Show Answer
The `JUSTIFIED RIGHT` (or `JUST RIGHT`) clause causes alphanumeric or alphabetic data to be **right-justified** with leading space padding when moved to the field, instead of the default left-justification with trailing space padding. Example: 01 WS-NORMAL PIC X(10).
01 WS-RIGHT PIC X(10) JUSTIFIED RIGHT.
MOVE "ABC" TO WS-NORMAL. *> "ABC "
MOVE "ABC" TO WS-RIGHT. *> " ABC"
It is useful for formatting output where you want data aligned to the right side of a column, such as numeric codes displayed in a fixed-width field. It does NOT apply to numeric fields, which are always right-justified by nature.
Question 15
What are figurative constants in COBOL? Name all six and explain when you would use each.
Show Answer
Figurative constants are reserved words representing commonly used values that automatically expand to fill the receiving field: 1. **ZERO / ZEROS / ZEROES** -- Represents numeric zero for numeric fields, character "0" for alphanumeric fields. Used to initialize counters and clear numeric fields. 2. **SPACE / SPACES** -- Represents the space character. Used to clear alphanumeric fields and test for blank fields. 3. **HIGH-VALUE / HIGH-VALUES** -- Represents the highest character in the collating sequence (X"FF"). Used as a sentinel value in sorting (sorts after everything) and sequential processing. 4. **LOW-VALUE / LOW-VALUES** -- Represents the lowest character (X"00", null). Used as end-of-data markers and for initializing buffers. 5. **QUOTE / QUOTES** -- Represents the quotation mark character. Used when you need to embed a quote character that would otherwise terminate a literal. 6. **ALL literal** -- Repeats a literal pattern to fill the field. Used for decorative lines (`ALL "*"`), test patterns, and custom fill characters. The singular and plural forms of each constant are interchangeable.Question 16
Identify the error in each of the following data definitions:
a) 01 WS-AMT PIC 9(5)S.
b) 01 WS-RATE PIC S9V99 COMP-1.
c) 01 WS-GROUP PIC X(20). 05 WS-PART PIC X(10).
d) 01 WS-EDIT PIC Z(6)9.99. ADD 1 TO WS-EDIT.
Show Answer
a) **Error:** The `S` must be the leftmost character in the PICTURE string. **Fix:** `PIC S9(5)`. b) **Error:** COMP-1 (floating point) does not allow a PICTURE clause. **Fix:** Either use `COMP-1` alone (no PIC) or use `PIC S9V99 COMP-3` for exact decimal. c) **Error:** A group item must NOT have a PICTURE clause. If `WS-GROUP` has a PIC clause, it is an elementary item and cannot have subordinate level 05 items. **Fix:** Remove `PIC X(20)` from the group item. d) **Error:** You cannot perform arithmetic on a numeric edited field. `PIC Z(6)9.99` is an edited picture (contains Z and period). **Fix:** Keep a separate numeric field `PIC S9(7)V99` for arithmetic and MOVE to the edited field for display.Question 17
Given the following record, what is stored in WS-DATE-PORTION after the MOVEs?
01 WS-DATETIME-REC.
05 WS-YEAR PIC 9(4) VALUE 2026.
05 WS-MONTH PIC 9(2) VALUE 02.
05 WS-DAY PIC 9(2) VALUE 10.
05 WS-HOUR PIC 9(2) VALUE 14.
05 WS-MINUTE PIC 9(2) VALUE 30.
66 WS-DATE-PORTION RENAMES WS-YEAR THRU WS-DAY.
Show Answer
`WS-DATE-PORTION` contains `20260210`. The level 66 RENAMES creates an alternative view spanning from `WS-YEAR` through `WS-DAY`, covering the first 8 bytes of the record. The values are: YEAR=2026, MONTH=02, DAY=10, concatenated as the 8-character alphanumeric value `20260210`. The RENAMES does not create new storage -- it references the same bytes as the original fields.Question 18
What is the difference between the VALUE clause and the INITIALIZE statement?
Show Answer
- **VALUE clause**: Sets the initial value of a data item at **compile time** (or more precisely, when the program is loaded into memory). It is a one-time initialization. The VALUE clause is part of the data definition in the DATA DIVISION. 01 WS-COUNTER PIC 9(5) VALUE ZERO. *> Set when program loads
- **INITIALIZE statement**: Resets values at **run time**. It is a PROCEDURE DIVISION statement that can be executed at any point during program execution, multiple times. By default, it sets alphanumeric fields to SPACES and numeric fields to ZEROS.
INITIALIZE WS-CUSTOMER-RECORD. *> Can be executed anytime
Key difference: VALUE is static (one-time), INITIALIZE is dynamic (can be repeated). Use VALUE for initial state and INITIALIZE to reset a record structure between processing iterations.
Question 19
A field is defined as PIC S9(5)V99 COMP-3. The value stored is -12345.67. Write the hexadecimal storage representation byte by byte.
Show Answer
The hex representation is: `01 23 45 67 8D` Wait -- let me recalculate. The value is -12345.67. Total digits: 5 (integer) + 2 (decimal) = 7 digits. Nibbles needed: 7 digits + 1 sign = 8 nibbles = 4 bytes. But `PIC S9(5)V99` has 7 digit positions. (7+1)/2 = 4 bytes. Storage layout (4 bytes):Byte 0: 1|2 (digits 1, 2)
Byte 1: 3|4 (digits 3, 4)
Byte 2: 5|6 (digits 5, 6)
Byte 3: 7|D (digit 7, sign D=negative)
Hex: `12 34 56 7D`
The value 12345.67 is stored as digits 1234567 (V does not occupy space), with D for negative sign.
Question 20
When would you use PIC X instead of PIC 9 for a field that contains only digits (such as a ZIP code or Social Security Number)?
Show Answer
Use `PIC X` for numeric-looking data when: 1. **No arithmetic will be performed on the field.** You will never add, subtract, multiply, or divide ZIP codes or SSNs. 2. **The field may contain leading zeros that must be preserved.** ZIP code 01234 must display as "01234", not as the number 1234. 3. **The field may contain non-digit characters.** Some ZIP codes use the format "01234-5678" (with a dash). Canadian postal codes mix letters and digits ("K1A 0B1"). 4. **The field is used for comparison and display only.** PIC X is sufficient and avoids any possibility of inadvertent arithmetic. 5. **The value is an identifier, not a quantity.** SSN, phone number, account number, and ZIP code are identifiers -- their numeric appearance is coincidental. You would never add two SSNs together. **Rule of thumb:** If you would never do arithmetic with it, use PIC X.Question 21
What is COMP-5, and how does it differ from COMP/BINARY?
Show Answer
**COMP-5** (native binary) is similar to COMP/BINARY but with one key difference: the stored value can **exceed the range implied by the PIC clause**. With COMP/BINARY: - `PIC S9(4) COMP` stores in 2 bytes, maximum value is 9,999 (limited by PIC). With COMP-5: - `PIC S9(4) COMP-5` stores in 2 bytes, maximum value is 32,767 (full signed halfword range). The PIC clause determines the **storage size** (2, 4, or 8 bytes) for both COMP and COMP-5, but COMP-5 does not restrict the value to the PIC range. COMP-5 uses the platform's native binary representation and byte order, making it suitable for interfacing with C programs, system calls, and other non-COBOL software.Question 22
Explain the SIGN IS TRAILING SEPARATE CHARACTER clause. When would you use it?
Show Answer
`SIGN IS TRAILING SEPARATE CHARACTER` stores the sign as a distinct `+` or `-` character in a separate byte at the end of the field, rather than embedding it in the zone bits of a digit. For `PIC S9(5) SIGN IS TRAILING SEPARATE CHARACTER`: - Storage: **6 bytes** (5 digits + 1 sign character) - Value +12345 stored as: `12345+` - Value -12345 stored as: `12345-` You would use it when: 1. The data file format requires an explicit sign character (common in data exchange formats). 2. You need the sign to be human-readable in file listings and dumps. 3. You are interfacing with external systems that expect a visible sign character. 4. The data will be transferred between EBCDIC and ASCII platforms (separate sign avoids zone-bit encoding differences).Question 23
What is the maximum number of digit positions allowed in a COBOL PICTURE clause?
Show Answer
The COBOL standard allows a maximum of **18 digit positions** in a PICTURE clause. This applies to the total number of `9` symbols (plus any implied by `P`). This means: - `PIC 9(18)` -- valid (18 digits) - `PIC S9(16)V99` -- valid (16 + 2 = 18 digits) - `PIC S9(17)V99` -- invalid (17 + 2 = 19 digits, exceeds 18) - `PIC 9(19)` -- invalid (exceeds 18) For COMP/BINARY, 18 digits fit in an 8-byte doubleword. For COMP-3, 18 digits require 10 bytes ((18+1)/2 = 9.5, rounded up to 10). Some compiler extensions support more than 18 digits (e.g., 31 or 38), but 18 is the standard maximum.Question 24
What does the P symbol do in PIC 9(3)P(4)? What value does PIC 9(3)P(4) represent when the stored digits are 123?
Show Answer
The `P` symbol represents a **decimal scaling position** -- a digit position that is NOT stored but is assumed to be zero. In `PIC 9(3)P(4)`: - 3 digit positions are stored (the `9(3)` part) - 4 additional digit positions are assumed to be zero (the `P(4)` part) - The assumed zeros are to the RIGHT of the stored digits When the stored digits are 123, the represented value is **1,230,000** (123 followed by four assumed zeros). Storage: only 3 bytes (the three stored digits). The four P positions do not occupy storage. `P` can also appear to the LEFT of the digits (before the decimal point) to represent very small numbers. For example, `PIC P(3)9(2)` with stored digits 45 represents 0.00045.Question 25
Write a level 88 condition name structure for a WS-MONTH field (PIC 9(2)) that supports conditions for each quarter and each season.
Show Answer
01 WS-MONTH PIC 9(2).
88 JANUARY VALUE 01.
88 FEBRUARY VALUE 02.
88 MARCH VALUE 03.
88 APRIL VALUE 04.
88 MAY VALUE 05.
88 JUNE VALUE 06.
88 JULY VALUE 07.
88 AUGUST VALUE 08.
88 SEPTEMBER VALUE 09.
88 OCTOBER VALUE 10.
88 NOVEMBER VALUE 11.
88 DECEMBER VALUE 12.
88 VALID-MONTH VALUE 01 THRU 12.
88 QUARTER-1 VALUE 01 THRU 03.
88 QUARTER-2 VALUE 04 THRU 06.
88 QUARTER-3 VALUE 07 THRU 09.
88 QUARTER-4 VALUE 10 THRU 12.
88 WINTER VALUE 12 01 02.
88 SPRING VALUE 03 04 05.
88 SUMMER VALUE 06 07 08.
88 AUTUMN VALUE 09 10 11.
Note: WINTER uses individual values (12 01 02) rather than THRU because it wraps around the year boundary. THRU requires the lower value first, so `12 THRU 02` would not work correctly.
Question 26 (Bonus)
A COBOL program needs to store and process the US national debt, currently approximately $33 trillion ($33,000,000,000,000.00). Design the appropriate data fields for storage, arithmetic, and display. Justify your design choices.
Show Answer
*--- Storage and arithmetic field ---
01 WS-NATIONAL-DEBT PIC S9(15)V99 COMP-3.
* Range: up to +/- $999,999,999,999,999.99
* (999 trillion, providing growth room)
* Storage: (17+1)/2 = 9 bytes in COMP-3
* Signed to handle adjustments and calculations
* COMP-3 for exact decimal arithmetic
* 17 total digits (within the 18-digit maximum)
*--- Display/report field ---
01 WS-DEBT-DISPLAY
PIC $$$,$$$, MATH3 $,$$$,$$9.99.
* Shows formatted output with commas and dollar sign
* Example: "$33,000,000,000,000.00"
*--- Alternative: with sign for changes ---
01 WS-DEBT-CHANGE-DISPLAY
PIC -$$$,$$$, MATH6 $,$$9.99.
* Shows negative sign for debt reductions
Design justifications:
- **PIC S9(15)V99**: 15 integer digits support up to 999 trillion, providing ample room for growth. 2 decimal places for cents. S for sign because debt-related calculations may involve negative intermediate values.
- **COMP-3**: Essential for exact financial arithmetic. No floating-point rounding errors. Compact storage (9 bytes vs. 17 bytes for DISPLAY).
- **17 total digits**: Within COBOL's 18-digit maximum, leaving room for the sign nibble.
- The edited field uses floating `$` for clean formatting.
Scoring Guide
| Score | Rating |
|---|---|
| 24-26 | Expert -- ready for production COBOL data design |
| 20-23 | Proficient -- solid understanding of fundamentals |
| 15-19 | Developing -- review sections on USAGE and editing |
| 10-14 | Beginner -- re-read the chapter and redo Tier 1-2 exercises |
| 0-9 | Start over -- focus on sections 3.2 through 3.6 first |