PL/I Interview Questions (21 - 30)
21. What is the difference between CALL BY VALUE and CALL BY REFERENCE in PL/I?
- CALL BY VALUE: The called procedure receives a copy of the argument's value. Changes made to the parameter within the procedure do not affect the original variable.
- CALL BY REFERENCE: The called procedure receives a reference to the original argument. Changes made to the parameter within the procedure affect the original variable.
22. What is the use of the DEFINED attribute in PL/I?
The DEFINED attribute in PL/I is used to declare a variable that shares storage with another variable or a portion of it. This is similar to the REDEFINES clause in COBOL.
DCL VAR1 CHAR(10);
DCL VAR2 CHAR(5) DEFINED VAR1;
In this example, VAR2 occupies the same storage as the first 5 characters of VAR1.
23. How do you perform string concatenation in PL/I?
String concatenation in PL/I is performed using the || operator.
DCL FIRST_NAME CHAR(10) INIT('John');
DCL LAST_NAME CHAR(10) INIT('Doe');
DCL FULL_NAME CHAR(20);
FULL_NAME = FIRST_NAME || ' ' || LAST_NAME;
24. What is the purpose of the TRANSLATE function in PL/I?
The TRANSLATE function in PL/I is used to replace characters in a string based on a translation table. It's useful for tasks like converting lowercase letters to uppercase.
DCL INPUT_STRING CHAR(10) INIT('abc');
DCL OUTPUT_STRING CHAR(10);
OUTPUT_STRING = TRANSLATE(INPUT_STRING, 'ABC', 'abc');
In this example, OUTPUT_STRING will contain 'ABC'.
25. How do you declare and use files in PL/I?
Files in PL/I are declared using the FILE attribute.
DCL FILEA FILE INPUT;
To open and close files:
OPEN FILE(FILEA) TITLE('DDNAME');
/* File operations */
CLOSE FILE(FILEA);
26. What is the difference between stream I/O and record I/O in PL/I?
- Stream I/O: Handles data as a continuous stream of characters. Used for reading and writing text files where data is not organized into fixed-length records.
- Record I/O: Handles data as discrete records, each with a defined structure. Suitable for files with fixed or variable-length records.
27. How do you handle end-of-file conditions in PL/I?
In PL/I, when you read a file using READ FILE, the program can detect the end of the file (EOF) using an ON condition. For example -
ON ENDFILE(INPUT_FILE) BEGIN;
PUT SKIP LIST('End of file reached.');
END;
This will catch the EOF condition when no more data is available to read.
28. What is the purpose of the ONCODE built-in function in PL/I?
The ONCODE function returns the code number of the current error or exception. For example -
ON ERROR BEGIN;
PUT SKIP LIST('Error code is:', ONCODE());
END;
It helps you identify what kind of error occurred during program execution.
29. What is a FETCHABLE procedure in PL/I?
A FETCHABLE procedure is one that is not loaded into memory until it is needed (when you call it using FETCH). It uses to save memory – especially in large programs with many optional parts. For example -
CALL 'MYPROC'; /* Will fetch and run MYPROC from an external module */
30. What is the difference between STATIC and AUTOMATIC storage classes in PL/I?
- STATIC: Variable keeps its value between calls and exists throughout the program's life.
- AUTOMATIC: Variable is created when the block starts and destroyed when the block ends.
DCL A FIXED BIN STATIC;
DCL B FIXED BIN AUTOMATIC;
Use STATIC for values you want to keep between uses, like counters.