RPG & CL development · Easy

RPG, RPGLE & RPG IV

Read legacy code and write clear modern RPG with appropriate types and interfaces.

Learn this topic first: RPG development: from opcodes to procedures →

8 explained questions · 8 practice MCQs

Questions and answers

1. What do RPG IV, ILE RPG, RPGLE, and SQLRPGLE mean? (Easy)

RPG IV is the language generation commonly associated with ILE RPG. RPGLE is a source-type convention for ILE RPG source; SQLRPGLE identifies source containing embedded SQL that requires the SQL preprocessing/build path. These labels are related but not interchangeable layers.

In a maintenance interview, identify the source format, compiler, SQL build settings, and resulting object. A source member suffix alone does not reveal all runtime characteristics, such as activation group or commitment settings.

2. How does fully free RPG differ from fixed format? (Easy)

Fixed-format RPG assigns meaning to particular columns and specification types. Fully free RPG uses free-form declarations and statements, usually beginning with **FREE. It improves readability and makes structured declarations and procedures easier to review.

Changing layout does not automatically remove global state, the RPG cycle, or poor error handling. Modernization should preserve behavior through tests while introducing explicit interfaces and understandable control flow. Verify compiler support before using a newer language feature.

Example

**FREE
dcl-s total packed(11:2) inz(0);
total += amount;
3. How do packed, zoned, integer, and character fields differ? (Easy)

Packed and zoned decimal represent decimal digits using different storage layouts; integers use binary representation; character fields store text according to encoding rules. Decimal precision and scale affect the valid range and arithmetic results.

Use a domain-appropriate type: money usually needs explicit decimal precision, identifiers may be character even when composed of digits, and counters can be integers. Passing the wrong storage layout to a program can misinterpret bytes even when the displayed values look similar.

4. What are built-in functions used for? (Intermediate)

RPG built-in functions provide operations such as conversion, substring extraction, trimming, lookup, and I/O status checks. Examples include %CHAR, %DEC, %SUBST, %TRIM, %FOUND, and %EOF. Their arguments and results have defined types and boundary behavior.

Do not treat conversion as validation. Validate length and syntax, then handle conversion exceptions and range limits. For status functions, specify the relevant file where supported and inspect the result immediately after the operation of interest.

Example

if %found(Customers);
  displayName = %trim(customerName);
endif;
5. Why do RETURN and setting LR have different effects? (Intermediate)

RETURN transfers control back to the caller. In a traditional RPG main procedure, returning without last-record termination can preserve resources and state for a later call. Setting LR and ending the main procedure performs RPG end-of-program processing, including relevant file cleanup.

Do not apply this simplified rule blindly to every subprocedure or activation group. Explicit cleanup and well-defined initialization are important in long-lived jobs. A program that works once but fails on its second call often exposes retained state assumptions.

6. How do arrays, SORTA, and %LOOKUP work together? (Intermediate)

An array stores multiple elements of a defined type. SORTA orders an array according to supported sort options; %LOOKUP searches for an element and returns a matching index or the no-match result. Ordered-array declarations and search variants have specific requirements.

Track the populated portion rather than treating every allocated element as business data. Check bounds and the returned index before access. When sorting parallel information, use a structured representation or a coordinated sort so related values do not become misaligned.

Example

**FREE
dcl-s names char(20) dim(5);
dcl-s populated int(10) inz(3);
dcl-s found int(10);

names(1) = 'ZARA';
names(2) = 'AMIR';
names(3) = 'MEI';
sorta names;
found = %lookup('MEI' : names : 1 : populated);
if found > 0;
  dsply ('Found at index ' + %char(found));
endif;
7. Why use date types instead of numeric date arithmetic? (Intermediate)

A real date type represents calendar values and supports date-aware operations. Adding 1 to a numeric YYYYMMDD field can produce an invalid date at month or year boundaries. Character formats also require explicit interpretation during conversion.

Validate inbound date format and use supported duration/date operations. Test leap days, month ends, and invalid values. Timestamps and time zones require separate business rules, particularly when IBM i exchanges times with web clients in other regions.

8. How would you safely modernize a large fixed-format program? (Advanced)

Capture representative behavior first: normal cases, invalid inputs, rounding, file updates, and error messages. Convert in small reviewable steps and keep business changes separate from mechanical syntax changes.

Then isolate business operations behind typed procedures, reduce global state, and clarify transaction ownership. Compare outputs and side effects against the old version in a controlled environment. A clean-looking free-form rewrite can still change decimal semantics, indicator flow, or record-lock duration if those are not tested.

Practice checkpoint

  1. 1. What does SQLRPGLE commonly identify?
    1. A job description
    2. Only fixed-format RPG II
    3. A database schema
    4. RPG source with embedded SQL
  2. 2. Which type choice is usually suitable for fixed-scale money?
    1. Packed decimal with explicit precision and scale
    2. An arbitrary text buffer
    3. A job number
    4. A pointer
  3. 3. Does free-form conversion automatically remove global state?
    1. Yes, always
    2. No
    3. Only on Power hardware
    4. Only in QTEMP
  4. 4. Which BIF checks whether CHAIN found a record?
    1. %TRIM
    2. %CHAR
    3. %FOUND
    4. %LEN
  5. 5. A program fails only on its second call in one job. Investigate:
    1. Only source indentation
    2. Only the terminal emulator
    3. Whether the file has a long name
    4. Retained state and cleanup
  6. 6. Which RPG operation searches a sorted array and returns its matching position?
    1. %LOOKUP after SORTA
    2. RETURN after CLEAR
    3. WRITE after SETLL
    4. SCAN after DELETE
  7. 7. Why should date fields use date operations instead of numeric arithmetic?
    1. Date operations handle calendar rules and make intent explicit
    2. Numeric arithmetic is required by the compiler
    3. Date operations only change screen formatting
    4. Numeric arithmetic automatically validates leap years
  8. 8. What is the safest first step when modernizing fixed-format RPG?
    1. Rewrite every procedure in one release
    2. Capture behavior and regression cases before small conversions
    3. Remove all indicators immediately
    4. Change database keys during syntax conversion
Show answer key and explanations

1. D — RPG source with embedded SQL It signals the SQL-aware RPG build path.

2. A — Packed decimal with explicit precision and scale Decimal arithmetic supports an explicit business rounding and range policy.

3. B — No Source layout and program architecture are separate concerns.

4. C — %FOUND Check the relevant file status immediately after the read.

5. D — Retained state and cleanup RETURN and runtime lifecycles can preserve state across calls.

6. A — %LOOKUP after SORTA SORTA establishes order and %LOOKUP returns the index; always validate the no-match result before indexing.

7. A — Date operations handle calendar rules and make intent explicit Typed date values and built-in date functions prevent invalid calendar arithmetic and document the business intent.

8. B — Capture behavior and regression cases before small conversions Behavior capture separates mechanical modernization from accidental business or decimal-semantics changes.

IBM documentation and further reading