COBOL Realtime (Scenario based) Interview Questions (11 - 20)
11. I have declared two variables WS-A and WS-B. WS-A is declared with level number 05 and WS-B with 10. Which one is larger and why?
01 WS-GROUP.
05 WS-A PIC X(10).
10 WS-B PIC X(5).
WS-A is larger because of its higher level in the COBOL hierarchy.
- Level numbers determine data hierarchy, not size.
- Lower level numbers (e.g., 01, 05) indicate a larger structure that may contain nested fields.
- Higher level numbers (e.g., 10, 15) are subordinate to lower-level numbers.
12. Can we declare two variables with same name and same level numbers?
Yes, but only if they are in different groups (separate hierarchical structures).
01 GROUP-1.
05 WS-VAR PIC X(10).
01 GROUP-2.
05 WS-VAR PIC 9(5).
No, if they are within the same group (nested structure), as it causes a compilation error.
01 GROUP-1.
05 WS-VAR PIC X(10).
05 WS-VAR PIC 9(5). *> ERROR: Duplicate variable name
13. Can we declare two variables with same name and different level numbers?
Allowed if the variables belong to separate groups. Not allowed within the same group structure.
14. My requirement is to declare two variables with same name. How can we do that?
You can declare two variables with the same name in COBOL using different group structures:
01 GROUP-1.
05 WS-VAR PIC X(10).
01 GROUP-2.
05 WS-VAR PIC 9(5).
15. Can I declare a variable without PIC clause? How?
Yes, we can declare the below variables without PIC clause -
- Group variables.
- 66 level RENAMES provides an alternative name.
- Table definition.
16. Is the following declaration is correct?
01 WS-A.
05 WS-A1 PIC X(05).
05 WS-A2 PIC X(05).
05 WS-A3 PIC X(05).
05 WS-A4 PIC X(05).
66 WS-B RENAMES WS-A2 THRU WS-A3.
Yes, this declaration is correct. 66 RENAMES allows renaming a range of fields within a single group-level variable (01 level). WS-B now refers to both WS-A2 and WS-A3 together.
17. What is the value of WS-B in the below code -
01 WS-A.
05 WS-A1 PIC X(05).
05 WS-A2 PIC X(05).
05 WS-A3 PIC X(05).
05 WS-A4 PIC X(05).
66 WS-B RENAMES WS-A1 THRU WS-A2.
...
PROCEDURE DIVISION.
MOVE "MAINFRAMESTECHHELP" TO WS-A.
Breakdown of Data Assignment: WS-A receives "MAINFRAMESTECHHELP" (20 characters). Fields get the following values:
WS-A1 = "MAINF" WS-A2 = "RAMES" WS-A3 = "TECHH" WS-A4 = "ELP "
Value of WS-B: WS-B renames WS-A1 THRU WS-A2, meaning it represents the combined value of WS-A1 and WS-A2. WS-B = "MAINFRAMES" (First 10 characters from WS-A1 + WS-A2).
18. Go through the below code and explain what is the value in WS-A?
01 WS-A.
88 WS-A-VALID VALUE "A" "E" "I" "O" "U".
...
PROCEDURE DIVISION.
SET WS-A-VALID TO TRUE.
WS-A will hold one of the valid values ("A", "E", "I", "O", or "U"), but the exact value is not explicitly defined in COBOL.
19. Go through the below code and explain what is the value in WS-A?
01 WS-A.
88 WS-A-VALID VALUE "A" THRU "Z".
...
PROCEDURE DIVISION.
SET WS-A-VALID TO TRUE.
WS-A will hold one valid character from "A" to "Z", typically "A" by default, but exact behavior depends on the COBOL compiler.
20. What is the difference between 01 and 77 level number?
Level 01 | Level 77 |
---|---|
Used for group items (can contain subfields) or elementary items. | Used for standalone elementary items only (cannot have subfields). |
Can define group structures with nested levels (05, 10, etc.). | Cannot be grouped or have subordinate fields. |
Commonly used for records, structures, and group data definitions. | Used for single-value variables (constants, counters, etc.). |
Group item or standalone:
|
Standalone variable:
|