Cobol Test Review
- As an acronym, what does COBOL represent?
Common Business Oriented Language
- What are the four divisions of a Cobol program?
1)Identification
2)Environment
3)Data
4)Procedure
- Describe how variables are allocated in Cobol.
Static
- Show the hex contents of the following fields:
a) 15 FIELDA PIC X(3) VALUE "ABC". C1C2C3
b) 15 FIELDA PIC X(3) VALUE "A". C14040
c) 15 FIELDA PIC X(3) VALUE "ABCDEFG". C1C2C3
d) 15 FIELDA PIC X(3) VALUE "ABC". C1C2C3
e) 15 FIELDA PIC S9(3) VALUE "123". F1F2C3
f) 15 FIELDA PIC 9(3) VALUE "123". F1F2C3
g) 15 FIELDA PIC S9(3) VALUE "-123". F1F2D3
h) 15 FIELDA PIC S9(3) VALUE "123456". F4F5C6
i) 15 FIELDA PIC S9(5) PACKED-DECIMAL VALUE "123". 00123C
j) 15 FIELDA PIC S9(5) PACKED-DECIMAL VALUE "-123". 00123D
k) 15 FIELDA PIC S9(5) PACKED-DECIMAL VALUE "-12345". 12345D
15 FIELDA PIC S9(4) PACKED-DECIMAL VALUE “-12345”. 02345D
l) 15 FIELDA PIC S9(3)V99 PACKED-DECIMAL VALUE "12.34". 01234C
15 FIELDA PIC S9(7) PACKED-DECIMAL VALUE ‘123.45” 0012345C
m) 15 FIELDA PIC S9(8) BINARY VALUE 24. 00000018
n) 15 FIELDA PIC S9(4) BINARY VALUE 24. 0018
o) 15 FIELDA PIC S9(4) BINARY VALUE -3. FFFD
- Describe suitable output PICs for the fields described above in j,k, and l.
J&K. PIC ZZZZ9- L. ZZZ.99-
- Consider the information below:
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO MYIFILE.
SELECT OUTPUT-FILE ASSIGN TO MYOFILE.
What is INPUT-FILE and where is it defined? Internal file name defined in an FD.
What is MYIFILE and where is it defined?
It is the DD Name of the file defined in the JCL.
- Consider the information below:
FD CUSTOMER
RECORDING MODE IS F.
01 OUTPUT-REC.
10 PIC X(20) VALUE SPACES.
10 FIELDA-O PIC ZZ.99-.
10 PIC X(5).
10 FIELDB-O PIC Z,ZZ9.999-.
10 PIC X(5).
10 FIELDC-O PIC ZZZ,ZZ9.9-.
10 PIC X(79).
What does the RECORDING MODE IS F clause indicate? All records are fixed in length.
Where is the storage associated with OUTPUT-REC?In system supplied buffers.
Why would you code multiple 01s in an FD?You need multiple record types in the file, or for variable length records.
What does the compiler do with VALUE clauses in an FD? Value clauses are ignored – there is no working storage to initialize.
How would you print a record to the CUSTOMER file?
WRITE OUTPUT-REC
or
WRITE OUTPUT-REC FROM (SOME OTHER AREA)
- Consider the information below:
FD INPUT-FILE
RECORDING MODE IS F.
01 INPUT-REC.
10 FIELDA PIC S9(2)V99.
10 FIELDB PIC S9(4)V9(3) PACKED-DECIMAL.
10 FIELDC PIC S9(6)V9 PACKED-DECIMAL.
10 PIC X(68).
Are value clauses allowed in INPUT-REC? YES, but ignored.
How do you read a record from INPUT-FILE?
READ INPUT FILE
AT END
…
END-READ
Where can you define fields in COBOL? WORKING-STORAGE SECTION, FD’S, LINKAGE-SECTION
10. Show how to define a name field that consists of three subfields: first, last and middle initial.
10 A-NAME.
15 FIRST-NAME PIC X(10).
15 MI PIC X.
15 LAST-NAME PIC X(20).
11. Show how to define a table of 50 name fields. Use the name structure you built in #10. Define the table twice – with and without indexes.
01 MY-TABLE.
10 A-NAME OCCURS 50 TIMES.
15 FIRST-NAME PIC X(10).
15 MI PIC X.
15 LAST-NAME PIC X(20).
01 MY-TABLE.
10 A-NAME OCCURS 50 TIMES INDEXED BY NDX.
15 FIRST-NAME PIC X(10).
15 MI PIC X.
15 LAST-NAME PIC X(20).
12. Show how to define a two dimensional table of name fields with 5 rows and 10 columns. Use the name structure you built in #10.
01 MY-TABLE.
05 ROW OCCURS 5 TIMES INDEXED BY I.
10 A-NAME OCCURS 10 TIMES INDEXED BY J.
15 FIRST-NAME PIC X(10).
15 MI PIC X.
15 LAST-NAME PIC X(20).
13. Show how to define a Sudoku table.
01 SUDOKU.
10 ROW OCCURS 9 TIMES
20 CELL PIC X OCCURS 9 TIMES.
14. Write a loop that would print the table of names in #11. Show how to do this with both indexes and without.
Using indexes manually, show how to print the 10th and 11th names.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 50
DISPLAY A-NAME(I)
END-PERFORM
MOVE 1 TO I
or
PERFORM UNTIL I > 50
DISPLAY A-NAME(I)
ADD 1 TO I
END-PERFORM
15. Write a loop that would print the Sudoku table in #13 in a rectangular form.
01 OUT-REC.
10 ITEM OCCURS 9 TIMES.
20 CELL-O PIC X.
20 PIC X VALUE SPACE.
10 PIC X(114).
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 9
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 9
MOVE CELL(I , J) TO CELL-O(J)
END-PERFORM
WRITE OUTPUT-REC FROM OUT-REC AFTER ADVANCING 1 LINE
END-PERFORM
16. Write a loop that would check the rows of a Sudoku table to see if a rule has been violated.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 9
PERFORM VARYING J FROM 1 BY 1 UNTIL I > 9
PERFORM VARYING K FROM J + 1 BY 1 UNTIL K > 8
IF CELL(I J) = CELL(I K)
DISPLAY “BAD ROW”
MOVE 10 TO K
MOVE 10 TO J
MOVE 10 TO I
END-IF
END-PERFORM
END-PERFORM
END-PERFORM
17. Write a loop that would read all the records in INPUT-FILE defined in #8.
READ INPUT FILE AT END
MOVE ‘YES’ TO EOF
END-READ
PERFORM UNTIL EOF
…
READ INPUT FILE AT END
MOVE ‘YES’ TO EOF
END-READ
END-PERFORM
18. How does Cobol determine if a file is used for input or output? Determined by the open statement
OPEN INPUT
OPEN OUTPUT
OPEN EXTEND
19. Consider the field below:
15 FIELDA PIC S9(5).
Show how give the field a second name and a PIC with all X’s.
15 FIELDA PIC S9(5).
15 FIELDA-1 REDEFINES FIELDA PIC X(5).
20. To assign FIELDB to FIELDA, what would you code if they are both PIC X(10) fields? What if they were numeric fields?
MOVE FIELDB TO FIELDA
Numeric: MOVE FIELDB TO FIELDA OR
COMPUTE FIELDA = FIELDB
21. Describe two ways to set a numeric field to 0 at run-time.
LET X BE THE FIELD
1) MOVE 0 TO X
2) INITIALIZE X
22. Describe two ways to set an alphanumeric field to spaces at run-time.
1) MOVE SPACES TO X
2) INITIALIZE X
23. Define a table of 10 integers in memory. Write the code to compute the average of the integers.
01 MYTABLE.
10 THE-DATA PIC X’12345678901234567890’.
10 X REDEFINES THE-DATA PIC S99 OCCURS 10 TIMES.
01AVG PIC S999V99 PACKED-DECIMAL.
01 TOTAL PIC S99999V99.
COMPUTE AVG = FUNCTION AVERAGE(X(ALL))
or
MOVE 0 TO TOTAL
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
ADD X(I) TO TOTAL
END-PERFORM
COMPUTE TOTAL = TOTAL / 10
24. Consider the following fields:
15 A PIC S9(5)V9(4) PACKED-DECIMAL.
15 B PIC S9(5)V9(4) PACKED-DECIMAL.
15 C PIC S9(5)V9(4) PACKED-DECIMAL.
15 D PIC S9(5)V9(4) PACKED-DECIMAL.
15 ANS PIC S9(5)V9(4) PACKED-DECIMAL.
Show how to compute –b + √(b2 – 4ac)
2a
Store the answer in ANS.
COMPUTE ANS = (- b + FUNCTION SQRT ((b * b) – (4 * A * C))) / ( 2 * A)
25. Consider the following fields:
15 A PIC S9(5) PACKED-DECIMAL.
15 B PIC S9(5) PACKED-DECIMAL.
15 REM PIC S9(5) PACKED-DECIMAL.
15 X PIC S9(5) PACKED-DECIMAL.
Show how to compute the remainder of A/B. Put the answer in REM.
DIVIDE A BY B GIVING X REMAINDER REM
26. Write the code that would display the smallest of three integer fields X,Y, and Z.
MOVE X TO SMALLEST
IF (Y < SMALLEST) MOVE Y TO SMALLEST END-IF
IF (Z < SMALLEST) MOVE Z TO SMALLEST END-IF
DISPLAY SMALLEST
27. Write an EVALUATE that would examine integer field AGE and display “Baby” if AGE <= 3, “Child” if the 4 <= AGE <= 17, and “Adult” if AGE >= 18.
EVALUATE TRUE
WHEN AGE < 3 DISPLAY “BABY”
WHEN AGE < 17 DISPLAY “CHILD”
WHEN AGE IS POSITIVE DISPLAY “ADULT”
END-EVALUATE
28. Write the code that would execute paragraph 100-PARA 10 times.
PERFORM 100-PAR 10 TIMES
29. Write the code that would repeatedly execute paragraph 100-PARA until integer field X > 100.
PERFORM 100-PARA UNTIL X > 100
30. Use a loop control variable I to execute paragraph 100-PARA 100 times.
PERFORM 100-PARA VARYING I FROM 1 BY 1 UNTIL I > 100
31. Write the code that would call program SUBPROGA.
MOVE “SUBPROGA” TO PROGNAME
CALL PROGNAME
or
CALL “SUBPROGA”
32. Assume you have defined variables X, Y, and Z with PIC S9(5). Show the relevant code needed to call program SUBPROGA passing X,Y and Z.
CALL “SUBPROGA” USING BY CONTENT X Y Z
OR
CALL “SUBPROGA” USING X Y Z
33. Show the relevant code in SUBPROGA needed to accept the variables passed in #32.
LINKAGE SECTION.
01 A PIC S9(5).
01 B PIC S9(5).
01 C PIC S9(5).
PROCEDURE DIVISION USING A B C.
34. What is the default parameter passing method in Cobol? BY REFERENCE
35. What is the effect of passing a parameter BY CONTENT?
A COPY OF THE PARAMETER IS MADE AND THE COPY IS PASSED BY REFERENCE
36. Write an in-line PERFORM that displays 1,2,3,…10 on separate lines.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
DISPLAY I
END-PERFORM
37. Describe how to search a table sequentially.
Use a Search command. Set the index to an initial value. Issue the search on the field that occurs.
38. Describe how to search a table with a binary search.
The data has to be sorted. Issue a Search All.
39. Assume the following fields:
15 X PIC X(10) VALUE “ABC”.
15 Y PIC X(10) VALUE “CDE”.
15 Z PIC X(10) VALUE “FGH”.
15 ANS PIC X(30).
Show how to concatenate fields X, Y, and Z into ANS.
STRING X DELIMITED BY SIZE
Y DELIMITED BY SIZE
Z DELIMITED BY SIZE INTO ANS
Show how to concatenate fields X, Y, and Z into ANS removing the spaces inside each field.
STRING X DELIMITED BY “ “
Y DELIMITED BY “ “
Z DELIMITED BY “ “ INTO ANS
40. Write the code that will display field X if it contains numeric data.
IF X IS NUMERIC
DISPLAY X
END-IF
41. Consider the field below:
15 X PIC X(10) VALUE ‘ABCDEFGHIJ’.
Show how to use reference modification to print the following:
AB
BC
CD
…
IJ
PEFORM VARYING I FROM 1 BY 1 UNTIL I > 9
DISPLAY X(I : 2)
END-IF
42. Show how to define a variable length table. How much space is allocated to your table? When should you use a variable length table?
01 MY-TABLE.
10 X PIC X(3) OCCURS 1 TO 100 TIMES DEPENDING ON J.