Cobol Coding Questions
Cobol Coding Questions
Explanation:
This program reverses a given string by iterating through it backward and storing the reversed
result in another variable.
cbol
CopyEdit
IDENTIFICATION DIVISION.
PROGRAM-ID. ReverseString.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUT-STRING PIC X(50) VALUE 'HELLO WORLD'. * Input string to
reverse.
01 WS-OUTPUT-STRING PIC X(50). * Stores the reversed
string.
01 WS-LENGTH PIC 9(4) COMP. * Holds the length of
the input string.
01 WS-INDEX PIC 9(4) COMP. * Loop index variable.
PROCEDURE DIVISION.
MOVE FUNCTION LENGTH(WS-INPUT-STRING) TO WS-LENGTH * Get the length of
the string.
PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > WS-LENGTH
MOVE WS-INPUT-STRING(WS-LENGTH - WS-INDEX + 1:1) * Pick characters
from the end.
TO WS-OUTPUT-STRING(WS-INDEX:1) * Store them in
reverse order.
END-PERFORM
DISPLAY 'REVERSED STRING: ' WS-OUTPUT-STRING * Display the
reversed string.
STOP RUN.
Key Points:
Explanation:
This JCL script compiles a COBOL program and executes it.
//JOBNAME JOB (ACCT),'DESCRIPTION',CLASS=A,MSGCLASS=A,NOTIFY=&SYSUID
//STEP1 EXEC PGM=IGYCRCTL * COBOL
compiler.
//SYSIN DD DSN=MY.COBOL.SOURCE(REVERSE),DISP=SHR * Input COBOL
source.
//SYSLIB DD DSN=MY.COBOL.COPYLIB,DISP=SHR * Copybook
library.
//SYSLMOD DD DSN=MY.COBOL.LOADLIB,DISP=SHR * Load module
output.
//SYSPRINT DD SYSOUT=* * Compilation
output.
//SYSUT1 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) * Temporary
storage.
//*
//STEP2 EXEC PGM=REVERSE * Execute the
compiled program.
//STEPLIB DD DSN=MY.COBOL.LOADLIB,DISP=SHR * Path to load
module.
//SYSOUT DD SYSOUT=* * Program
output.
//SYSIN DD * * Input to the
program.
INPUT STRING: HELLO WORLD * Example
input.
Key Points:
Explanation:
This program fetches employee details from a DB2 table using embedded SQL.
IDENTIFICATION DIVISION.
PROGRAM-ID. FetchEmployeeDetails.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC. * SQL
communication area.
01 WS-EMP-ID PIC X(10). * Holds employee
ID.
01 WS-EMP-NAME PIC X(50). * Holds employee
name.
01 WS-EMP-SALARY PIC 9(9)V99. * Holds employee
salary.
01 WS-SQLCODE PIC S9(4) COMP. * Holds SQL
return code.
PROCEDURE DIVISION.
EXEC SQL
DECLARE EMPLOYEE_CURSOR CURSOR FOR * Declare cursor
to fetch rows.
SELECT EMP_ID, EMP_NAME, EMP_SALARY
FROM EMPLOYEE
END-EXEC.
Key Points:
Explanation:
This program reads a record from a VSAM file using CICS commands.
IDENTIFICATION DIVISION.
PROGRAM-ID. ReadVSAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-EMP-ID PIC X(10). * Key of the VSAM
record.
01 WS-EMP-DATA PIC X(100). * Data read from
VSAM file.
01 WS-RESP-CODE PIC S9(4) COMP. * CICS response
code.
FILE-CONTROL.
SELECT EMPFILE ASSIGN TO 'EMPLOYEE.VSAM'. * VSAM file name.
PROCEDURE DIVISION.
EXEC CICS READ
FILE('EMPFILE') * File name.
INTO(WS-EMP-DATA) * Buffer to hold
record data.
RIDFLD(WS-EMP-ID) * Key for record
search.
RESP(WS-RESP-CODE) * Response code.
END-EXEC.
STOP RUN.
Key Points:
Explanation:
This program searches for a specific value in a COBOL table using a sequential search.
IDENTIFICATION DIVISION.
PROGRAM-ID. SearchTable.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-ELEMENT OCCURS 10 TIMES INDEXED BY IDX. * Table with 10
elements.
10 WS-VALUE PIC X(10). * Value of each
element.
01 WS-SEARCH-VALUE PIC X(10) VALUE 'VALUE05'. * Value to search.
PROCEDURE DIVISION.
MOVE 'VALUE01' TO WS-VALUE(1) * Initialize table
elements.
MOVE 'VALUE02' TO WS-VALUE(2)
MOVE 'VALUE05' TO WS-VALUE(3)
SET IDX TO 1 * Start index at 1.
PERFORM UNTIL IDX > 10 OR WS-VALUE(IDX) = WS-SEARCH-VALUE
SET IDX UP BY 1 * Increment index.
END-PERFORM
STOP RUN.
Key Points:
IDENTIFICATION DIVISION.
PROGRAM-ID. FindMaximum.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-ELEMENT OCCURS 10 TIMES INDEXED BY IDX. * Table of 10
elements.
10 WS-VALUE PIC 9(3). * Numeric value of
each element.
01 WS-MAX-VALUE PIC 9(3) VALUE 0. * Stores the maximum
value.
01 WS-CURRENT-VALUE PIC 9(3). * Holds current value
during iteration.
PROCEDURE DIVISION.
MOVE 10 TO WS-VALUE(1) * Initialize table
values.
MOVE 20 TO WS-VALUE(2)
MOVE 50 TO WS-VALUE(3)
MOVE 30 TO WS-VALUE(4)
MOVE 40 TO WS-VALUE(5)
Key Points:
Question: Write a JCL script to create a sequential file and write data into it.
jcl
CopyEdit
//JOBNAME JOB (ACCT),'CREATE FILE',CLASS=A,MSGCLASS=A,NOTIFY=&SYSUID
//STEP1 EXEC PGM=IEFBR14 * Utility to create
files.
//DD1 DD DSN=MY.DATA.FILE, * Define the new
file.
// DISP=(NEW,CATLG,DELETE), * New file; catalog
it.
// SPACE=(CYL,(1,1)), * Allocate space.
// DCB=(RECFM=FB,LRECL=80,BLKSIZE=800) * File attributes.
//*
//STEP2 EXEC PGM=IEBGENER * Write data to the
file.
//SYSUT1 DD * * Input data.
//NAME: JOHN DOE
//AGE: 30
//LOCATION: NEW YORK
//SYSUT2 DD DSN=MY.DATA.FILE,DISP=SHR * Output file.
//SYSPRINT DD SYSOUT=* * Print output.
//SYSIN DD DUMMY * No control
statements.
Key Points:
Question: Write a COBOL-DB2 program to update an employee's salary in the EMPLOYEE table.
IDENTIFICATION DIVISION.
PROGRAM-ID. UpdateSalary.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC. * SQL communication
area.
01 WS-EMP-ID PIC X(10) VALUE 'E001'. * Employee ID to
update.
01 WS-NEW-SALARY PIC 9(9)V99 VALUE 75000. * New salary to set.
01 WS-SQLCODE PIC S9(4) COMP. * SQL return code.
PROCEDURE DIVISION.
EXEC SQL
UPDATE EMPLOYEE
SET EMP_SALARY = :WS-NEW-SALARY * Update salary
field.
WHERE EMP_ID = :WS-EMP-ID * Match the given ID.
END-EXEC.
STOP RUN.
Key Points:
IDENTIFICATION DIVISION.
PROGRAM-ID. SortArray.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-NUMBER OCCURS 5 TIMES INDEXED BY IDX1 IDX2. * Array of 5 numbers.
10 WS-VALUE PIC 9(3). * Each number is 3
digits.
01 WS-TEMP PIC 9(3). * Temporary storage
for swapping.
PROCEDURE DIVISION.
MOVE 50 TO WS-NUMBER(1) * Initialize array.
MOVE 20 TO WS-NUMBER(2)
MOVE 40 TO WS-NUMBER(3)
MOVE 10 TO WS-NUMBER(4)
MOVE 30 TO WS-NUMBER(5)
STOP RUN.
Key Points:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INTEREST-RATE PIC 9(3)V99 VALUE 0.02. "Annual interest
rate (2%)."
01 WS-DAYS-IN-YEAR PIC 9(3) VALUE 365. "Number of days in a
year, used for daily interest calculation."
01 WS-ACCOUNT-ID PIC X(10). "Holds the account
ID fetched from the database."
01 WS-ACCOUNT-BALANCE PIC 9(9)V99. "Stores the current
account balance."
01 WS-DAILY-INTEREST PIC 9(9)V99. "Holds the
calculated daily interest for an account."
PROCEDURE DIVISION.
DISPLAY 'STARTING DAILY INTEREST CALCULATION' "Logs the start of
the process."
EXEC SQL
DECLARE ACCOUNTS CURSOR FOR
SELECT ACCOUNT_ID, BALANCE
FROM SAVINGS_ACCOUNTS
WHERE STATUS = 'ACTIVE' "Fetches only active
savings accounts."
END-EXEC.
COMPUTE WS-ACCOUNT-BALANCE =
WS-ACCOUNT-BALANCE + WS-DAILY-INTEREST "Adds daily interest
to the account balance."
EXEC SQL
UPDATE SAVINGS_ACCOUNTS
SET BALANCE = :WS-ACCOUNT-BALANCE
WHERE ACCOUNT_ID = :WS-ACCOUNT-ID "Updates the balance
in the database."
END-EXEC.
END-IF
END-PERFORM.
Explanation:
1. Purpose: Automates daily interest calculations for all active savings accounts.
2. Key Operations:
o Cursor fetches account details from the SAVINGS_ACCOUNTS table.
o Interest is calculated using the formula (Balance * Interest Rate) / 365.
o Updated balances are stored back in the database.
3. SQLCODE: Monitors successful fetches (SQLCODE = 0) and determines the end of data
(SQLCODE = 100).
2. Batch File Processing for Bulk Payments
IDENTIFICATION DIVISION.
PROGRAM-ID. ProcessBulkPayments.
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD.
05 ACCOUNT-ID PIC X(10). "Holds the account
ID from the input file."
05 PAYMENT-AMOUNT PIC 9(9)V99. "Holds the payment
amount from the input file."
WORKING-STORAGE SECTION.
01 WS-ACCOUNT-ID PIC X(10). "Stores the current
account ID."
01 WS-PAYMENT-AMOUNT PIC 9(9)V99. "Stores the current
payment amount."
01 WS-ACCOUNT-BALANCE PIC 9(9)V99. "Holds the fetched
balance of the current account."
PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE "Opens the input
file for reading."
DISPLAY 'PROCESSING BULK PAYMENTS'
EXEC SQL
SELECT BALANCE
INTO :WS-ACCOUNT-BALANCE
FROM ACCOUNTS
WHERE ACCOUNT_ID = :WS-ACCOUNT-ID "Fetches the current
account balance."
END-EXEC
EXEC SQL
UPDATE ACCOUNTS
SET BALANCE = :WS-ACCOUNT-BALANCE
WHERE ACCOUNT_ID = :WS-ACCOUNT-ID "Updates the balance
in the database."
END-EXEC
ELSE
DISPLAY 'ACCOUNT NOT FOUND: ' WS-ACCOUNT-ID "Logs error if the
account is missing."
END-IF
Explanation:
1. Purpose: Processes payment instructions from a file and updates account balances.
2. Key Operations:
o Reads payment details (account ID and amount) from an input file.
o Deducts payment from the current balance fetched from the ACCOUNTS table.
o Handles errors for invalid accounts.
3. Efficiency: Uses SQL for direct database interaction, minimizing processing time.
3. Transaction Reconciliation
IDENTIFICATION DIVISION.
PROGRAM-ID. TransactionReconciliation.
DATA DIVISION.
FILE SECTION.
FD SYSTEM1-FILE.
01 SYS1-RECORD.
05 SYS1-TRANSACTION-ID PIC X(10). "Transaction ID from
System 1."
05 SYS1-AMOUNT PIC 9(9)V99. "Transaction amount
from System 1."
FD SYSTEM2-FILE.
01 SYS2-RECORD.
05 SYS2-TRANSACTION-ID PIC X(10). "Transaction ID from
System 2."
05 SYS2-AMOUNT PIC 9(9)V99. "Transaction amount
from System 2."
WORKING-STORAGE SECTION.
01 WS-SYS1-TRANSACTION-ID PIC X(10). "Stores System 1
transaction ID."
01 WS-SYS2-TRANSACTION-ID PIC X(10). "Stores System 2
transaction ID."
01 WS-SYS1-AMOUNT PIC 9(9)V99. "Stores System 1
transaction amount."
01 WS-SYS2-AMOUNT PIC 9(9)V99. "Stores System 2
transaction amount."
PROCEDURE DIVISION.
OPEN INPUT SYSTEM1-FILE SYSTEM2-FILE "Opens both files
for reading."
DISPLAY 'STARTING TRANSACTION RECONCILIATION'
Explanation:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CARD-NUMBER PIC X(16). "Stores the card
number."
01 WS-EXPIRY-DATE PIC 9(6). "Stores the card
expiry date in YYYYMM format."
01 WS-CURRENT-DATE PIC 9(6). "Stores the current
date in YYYYMM format."
01 WS-VALID-FLAG PIC X(1). "Holds 'Y' for valid,
'N' for expired."
PROCEDURE DIVISION.
DISPLAY 'START CARD EXPIRATION VALIDATION.'
DISPLAY 'CHECK NEXT CARD? ENTER SPACE TO STOP, ELSE ENTER ANY KEY:'
ACCEPT WS-CARD-NUMBER
END-PERFORM.
Explanation:
1. Purpose: Ensures a card is valid by checking its expiry date against the current system date.
2. Key Operations:
o System date is fetched using ACCEPT ... FROM DATE.
o Compares the expiry date with the current date (YYYYMM format).
o Outputs whether the card is valid or expired.
3. Practical Use: Commonly used in authorization systems to reject expired cards.
2. Credit Card Transaction Authorization
Scenario: Validate and authorize a credit card transaction based on available credit limit.
IDENTIFICATION DIVISION.
PROGRAM-ID. TransactionAuthorization.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CARD-NUMBER PIC X(16). "Stores the card
number."
01 WS-TRANSACTION-AMOUNT PIC 9(7)V99. "Stores the
transaction amount."
01 WS-CREDIT-LIMIT PIC 9(9)V99. "Holds the credit
limit of the card."
01 WS-AVAILABLE-CREDIT PIC 9(9)V99. "Holds the available
credit after processing."
01 WS-STATUS-FLAG PIC X(1). "Holds 'A' for
approved, 'D' for declined."
PROCEDURE DIVISION.
DISPLAY 'START CREDIT CARD TRANSACTION AUTHORIZATION.'
IF WS-AVAILABLE-CREDIT >= 0
MOVE 'A' TO WS-STATUS-FLAG
DISPLAY 'TRANSACTION APPROVED. REMAINING CREDIT: ' WS-AVAILABLE-CREDIT
ELSE
MOVE 'D' TO WS-STATUS-FLAG
DISPLAY 'TRANSACTION DECLINED. INSUFFICIENT CREDIT.'
END-IF
Explanation:
IDENTIFICATION DIVISION.
PROGRAM-ID. FraudDetection.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CARD-NUMBER PIC X(16). "Stores the card
number."
01 WS-TRANSACTION-AMOUNT PIC 9(7)V99. "Stores the
transaction amount."
01 WS-THRESHOLD-AMOUNT PIC 9(7)V99 VALUE 5000.00. "Predefined fraud
detection threshold."
PROCEDURE DIVISION.
DISPLAY 'START FRAUDULENT TRANSACTION DETECTION.'
Explanation:
Scenario: Calculate the total debits and credits for the day and update the settlement file.
IDENTIFICATION DIVISION.
PROGRAM-ID. DailySettlement.
DATA DIVISION.
FILE SECTION.
FD TRANSACTION-FILE.
01 TRANSACTION-RECORD.
05 CARD-NUMBER PIC X(16). "Stores the card
number."
05 TRANSACTION-TYPE PIC X(1). "D = Debit, C =
Credit."
05 TRANSACTION-AMOUNT PIC 9(7)V99. "Transaction amount."
WORKING-STORAGE SECTION.
01 WS-TOTAL-DEBITS PIC 9(9)V99 VALUE 0.00. "Total amount of
debits for the day."
01 WS-TOTAL-CREDITS PIC 9(9)V99 VALUE 0.00. "Total amount of
credits for the day."
PROCEDURE DIVISION.
OPEN INPUT TRANSACTION-FILE "Opens the
transaction file for reading."
DISPLAY 'START DAILY SETTLEMENT PROCESS.'
Explanation:
1. Purpose: Calculates the total debit and credit transactions for a day.
2. Key Operations:
o Reads transactions from a file.
o Segregates and sums up debits and credits.
o Outputs the totals for settlement purposes.
3. Practical Use: Forms part of the reconciliation and settlement process in card systems.
Scenario: Update the PIN for a card after verifying the existing PIN.
IDENTIFICATION DIVISION.
PROGRAM-ID. PINChange.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CARD-NUMBER PIC X(16). "Stores the card
number."
01 WS-OLD-PIN PIC X(4). "Stores the existing
PIN entered by the user."
01 WS-NEW-PIN PIC X(4). "Stores the new PIN
entered by the user."
01 WS-STORED-PIN PIC X(4). "Holds the PIN
fetched from the database."
PROCEDURE DIVISION.
DISPLAY 'START PIN CHANGE PROCESS.'
EXEC SQL
SELECT PIN INTO :WS-STORED-PIN
FROM CARD_ACCOUNTS
WHERE CARD_NUMBER = :WS-CARD-NUMBER "Fetches the stored
PIN from the database."
END-EXEC.
IF WS-OLD-PIN = WS-STORED-PIN
DISPLAY 'ENTER NEW PIN:'
ACCEPT WS-NEW-PIN "Accepts the new
PIN."
EXEC SQL
UPDATE CARD_ACCOUNTS
SET PIN = :WS-NEW-PIN
WHERE CARD_NUMBER = :WS-CARD-NUMBER "Updates the PIN in
the database."
END-EXEC
DISPLAY 'PIN CHANGE SUCCESSFUL.'
ELSE
DISPLAY 'INVALID EXISTING PIN.'
END-IF
Explanation:
1. Purpose: Securely updates the PIN for a card after user authentication.
2. Key Operations:
o Validates the existing PIN.
o Updates the database with the new PIN if validation succeeds.
3. Practical Use: Used in ATM or online banking systems for secure PIN management.