Chapter 5 Quiz: Basic Input/Output and DISPLAY/ACCEPT Statements
Test your understanding of COBOL DISPLAY and ACCEPT statements. Try to answer each question before revealing the answer.
Question 1
What is the default behavior of a DISPLAY statement regarding line termination?
Show Answer
Each DISPLAY statement automatically appends a carriage return and line feed after the output, causing the cursor to move to the beginning of the next line. To suppress this behavior, use the `WITH NO ADVANCING` clause.Question 2
Given the following data definitions, what is the exact output of the DISPLAY statement?
01 WS-NAME PIC X(10) VALUE "COBOL".
01 WS-NUM PIC 9(5) VALUE 42.
DISPLAY "[" WS-NAME "][" WS-NUM "]"
Show Answer
[COBOL ][00042]
PIC X(10) fields are left-justified and padded with 5 trailing spaces. PIC 9(5) fields display with 3 leading zeros. The items are concatenated directly with no automatic spacing.
Question 3
What is the purpose of the WITH NO ADVANCING clause, and when would you typically use it?
Show Answer
`WITH NO ADVANCING` suppresses the carriage return/line feed that DISPLAY normally appends. The cursor remains on the same line after the output. It is typically used to create prompts where the user's input should appear on the same line as the prompt text: DISPLAY "Enter your name: " WITH NO ADVANCING
ACCEPT WS-NAME
Question 4
What is the difference between DISPLAY UPON CONSOLE and DISPLAY UPON SYSOUT on an IBM mainframe?
Show Answer
- `DISPLAY UPON CONSOLE` sends a Write-To-Operator (WTO) message to the z/OS operator console. It is used for operator communication in production batch jobs. - `DISPLAY UPON SYSOUT` sends output to the SYSOUT DD statement defined in the JCL. This output goes to a spool dataset that the programmer can review after the job completes. - In GnuCOBOL, both forms send output to stdout (the terminal) and are functionally equivalent.Question 5
What does ACCEPT WS-DATE FROM DATE YYYYMMDD return, and why is it preferred over ACCEPT WS-DATE FROM DATE?
Show Answer
`ACCEPT FROM DATE YYYYMMDD` returns the current system date as an 8-digit number in YYYYMMDD format (for example, 20260210 for February 10, 2026). It requires a PIC 9(8) field. It is preferred over `ACCEPT FROM DATE` (which returns YYMMDD, a 6-digit format with a 2-digit year) because the 4-digit year eliminates Y2K-type ambiguity. With a 2-digit year, there is no way to distinguish between 1926 and 2026.Question 6
What format does ACCEPT FROM TIME return, and what does each component represent?
Show Answer
`ACCEPT FROM TIME` returns an 8-digit number in the format HHMMSSss: - HH: Hours in 24-hour format (00-23) - MM: Minutes (00-59) - SS: Seconds (00-59) - ss: Hundredths of a second (00-99) For example, 14305075 represents 2:30:50.75 PM. The receiving field should be PIC 9(8).Question 7
In the ACCEPT FROM DAY-OF-WEEK statement, what value represents Monday?
Show Answer
Monday is represented by the value 1. The numbering follows the ISO 8601 convention: 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday. The receiving field should be PIC 9 (a single digit).Question 8
What is a Julian date, and how do you retrieve it in COBOL?
Show Answer
A Julian date represents a date as the year plus the sequential day number within that year (1-365 or 366 for leap years). For example, February 10, 2026 would be represented as 2026041 (the 41st day of 2026). Retrieve it with: ACCEPT WS-JULIAN FROM DAY YYYYDDD
The 2-digit year version (`ACCEPT FROM DAY`, format YYDDD) is also available but should be avoided for the same Y2K reasons as `ACCEPT FROM DATE`.
Question 9
What happens when a user types "CHRISTOPHER" and it is accepted into a PIC X(8) field?
Show Answer
The value is truncated to fit the field. The field will contain "CHRISTOP" (the first 8 characters). Alphanumeric fields truncate on the right. COBOL does not generate an error or warning for this truncation; it is standard behavior.Question 10
What is the danger of accepting user input directly into a PIC 9(5) field?
Show Answer
If the user types non-numeric characters (letters, special characters) into a PIC 9 field, the data is invalid. Subsequent arithmetic operations on the field may cause a data exception abend (SOC7 on IBM mainframes). The best practice is to accept input into a PIC X field, validate that it is numeric (using `IF WS-INPUT IS NUMERIC`), and then move it to the numeric field.Question 11
What is the difference between DELIMITED BY SIZE and DELIMITED BY SPACES in a STRING statement?
Show Answer
- `DELIMITED BY SIZE` copies the entire source field, including all trailing spaces/padding, up to its full defined length. - `DELIMITED BY SPACES` copies characters from the source field only until the first space is encountered, effectively trimming the field. For example, if WS-NAME is PIC X(15) containing "GRACE": - `DELIMITED BY SIZE` copies "GRACE " (all 15 characters) - `DELIMITED BY SPACES` copies "GRACE" (5 characters, stopping at the first space)Question 12
What is the SCREEN SECTION, and which COBOL compilers support it?
Show Answer
The SCREEN SECTION is a special section in the DATA DIVISION (defined after WORKING-STORAGE) that allows programmers to define formatted screen layouts with cursor positioning (LINE, COLUMN), colors (FOREGROUND-COLOR, BACKGROUND-COLOR), and visual attributes (HIGHLIGHT, UNDERLINE, BLINK, REVERSE-VIDEO). It was standardized in COBOL 2002. GnuCOBOL fully supports it. IBM Enterprise COBOL does **not** support it; IBM uses BMS (Basic Mapping Support) maps in CICS environments for formatted screen I/O.Question 13
In the SCREEN SECTION, what is the difference between USING, FROM, and TO in a field definition?
Show Answer
- `USING identifier` -- The field is both displayed (showing the current value of the identifier) and editable (the user can modify it, and the new value is stored back in the identifier). This is the most common for data entry forms. - `FROM identifier` -- The field displays the value of the identifier but is not editable. It is output-only. - `TO identifier` -- The field is input-only; it does not display a pre-existing value, but whatever the user types is stored in the identifier.Question 14
What are the standard color codes in the SCREEN SECTION?
Show Answer
| Code | Color | |------|-------| | 0 | Black | | 1 | Blue | | 2 | Green | | 3 | Cyan | | 4 | Red | | 5 | Magenta | | 6 | Yellow/Brown | | 7 | White | These codes are used with FOREGROUND-COLOR and BACKGROUND-COLOR clauses.Question 15
What is the difference between STOP RUN and GOBACK?
Show Answer
- `STOP RUN` terminates the entire run unit. If the program was called by another program, STOP RUN terminates all programs in the call chain. - `GOBACK` returns control to the calling program (if one exists) or to the operating system (if this is the main program). GOBACK is preferred for subprograms because it allows the calling program to continue executing. STOP RUN is appropriate for main programs or for fatal error situations where the entire run unit should terminate.Question 16
What does DISPLAY ALL "*" produce?
Show Answer
It fills the output line with asterisks (*). The exact width depends on the implementation. In GnuCOBOL, this typically produces a line of 80 asterisks. The `ALL` figurative constant repeats the specified character (or string) to fill the available width.Question 17
In IBM batch COBOL, where does the output of a plain DISPLAY statement (without UPON) go?
Show Answer
In IBM Enterprise COBOL, a plain DISPLAY statement (without an UPON clause) sends output to the SYSOUT DD statement defined in the JCL. This output goes to a spool dataset. This is different from GnuCOBOL, where the default destination is stdout (the terminal).Question 18
Write the COBOL statements to format the system date as "February 10, 2026" using STRING and a month name table.
Show Answer
01 WS-DATE PIC 9(8).
01 WS-DATE-R REDEFINES WS-DATE.
05 WS-YYYY PIC 9(4).
05 WS-MM PIC 9(2).
05 WS-DD PIC 9(2).
01 WS-MONTH-TABLE.
05 FILLER PIC X(9) VALUE "January".
05 FILLER PIC X(9) VALUE "February".
05 FILLER PIC X(9) VALUE "March".
*> ... (remaining months) ...
05 FILLER PIC X(9) VALUE "December".
01 WS-MONTH-ARRAY REDEFINES WS-MONTH-TABLE.
05 WS-MONTH-NAME PIC X(9) OCCURS 12 TIMES.
01 WS-FORMATTED PIC X(30).
01 WS-DAY-EDITED PIC Z9.
ACCEPT WS-DATE FROM DATE YYYYMMDD
MOVE WS-DD TO WS-DAY-EDITED
STRING WS-MONTH-NAME(WS-MM) DELIMITED " "
" " DELIMITED SIZE
WS-DAY-EDITED DELIMITED SPACES
", " DELIMITED SIZE
WS-YYYY DELIMITED SIZE
INTO WS-FORMATTED
END-STRING
DISPLAY WS-FORMATTED
Question 19
What is the recommended approach for accepting and validating numeric input from a user?
Show Answer
1. Accept the input into an alphanumeric field (PIC X), not directly into a numeric field (PIC 9). 2. Validate the input using `IF identifier IS NUMERIC`. 3. If valid, move the value to the numeric field or use `FUNCTION NUMVAL` for conversion. 4. If invalid, display an error message and re-prompt. 01 WS-INPUT PIC X(10).
01 WS-NUMBER PIC 9(7).
DISPLAY "Enter a number: " WITH NO ADVANCING
ACCEPT WS-INPUT
IF WS-INPUT IS NUMERIC
MOVE WS-INPUT TO WS-NUMBER
ELSE
DISPLAY "Error: Please enter digits only."
END-IF
This prevents data exception abends (SOC7) from non-numeric data in arithmetic operations.
Question 20
How does ACCEPT FROM ENVIRONMENT work in GnuCOBOL, and how does this differ from the COBOL 2002 standard approach?
Show Answer
**GnuCOBOL:** ACCEPT WS-VALUE FROM ENVIRONMENT "HOME"
A single statement reads the environment variable directly by name.
**COBOL 2002 standard:**
MOVE "HOME" TO WS-ENV-NAME
DISPLAY WS-ENV-NAME UPON ENVIRONMENT-NAME
ACCEPT WS-VALUE FROM ENVIRONMENT-VALUE
The standard approach is a two-step process: first set the environment variable name using DISPLAY UPON ENVIRONMENT-NAME, then retrieve its value with ACCEPT FROM ENVIRONMENT-VALUE.
The GnuCOBOL extension is more concise and commonly preferred when portability to other compilers is not a concern.
Question 21
What is the output of this code?
DISPLAY "A" WITH NO ADVANCING
DISPLAY "B" WITH NO ADVANCING
DISPLAY "C" WITH NO ADVANCING
DISPLAY "D"
DISPLAY "E"
Show Answer
ABCD
E
The first three DISPLAYs use WITH NO ADVANCING, so A, B, and C are all placed on the same line without line terminators. The fourth DISPLAY adds "D" to the same line and then terminates the line (no WITH NO ADVANCING). The fifth DISPLAY starts on a new line.
Question 22
Explain why the SCREEN SECTION uses DISPLAY screen-name and ACCEPT screen-name instead of individual field-level ACCEPT statements.
Show Answer
Screen-level DISPLAY and ACCEPT operate on the entire screen layout as a unit: - `DISPLAY screen-name` renders all fields, labels, colors, and attributes defined in the screen at once, creating the complete visual layout. - `ACCEPT screen-name` activates all input fields simultaneously, allowing the user to tab between them, fill in multiple fields, and submit them all by pressing Enter. This is more efficient and user-friendly than accepting fields one at a time because the user can see the entire form, navigate freely between fields, and review all entries before submitting. It mimics the behavior of a traditional data entry form on a 3270 terminal.Question 23
What is the significance of the BLANK SCREEN clause in a SCREEN SECTION definition?
Show Answer
`BLANK SCREEN` clears the entire terminal screen before the screen layout is displayed. This ensures a clean presentation without residual output from previous operations. It is typically placed as the first 05-level entry in a screen definition: 01 MY-SCREEN.
05 BLANK SCREEN.
05 LINE 1 COLUMN 1
VALUE "Title" ...
Without `BLANK SCREEN`, the new screen would be overlaid on top of existing content, potentially creating a confusing display.
Question 24
Name three differences between batch and interactive COBOL programs in terms of I/O behavior.
Show Answer
1. **Input source:** Batch programs read from files (sequential, VSAM, DB2) and JCL SYSIN datasets. Interactive programs read from the keyboard using ACCEPT statements. 2. **Output destination:** Batch programs write to files, spool datasets, or databases. Interactive programs write to the terminal screen using DISPLAY statements or SCREEN SECTION. 3. **Program flow:** Batch programs process records sequentially from start to finish without pausing. Interactive programs loop waiting for user input, displaying menus, and processing choices until the user exits. Additional valid differences include: interactive programs require input validation (batch validates data structure), interactive programs use WITH NO ADVANCING for prompts (batch does not), and interactive programs may use the SCREEN SECTION (batch never does).Question 25
What happens if you use a REDEFINES clause on the result of ACCEPT FROM DATE YYYYMMDD, and the date is January 5, 2026?
01 WS-DATE PIC 9(8).
01 WS-DATE-R REDEFINES WS-DATE.
05 WS-YYYY PIC 9(4).
05 WS-MM PIC 9(2).
05 WS-DD PIC 9(2).
Show Answer
After `ACCEPT WS-DATE FROM DATE YYYYMMDD`, the value 20260105 is stored in WS-DATE. The REDEFINES overlays the same 8 bytes with three sub-fields: - WS-YYYY = 2026 - WS-MM = 01 - WS-DD = 05 This works because REDEFINES provides an alternative view of the same memory, and the YYYYMMDD format is specifically designed so that positions 1-4 are the year, 5-6 are the month, and 7-8 are the day. This is one of the most common uses of REDEFINES in COBOL.Question 26
What is the SECURE attribute in the SCREEN SECTION, and when would you use it?
Show Answer
The `SECURE` attribute causes the input field to display asterisks (or similar masking characters) instead of the actual characters the user types. The actual typed characters are still stored in the associated WORKING-STORAGE variable. It is used for password entry fields where the input should be hidden from observers: 05 LINE 5 COLUMN 15
PIC X(8) USING WS-PASSWORD
FOREGROUND-COLOR 7
SECURE.
This is the COBOL equivalent of `` in HTML or password fields in modern GUI frameworks.
Question 27
What is the WITH POINTER clause in the STRING statement, and how does it help?
Show Answer
The WITH POINTER clause specifies a numeric variable that tracks the current position in the destination field during STRING operations. Before the STRING executes, the pointer indicates where to start writing in the destination. After the STRING completes, the pointer is updated to the position just past the last character written. 01 WS-PTR PIC 99 VALUE 1.
01 WS-RESULT PIC X(50) VALUE SPACES.
STRING "Hello" DELIMITED SIZE
INTO WS-RESULT
WITH POINTER WS-PTR
END-STRING
*> WS-PTR is now 6 (position after "Hello")
It helps because:
1. You can chain multiple STRING operations, each continuing where the last one left off.
2. You can determine the actual length of the concatenated result by examining the pointer value minus 1.
3. You can start writing at a specific position for formatting purposes (for example, to center text).
Question 28
True or False: In GnuCOBOL, ACCEPT FROM COMMAND-LINE reads individual command-line arguments one at a time.
Show Answer
**False.** `ACCEPT FROM COMMAND-LINE` reads the entire command line as a single string, including all arguments. To access individual arguments, GnuCOBOL provides `ACCEPT FROM ARGUMENT-NUMBER` (to get the count of arguments) and `ACCEPT FROM ARGUMENT-VALUE` (to read specific arguments by position). The standard COMMAND-LINE accept retrieves the entire invocation string.Question 29
Write the minimal COBOL code to display the current time in 12-hour format with AM/PM.
Show Answer
01 WS-TIME PIC 9(8).
01 WS-TIME-R REDEFINES WS-TIME.
05 WS-HH PIC 9(2).
05 WS-MM PIC 9(2).
05 WS-SS PIC 9(2).
05 WS-HS PIC 9(2).
01 WS-12-HOUR PIC 9(2).
01 WS-AMPM PIC X(2).
ACCEPT WS-TIME FROM TIME
IF WS-HH >= 12
MOVE "PM" TO WS-AMPM
IF WS-HH > 12
SUBTRACT 12 FROM WS-HH GIVING WS-12-HOUR
ELSE
MOVE WS-HH TO WS-12-HOUR
END-IF
ELSE
MOVE "AM" TO WS-AMPM
IF WS-HH = 0
MOVE 12 TO WS-12-HOUR
ELSE
MOVE WS-HH TO WS-12-HOUR
END-IF
END-IF
DISPLAY WS-12-HOUR ":" WS-MM ":" WS-SS " " WS-AMPM
Question 30
A colleague writes the following code and complains that the employee name has "extra spaces in the middle" when displayed. Explain the problem and how to fix it.
01 WS-FIRST PIC X(15) VALUE "JOHN".
01 WS-LAST PIC X(15) VALUE "SMITH".
DISPLAY "Employee: " WS-FIRST " " WS-LAST
Show Answer
**Problem:** The DISPLAY statement concatenates items directly. WS-FIRST is PIC X(15), so "JOHN" is followed by 11 trailing spaces. The output looks like:Employee: JOHN SMITH
There appear to be 12 extra spaces between the names (11 trailing spaces from WS-FIRST plus the explicit " " literal).
**Fix:** Use the STRING statement with DELIMITED BY SPACES to trim trailing spaces:
01 WS-FULL-NAME PIC X(31).
STRING "Employee: " DELIMITED SIZE
WS-FIRST DELIMITED SPACES
" " DELIMITED SIZE
WS-LAST DELIMITED SPACES
INTO WS-FULL-NAME
END-STRING
DISPLAY WS-FULL-NAME
Output:
Employee: JOHN SMITH
Alternatively, in GnuCOBOL with COBOL 2014 support:
DISPLAY "Employee: "
FUNCTION TRIM(WS-FIRST) " "
FUNCTION TRIM(WS-LAST)
Scoring Guide
| Score | Rating |
|---|---|
| 27-30 | Excellent -- you have mastered Chapter 5 |
| 22-26 | Good -- review the topics you missed |
| 17-21 | Fair -- reread the relevant sections |
| Below 17 | Needs work -- revisit the chapter and examples |