Show List

Control Language/CL400 Coding Questions


  • Write a CL program to add two numbers and display the result.
scss
Copy code
PGM DCL VAR(&NUM1) TYPE(*DEC) LEN(3 0) VALUE(10) DCL VAR(&NUM2) TYPE(*DEC) LEN(3 0) VALUE(20) DCL VAR(&SUM) TYPE(*DEC) LEN(4 0) CHGVAR VAR(&SUM) VALUE(&NUM1 + &NUM2) SNDPGMMSG MSG('The sum is ' || &SUM) ENDPGM
  • Write a CL program to read a file and display its contents.
scss
Copy code
PGM DCL VAR(&FILE) TYPE(*CHAR) LEN(10) VALUE('MYFILE') DCL VAR(&RCDFMT) TYPE(*CHAR) LEN(10) VALUE('MYFORMAT') DCL VAR(&EOF) TYPE(*LGL) OVRDBF FILE(MYFILE) TOFILE(QTEMP/MYFILE) DO WHILE COND(&EOF = *OFF) RCVF FILE(QTEMP/MYFILE) RCDFMT(&RCDFMT) EOF(&EOF) IF COND(&EOF = *OFF) THEN(DO) /* process record */ ENDDO ENDDO DLTOVR FILE(MYFILE) ENDPGM
  • Write a CL program to search for a string in a file and display the matching records.
scss
Copy code
PGM DCL VAR(&FILE) TYPE(*CHAR) LEN(10) VALUE('MYFILE') DCL VAR(&RCDFMT) TYPE(*CHAR) LEN(10) VALUE('MYFORMAT') DCL VAR(&EOF) TYPE(*LGL) DCL VAR(&SEARCH) TYPE(*CHAR) LEN(10) VALUE('SEARCH') OVRDBF FILE(MYFILE) TOFILE(QTEMP/MYFILE) DO WHILE COND(&EOF = *OFF) RCVF FILE(QTEMP/MYFILE) RCDFMT(&RCDFMT) EOF(&EOF) IF COND(&EOF = *OFF AND %SST(&FIELD1 1 6) = &SEARCH) THEN(DO) /* process record */ ENDDO ENDDO DLTOVR FILE(MYFILE) ENDPGM
  • Write a CL program to update a record in a file.
scss
Copy code
PGM DCL VAR(&FILE) TYPE(*CHAR) LEN(10) VALUE('MYFILE') DCL VAR(&RCDFMT) TYPE(*CHAR) LEN(10) VALUE('MYFORMAT') DCL VAR(&KEY) TYPE(*CHAR) LEN(10) VALUE('KEY') DCL VAR(&FIELD) TYPE(*CHAR) LEN(10) VALUE('FIELD') OVRDBF FILE(MYFILE) TOFILE(QTEMP/MYFILE) UPDPFM FILE(QTEMP/MYFILE) MBR(MYMEMBER) KEYFLD(&KEY) RCDDATA(&FIELD) DLTOVR FILE(MYFILE) ENDPGM
  • Write a CL program to copy a file to another file.
scss
Copy code
PGM DCL VAR(&SRCFILE) TYPE(*CHAR) LEN(10) VALUE('SRCFILE') DCL VAR(&SRCMBR) TYPE(*CHAR) LEN(10) VALUE('SRCMBR') DCL VAR(&TGTFILE) TYPE(*CHAR) LEN(10) VALUE('TGTFILE') DCL VAR(&TGTMBR) TYPE(*CHAR) LEN(10) VALUE('TGTMBR') CPYF FROMFILE(&SRCFILE) TOFILE(&TGTFILE) FROMMBR(&SRCMBR) TOMBR(&TGTMBR) ENDPGM
  • Write a CL program to rename a file.
scss
Copy code
PGM DCL VAR(&FILE) TYPE(*CHAR) LEN(10) VALUE('OLDNAME') DCL VAR(&NEWNAME) TYPE(*CHAR) LEN(10) VALUE('NEWNAME') RNMOBJ OBJ(&FILE) OBJTYPE(*FILE) NEWOBJ(&NEWNAME) ENDPGM
  • Write a CL program to create a new library.
scss
Copy code
PGM DCL VAR(&LIBRARY) TYPE(*CHAR) LEN(10) VALUE('NEWLIB') CRTLIB LIB(&LIBRARY) ENDPGM
  • Write a CL program to create a new user profile.
scss
Copy code
PGM DCL VAR(&USER) TYPE(*CHAR) LEN(10) VALUE('NEWUSER') DCL VAR(&PASSWORD) TYPE(*CHAR) LEN(10) VALUE('PASSWORD') DCL VAR(&TEXT) TYPE(*CHAR) LEN(30) VALUE('New User') CRTUSRPRF USRPRF(&USER) PASSWORD(&PASSWORD) TEXT(&TEXT) ENDPGM
  • Write a CL program to change the password for a user profile.
scss
Copy code
PGM DCL VAR(&USER) TYPE(*CHAR) LEN(10) VALUE('USER') DCL VAR(&PASSWORD) TYPE(*CHAR) LEN(10) VALUE('NEWPASS') CHGUSRPRF USRPRF(&USER) PASSWORD(&PASSWORD) ENDPGM
  • Write a CL program to reset a user's failed sign-on attempts.
scss
Copy code
PGM DCL VAR(&USER) TYPE(*CHAR) LEN(10) VALUE('USER') CHGUSRPRF USRPRF(&USER) TRNFLD(FRCNPWD) ENDPGM
  • Write a CL program to display a list of active jobs.
Copy code
PGM WRKACTJOB ENDPGM
  • Write a CL program to display the spool files for a user.
scss
Copy code
PGM DCL VAR(&USER) TYPE(*CHAR) LEN(10) VALUE('USER') WRKSPLF SELECT(&USER) ENDPGM
  • Write a CL program to display the job log for a job.
scss
Copy code
PGM DCL VAR(&JOB) TYPE(*CHAR) LEN(10) VALUE('JOB') DSPJOBLOG JOB(&JOB) ENDPGM
  • Write a CL program to display the system value for QCENTURY.
scss
Copy code
PGM DSPSYSVAL SYSVAL(QCENTURY) ENDPGM
  • Write a CL program to call a RPG program.
scss
Copy code
PGM CALL PGM(MYRPG) ENDPGM
  • Write a CL program to call a CL program with parameters.
scss
Copy code
PGM DCL VAR(&PARAM1) TYPE(*CHAR) LEN(10) VALUE('VALUE1') DCL VAR(&PARAM2) TYPE(*CHAR) LEN(10) VALUE('VALUE2') CALL PGM(MYCLP) PARM(&PARAM1 &PARAM2) ENDPGM
  • Write a CL program to submit a batch job.
scss
Copy code
PGM SBMJOB CMD(CALL PGM(MYRPG)) JOB(MYJOB) ENDPGM
  • Write a CL program to monitor a job and send a message if it ends in error.
scss
Copy code
PGM DCL VAR(&JOB) TYPE(*CHAR) LEN(10) VALUE('MYJOB') DCL VAR(&MSG) TYPE(*CHAR) LEN(50) VALUE('Job ended in error') MONMSG MSGID(CPF0000) EXEC(DO) CHKJOB JOB(&JOB) JOBSTS(*END) IF COND(&SYSTS) EQ 'MSGW' THEN(SNDPGMMSG MSG(&MSG)) ENDDO ENDPGM
  • Write a CL program to display the current date and time.
scss
Copy code
PGM RTVSYSVAL SYSVAL(QDATETIME) VAR(&DATETIME) SNDPGMMSG MSG(&DATETIME) ENDPGM
  • Write a CL program to convert a numeric field to a character field.
scss
Copy code
PGM DCL VAR(&NUM) TYPE(*DEC) LEN(5 0) VALUE(12345) DCL VAR(&CHAR) TYPE(*CHAR) LEN(5) CVTDTAARA DTAARA(&NUM) TOVAR(&CHAR) SNDPGMMSG MSG(&CHAR) ENDPGM
  • Write a CL program to calculate the difference between two dates.
scss
Copy code
PGM DCL VAR(&DATE1) TYPE(*CHAR) LEN(10) VALUE('20220301') DCL VAR(&DATE2) TYPE(*CHAR) LEN(10) VALUE('20220201') DCL VAR(&DAYS) TYPE(*DEC) LEN(5 0) CVTDAT DTAARA(&DATE1) TOVAR(&DATE1) FROMFMT(*ISO) TOFMT(*USA) CVTDAT DTAARA(&DATE2) TOVAR(&DATE2) FROMFMT(*ISO) TOFMT(*USA) CALCDAYS DTAARA(&DATE1) - DTAARA(&DATE2) TOVAR(&DAYS) SNDPGMMSG MSG('Days between dates: ' *CAT &DAYS) ENDPGM
  • Write a CL program to concatenate two character fields.
scss
Copy code
PGM DCL VAR(&FIELD1) TYPE(*CHAR) LEN(10) VALUE('FIELD1') DCL VAR(&FIELD2) TYPE(*CHAR) LEN(10) VALUE('FIELD2') DCL VAR(&RESULT) TYPE(*CHAR) LEN(20) CHGVAR VAR(&RESULT) VALUE(&FIELD1 *CAT &FIELD2) SNDPGMMSG MSG(&RESULT) ENDPGM
  • Write a CL program to check if a character field contains a substring.
scss
Copy code
PGM DCL VAR(&FIELD) TYPE(*CHAR) LEN(10) VALUE('ABCDEFGHIJ') DCL VAR(&SUBSTR) TYPE(*CHAR) LEN(3) VALUE('CDE') CHKSUBST STRING(&FIELD) SUBSTR(&SUBSTR) RTNVAR(&FOUND) IF COND(&FOUND) EQ '1' THEN(SNDPGMMSG MSG('Substring found')) IF COND(&FOUND) EQ '0' THEN(SNDPGMMSG MSG('Substring not found')) ENDPGM
  • Write a CL program to display a message box.
scss
Copy code
PGM DCL VAR(&MSG) TYPE(*CHAR) LEN(50) VALUE('This is a message') DSPMSGD RANGE(*JOB) MSG(&MSG) TYPE(*INFO) ENDPGM
  • Write a CL program to read a database file.
scss
Copy code
PGM DCL VAR(&FILE) TYPE(*CHAR) LEN(10) VALUE('MYFILE') DCL VAR(&KEY) TYPE(*CHAR) LEN(5) VALUE('KEY01') DCL VAR(&FIELD1) TYPE(*CHAR) LEN(10) DCL VAR(&FIELD2) TYPE(*DEC) LEN(5 2) RGZPFM FILE(&FILE) KEY(&KEY) READ FILE(&FILE) KEY(&KEY) MONMSG MSGID(CPF0864) EXEC(GOTO CMDLBL(END)) CHGVAR VAR(&FIELD1) VALUE(&MYFILEFIELDA) CHGVAR VAR(&FIELD2) VALUE(&MYFILEFIELDB) SNDPGMMSG MSG('Field 1: ' *CAT &FIELD1) SNDPGMMSG MSG('Field 2: ' *CAT &FIELD2) ENDREAD ENDPGM
  • Write a CL program to update a database file.
scss
Copy code
PGM DCL VAR(&FILE) TYPE(*CHAR) LEN(10) VALUE('MYFILE') DCL VAR(&KEY) TYPE(*CHAR) LEN(5) VALUE('KEY01') DCL VAR(&FIELD1) TYPE(*CHAR) LEN(10) VALUE('NEWVAL') DCL VAR(&FIELD2) TYPE(*DEC) LEN(5 2) VALUE(99.99) RGZPFM FILE(&FILE) KEY(&KEY) UPDDTA FILE(&FILE) KEY(&KEY) FLDNAM(MYFILEFIELDA) VALUE(&FIELD1) UPDDTA FILE(&FILE) KEY(&KEY) FLDNAM(MYFILEFIELDB) VALUE(&FIELD2) SNDPGMMSG MSG('Record updated') ENDPGM
  • Write a CL program to delete a record from a database file.
scss
Copy code
PGM DCL VAR(&FILE) TYPE(*CHAR) LEN(10) VALUE('MYFILE') DCL VAR(&KEY) TYPE(*CHAR) LEN(5) VALUE('KEY01') RGZPFM FILE(&FILE) KEY(&KEY) DLTF FILE(&FILE) KEY(&KEY) SNDPGMMSG MSG('Record deleted') ENDPGM
  • Write a CL program to sort an array. (Continued)
scss
Copy code
DCL VAR(&N) TYPE(*DEC) LEN(2) VALUE(5) DCL &MYARRAY *CHAR 30 DCL &ELEM *CHAR 10 CHGVAR VAR(&MYARRAY) VALUE('FIVE TWO TEN ONE SIX ') SNDPGMMSG MSG('Before sort: ' *CAT &MYARRAY) DOFOR VAR(&I) FROM(1) TO(&N - 1) DOFOR VAR(&J) FROM(&I + 1) TO(&N) CHGVAR VAR(&ELEM) VALUE(%SST(&MYARRAY (&I - 1) *CHAR 10)) IF COND(&ELEM *LT %SST(&MYARRAY (&J - 1) *CHAR 10)) THEN(DO) CHGVAR VAR(&TEMP) VALUE(%SST(&MYARRAY (&I - 1) *CHAR 10)) CHGVAR %SST(&MYARRAY (&I - 1) *CHAR 10) VALUE(%SST(&MYARRAY (&J - 1) *CHAR 10)) CHGVAR %SST(&MYARRAY (&J - 1) *CHAR 10) VALUE(&TEMP) ENDDO ENDDO ENDDO SNDPGMMSG MSG('After sort: ' *CAT &MYARRAY) ENDPGM
  • Write a CL program to create a multi-dimensional array and display its contents.
scss
Copy code
PGM DCL VAR(&ROWS) TYPE(*DEC) LEN(2) VALUE(3) DCL VAR(&COLS) TYPE(*DEC) LEN(2) VALUE(4) DCL &MYARRAY *DEC (&ROWS &COLS) DCL &I *DEC DCL &J *DEC FOR (&I 1) TO (&ROWS) FOR (&J 1) TO (&COLS) CHGVAR VAR(&MYARRAY &I &J) VALUE(&I * &J) ENDFOR ENDFOR FOR (&I 1) TO (&ROWS) SNDPGMMSG MSG('Row ' *CAT &I *CAT ':') FOR (&J 1) TO (&COLS) SNDPGMMSG MSG('Col ' *CAT &J *CAT ': ' *CAT %CHAR(&MYARRAY &I &J)) ENDFOR ENDDO ENDPGM
  • Write a CL program to read a text file and count the number of lines.
scss
Copy code
PGM DCL VAR(&FILENAME) TYPE(*CHAR) LEN(100) VALUE('/MYFOLDER/MYFILE.TXT') DCL VAR(&LINES) TYPE(*DEC) LEN(5 0) VALUE(0) DCL VAR(&LINETEXT) TYPE(*CHAR) LEN(100) OVRDBF FILE(INPUTFILE) TOFILE(QTEMP/INPFILE) MBR(MYFILE) + OVRSCOPE(*JOB) SHARE(*YES) RCVF DOW NOT %EOF(INPUTFILE) CHGVAR VAR(&LINES) VALUE(&LINES + 1) RCVF ENDDO DLTOVR FILE(INPUTFILE) SNDPGMMSG MSG('Number of lines: ' *CAT %CHAR(&LINES)) ENDPGM
  • Write a CL program to calculate the sum of even numbers from 1 to 100.
scss
Copy code
PGM DCL VAR(&SUM) TYPE(*DEC) LEN(5 0) VALUE(0) DCL VAR(&I) TYPE(*DEC) LEN(3) VALUE(1) DO WHILE COND(&I *LE 100) IF COND(&I *REM 2 = 0) THEN(CHGVAR VAR(&SUM) VALUE(&SUM + &I)) CHGVAR VAR(&I) VALUE(&I + 1) ENDDO SNDPGMMSG MSG('Sum of even numbers from 1 to 100: ' *CAT %CHAR(&SUM)) ENDPGM
  • Write a CL program to display a multiplication table for a given number.
scss
Copy code
PGM DCL VAR(&MULTIPLICAND) TYPE(*DEC) LEN(2) /* Prompt for input */ CHGVAR VAR(&MULTIPLICAND) VALUE(%SST(&SYSIN 1 2)) /* Calculate and display table */ DOFOR VAR(&I) FROM(1) TO(10) SNDPGMMSG MSG(%CHAR(&MULTIPLICAND) *CAT ' x ' *CAT %CHAR(&I) *CAT ' = ' *CAT %CHAR(&MULTIPLICAND * &I)) ENDDO ENDPGM
  • Write a CL program to find the largest element in an array.
scss
Copy code
PGM DCL VAR(&N) TYPE(*DEC) LEN(2) VALUE(5) DCL &MYARRAY *DEC 5 DCL &MAX *DEC /* Initialize array */ CHGVAR VAR(&MYARRAY 1) VALUE(3) CHGVAR VAR(&MYARRAY 2) VALUE(7) CHGVAR VAR(&MYARRAY 3) VALUE(1) CHGVAR VAR(&MYARRAY 4) VALUE(9) CHGVAR VAR(&MYARRAY 5) VALUE(4) /* Find maximum */ CHGVAR VAR(&MAX) VALUE(&MYARRAY 1) DOFOR VAR(&I) FROM(2) TO(&N) IF COND(&MYARRAY &I *GT &MAX) THEN(CHGVAR VAR(&MAX) VALUE(&MYARRAY &I)) ENDDO SNDPGMMSG MSG('Maximum element: ' *CAT %CHAR(&MAX)) ENDPGM
  • Write a CL program to copy a file from one library to another.
scss
Copy code
PGM DCL VAR(&SOURCELIB) TYPE(*CHAR) LEN(10) VALUE('SRCLIB') DCL VAR(&TARGETLIB) TYPE(*CHAR) LEN(10) VALUE('TGTLIB') DCL VAR(&FILENAME) TYPE(*CHAR) LEN(10) VALUE('MYFILE') CPYF FROMFILE(&SOURCELIB/&FILENAME) TOFILE(&TARGETLIB/&FILENAME) SNDPGMMSG MSG('File copied from ' *CAT &SOURCELIB *CAT ' to ' *CAT &TARGETLIB) ENDPGM

    Leave a Comment


  • captcha text