PL SQL MCQ Questions. Explore PL/SQL: its meaning, advantages over basic SQL, and real uses. Understand compilation, block structure (DECLARE, BEGIN, EXCEPTION), data types (NUMBER, VARCHAR2, BOOLEAN, CLOB), operators, and control structures (IF, CASE, LOOP). Learn about cursors, procedures, functions, exceptions, packages, triggers, and collections (VARRAY, nested table, associative array), plus records.
PL SQL MCQ Questions Topics:
- Introduction to PL/SQL (MCQ 1–5)
- PL/SQL Block Structure (MCQ 6–7)
- Lexical Units and Data Types (MCQ 8–18)
- Operators in PL/SQL (MCQ 19–25)
- Conditional Statements in PL/SQL (MCQ 26–30)
- Loops in PL/SQL (MCQ 31–38)
- Introduction to Cursors (MCQ 39–42)
- Explicit Cursors in PL/SQL (MCQ 43–46)
- Procedures in PL/SQL (MCQ 47–50)
- Functions in PL/SQL (MCQ 51–56)
- Exception Handling in PL/SQL (MCQ 57–64)
- Packages in PL/SQL (MCQ 65–70)
- Triggers in PL/SQL (MCQ 71–80)
- Nested Tables in PL/SQL (MCQ 81–84)
- VARRAYs in PL/SQL (MCQ 85–88)
- Associative Arrays (Index-by Tables) in PL/SQL (MCQ 89–94)
- Records in PL/SQL (MCQ 95–100)
PL SQL MCQ Questions – Mock Online Test
Question 1: PL/SQL is an extension of SQL. What does PL/SQL stand for?
Show Explanation
Correct Answer: B. Procedural Language/Structured Query Language. PL/SQL stands for Procedural Language extension to the Structured Query Language.
Question 2: Which of the following is NOT a benefit of using PL/SQL over standard SQL?
Show Explanation
Correct Answer: C. Increased code complexity. PL/SQL aims to reduce code complexity by allowing for modularization and code reuse, although poorly written PL/SQL can be complex.
Question 3: PL/SQL is commonly used in which of the following real-world applications?
Show Explanation
Correct Answer: D. All of the above. PL/SQL is versatile and used in various applications involving Oracle databases, including web backends, data warehousing processes, and database administration tasks.
Question 4: How does PL/SQL interact with the Oracle database?
Show Explanation
Correct Answer: B. It sends SQL statements to the SQL engine for execution. PL/SQL integrates procedural logic with SQL, sending SQL commands to the database’s SQL engine.
Question 5: What is the correct order of the PL/SQL compilation and execution process?
Show Explanation
Correct Answer: B. Compilation, Execution. PL/SQL code is first compiled into bytecode, and then the bytecode is executed by the PL/SQL engine.
Question 6: Which section of a PL/SQL block is optional?
Show Explanation
Correct Answer: D. Both A and C. Both the DECLARE and EXCEPTION sections are optional in a PL/SQL block. Only the BEGIN and END keywords defining the executable section are mandatory.
Question 7: What is the purpose of the EXCEPTION section in a PL/SQL block?
Show Explanation
Correct Answer: C. To handle runtime errors. The EXCEPTION section contains handlers for errors that occur during the execution of the BEGIN section.
Question 8: Which of the following is NOT a lexical unit in PL/SQL?
Show Explanation
Correct Answer: C. Variable. Variables are program constructs used to store data, but they are composed of lexical units (like identifiers). Lexical units are the fundamental syntax elements like identifiers, keywords, literals, delimiters, and comments.
Question 9: Which data type would you use to store a person’s age in PL/SQL?
Show Explanation
Correct Answer: B. NUMBER. The NUMBER data type is suitable for storing numeric values like age.
Question 10: What is the maximum length of a VARCHAR2 variable in PL/SQL?
Show Explanation
Correct Answer: D. 32767 characters. Within PL/SQL, a VARCHAR2 variable can hold up to 32,767 bytes (or characters, depending on the database character set and configuration).
Question 11: Which data type is used to store logical values in PL/SQL?
Show Explanation
Correct Answer: C. BOOLEAN. The BOOLEAN data type stores logical values: TRUE, FALSE, or NULL.
Question 12: What is a record in PL/SQL?
Show Explanation
Correct Answer: C. A structured data type that groups together related variables of different data types. Records allow you to treat related but potentially differently-typed data as a single unit.
Question 13: How do you access individual elements within a PL/SQL record?
Show Explanation
Correct Answer: B. Using dot notation (.). You access a field within a record using the format record_name.field_name
.
Question 14: What is the purpose of indexing a table in PL/SQL?
Show Explanation
Correct Answer: A. To improve the performance of queries that access the table. Indexing is primarily a database concept (used within SQL executed by PL/SQL) to speed up data retrieval.
Question 15: Which of the following is true about VARRAYS in PL/SQL?
Show Explanation
Correct Answer: B. Their size is fixed at the time of declaration. VARRAYs (Variable-size arrays) have a maximum size defined at creation and cannot exceed it.
Question 16: Which data type would you use to store a large text file in PL/SQL?
Show Explanation
Correct Answer: C. CLOB. CLOB (Character Large Object) is designed to store large blocks of character data.
Question 17: What is the purpose of the DEFAULT
keyword when declaring a variable in PL/SQL?
Show Explanation
Correct Answer: A. To assign a default value to the variable. If no value is explicitly assigned, the variable will take the specified default value upon declaration.
Question 18: How do you enforce a NOT NULL constraint on a PL/SQL variable?
Show Explanation
Correct Answer: A. By using the NOT NULL
keyword in the variable declaration. You can specify NOT NULL
to ensure the variable must always hold a value.
Question 19: What is the result of the following PL/SQL expression: 10 + 5 * 2
?
Show Explanation
Correct Answer: A. 20. Multiplication (*
) has higher precedence than addition (+
), so 5 * 2
is evaluated first (10), then 10 + 10
equals 20.
Question 20: Which arithmetic operator is used for exponentiation in PL/SQL?
Show Explanation
Correct Answer: B. **. The double asterisk (**
) is used for raising a number to a power.
Question 21: Which relational operator means “not equal to” in PL/SQL?
Show Explanation
Correct Answer: B. !=. Both !=
and <>
are valid operators for “not equal to” in PL/SQL.
Question 22: What is the result of the following PL/SQL expression if x = 5
and y = 10
: x < 10 AND y > 5
?
Show Explanation
Correct Answer: A. TRUE. x < 10
(5 < 10) is TRUE. y > 5
(10 > 5) is TRUE. TRUE AND TRUE
evaluates to TRUE.
Question 23: Which operator is used to concatenate strings in PL/SQL?
Show Explanation
Correct Answer: C. ||. The double pipe (||
) operator is used for string concatenation.
Question 24: In the absence of parentheses, which operation is performed first in the following PL/SQL expression: NOT x > 5 AND y = 10
?
Show Explanation
Correct Answer: D. NOT
. The NOT
operator has the highest precedence among the logical and relational operators shown.
Question 25: What is the result of the following PL/SQL expression: 5 + 3 * 2 - 4 / 2
?
Show Explanation
Correct Answer: A. 9. Following standard operator precedence (Multiplication/Division before Addition/Subtraction): 3 * 2 = 6
, 4 / 2 = 2
. The expression becomes 5 + 6 - 2
. Then, 5 + 6 = 11
, and 11 - 2 = 9
.
Question 26: Which conditional statement allows you to test multiple conditions in PL/SQL?
Show Explanation
Correct Answer: D. All of the above. IF-THEN-ELSIF-ELSE
allows sequential condition testing, and CASE
statements (both simple and searched) also evaluate multiple conditions.
Question 27: What is the purpose of the ELSIF
clause in an IF statement?
Show Explanation
Correct Answer: A. To execute a block of code if the initial IF condition and all preceding ELSIF conditions are false. It allows testing additional conditions if the previous ones were not met.
Question 28: What is the difference between a simple CASE statement and a searched CASE statement in PL/SQL?
Show Explanation
Correct Answer: A. A simple CASE statement compares an expression to specific values, while a searched CASE statement evaluates a series of boolean conditions. The simple CASE uses a selector, while the searched CASE evaluates independent WHEN clauses.
Question 29: Which keyword is used to terminate a CASE statement in PL/SQL?
Show Explanation
Correct Answer: A. END CASE. Every CASE statement must conclude with END CASE;
.
Question 30: How many ELSE
clauses can an IF statement have?
Show Explanation
Correct Answer: A. Only one. An IF statement (including IF-THEN-ELSIF
) can have at most one ELSE
clause, which executes if none of the preceding conditions are TRUE.
Question 31: Which loop in PL/SQL executes a block of code at least once, even if the loop condition is initially false?
Show Explanation
Correct Answer: A. LOOP. The basic LOOP...EXIT WHEN...END LOOP;
structure checks the exit condition after executing the loop body at least once.
Question 32: Which loop is best suited for iterating over a specific range of numbers in PL/SQL?
Show Explanation
Correct Answer: C. FOR loop. The FOR loop_counter IN lower_bound..upper_bound LOOP
syntax is specifically designed for iterating over a numeric range.
Question 33: What is the purpose of the EXIT
statement in a loop?
Show Explanation
Correct Answer: A. To immediately terminate the loop. EXIT
(often used with WHEN
) causes control to pass to the statement immediately following the END LOOP
.
Question 34: What is the purpose of the CONTINUE
statement in a loop?
Show Explanation
Correct Answer: B. To skip the current iteration and proceed to the next. CONTINUE
(often used with WHEN
) bypasses the remaining statements in the current loop iteration and starts the next one.
Question 35: What is a nested loop in PL/SQL?
Show Explanation
Correct Answer: B. A loop that contains another loop within its code block. This allows for iterating through combinations or multi-dimensional structures.
Question 36: Which type of FOR loop in PL/SQL iterates over the rows returned by a database query?
Show Explanation
Correct Answer: B. Cursor FOR loop. The FOR record_name IN cursor_name LOOP
syntax implicitly opens, fetches from, and closes a cursor.
Question 37: What happens if you omit the WHEN
condition in a EXIT WHEN
statement within a loop?
Show Explanation
Correct Answer: C. This will result in a syntax error. The EXIT WHEN
statement requires a boolean condition to determine when to exit.
Question 38: Which of the following is NOT a valid loop control statement in PL/SQL?
Show Explanation
Correct Answer: C. GOTO. While PL/SQL has a GOTO
statement for transferring control to a labeled block, it’s generally discouraged and not considered a standard loop control statement like EXIT or CONTINUE.
Question 39: What is a cursor in PL/SQL?
Show Explanation
Correct Answer: B. A mechanism for retrieving and processing multiple rows from a query. Cursors provide a way to handle the result set of a SELECT statement, usually row by row.
Question 40: Why are cursors used in PL/SQL?
Show Explanation
Correct Answer: D. To process data one row at a time. When procedural logic needs to be applied individually to rows returned by a query, cursors are necessary.
Question 41: What is an implicit cursor in PL/SQL?
Show Explanation
Correct Answer: B. A cursor that is automatically created by PL/SQL for DML statements. PL/SQL uses implicit cursors (like SQL%FOUND
, SQL%ROWCOUNT
) automatically for INSERT, UPDATE, DELETE, and SELECT INTO statements.
Question 42: Which cursor attribute indicates the number of rows affected by a DML statement?
Show Explanation
Correct Answer: C. %ROWCOUNT
. For implicit cursors (SQL%), %ROWCOUNT
gives the number of rows affected by the last DML statement. For explicit cursors, it gives the number of rows fetched so far.
Question 43: What is the first step in working with an explicit cursor in PL/SQL?
Show Explanation
Correct Answer: C. Declaring the cursor. You must first declare the cursor in the DECLARE section, associating it with a SELECT statement.
Question 44: Which clause is used to pass values to a cursor when it is opened?
Show Explanation
Correct Answer: A. USING
. If the cursor declaration includes parameters, you pass arguments using the USING
clause within the OPEN
statement.
Question 45: What is the purpose of the Workspace
statement in relation to cursors?
Show Explanation
Correct Answer: C. To retrieve data from the cursor. The Workspace
statement retrieves the next row from the cursor’s active set into specified variables or a record.
Question 46: What happens if you try to fetch data from a cursor that is closed?
Show Explanation
Correct Answer: A. An exception is raised. Attempting to FETCH from a closed cursor typically raises an INVALID_CURSOR
exception.
Question 47: What is the main purpose of procedures in PL/SQL?
Show Explanation
Correct Answer: C. To modularize code and improve reusability. Procedures encapsulate a sequence of actions, making code organized, reusable, and easier to maintain.
Question 48: Which keyword is used to specify that a procedure parameter is an output parameter?
Show Explanation
Correct Answer: B. OUT
. An OUT
parameter allows the procedure to return a value back to the calling environment through that parameter.
Question 49: What is the scope of a local variable declared within a procedure?
Show Explanation
Correct Answer: C. The procedure in which it is declared. Local variables exist only within the execution scope of the procedure (or block) where they are declared.
Question 50: What is the main difference between a procedure and a function in PL/SQL?
Show Explanation
Correct Answer: B. A function must return a value, while a procedure does not have to. Functions are designed to compute and return a single value via the RETURN
keyword.
Question 51: Which keyword is used to return a value from a function in PL/SQL?
Show Explanation
Correct Answer: B. RETURN
. The RETURN
statement specifies the value that the function sends back to the caller.
Question 52: Can a function have OUT parameters?
Show Explanation
Correct Answer: B. No, a function cannot have OUT parameters. Functions return values via the RETURN
clause. While technically possible in some older versions or specific contexts with workarounds, standard practice and design dictate functions use RETURN
, not OUT
parameters.
Question 53: How do you call a procedure within another PL/SQL block?
Show Explanation
Correct Answer: B. By using the procedure’s name followed by a semicolon. For example: procedure_name(parameter1, parameter2);
.
Question 54: How do you call a function within a SQL statement?
Show Explanation
Correct Answer: B. By using the function’s name in the SELECT
list. Functions designed to be called from SQL can be used like built-in SQL functions, often in the SELECT list or WHERE clause, e.g., SELECT package_name.function_name(column) FROM table;
.
Question 55: What happens if you try to call a procedure from within a function?
Show Explanation
Correct Answer: A. It is perfectly acceptable to call a procedure from within a function. However, if the function is intended to be called from SQL, the called procedure must not violate purity rules (e.g., performing DML without autonomy).
Question 56: When calling a procedure with IN OUT parameters, what must be provided in the call statement?
Show Explanation
Correct Answer: C. A variable. An IN OUT
parameter needs a variable in the calling code to both receive the initial value and accept the potentially modified value back from the procedure.
Question 57: What is the NO_DATA_FOUND
exception in PL/SQL?
Show Explanation
Correct Answer: A. An exception raised when a SELECT statement returns no rows. Specifically, when a SELECT INTO
statement finds no rows to populate the target variables.
Question 58: Which predefined exception is raised when a DML statement attempts to modify more than one row when only one row is expected?
Show Explanation
Correct Answer: B. TOO_MANY_ROWS
. This is raised by a SELECT INTO
statement that returns more than one row.
Question 59: How do you declare a user-defined exception in PL/SQL?
Show Explanation
Correct Answer: A. Using the EXCEPTION
keyword in the DECLARE
section. You declare it like exception_name EXCEPTION;
.
Question 60: Which keyword is used to explicitly raise an exception in PL/SQL?
Show Explanation
Correct Answer: B. RAISE
. The RAISE exception_name;
statement is used to signal an error condition.
Question 61: What is the purpose of user-defined exceptions in PL/SQL?
Show Explanation
Correct Answer: B. To create custom error conditions specific to your application logic. They allow you to signal specific business rule violations or application-specific errors.
Question 62: Which clause is used to catch all unhandled exceptions in an exception handling block?
Show Explanation
Correct Answer: A. WHEN OTHERS
. The WHEN OTHERS THEN
clause acts as a catch-all for any exception not explicitly handled by preceding WHEN
clauses in the same block.
Question 63: What does it mean to propagate an exception in PL/SQL?
Show Explanation
Correct Answer: B. To re-raise the exception in a higher-level block. If an exception handler decides it cannot fully resolve the error, it can use RAISE;
to pass the same exception up the call stack.
Question 64: What happens if an exception is raised in a PL/SQL block and is not handled by an exception handler?
Show Explanation
Correct Answer: B. The program terminates with an error message. The unhandled exception propagates out of the PL/SQL block, usually resulting in an error reported to the calling application or user.
Question 65: What is a package in PL/SQL?
Show Explanation
Correct Answer: A. A collection of related procedures, functions, types, cursors, variables, and other PL/SQL constructs, grouped together as a logical unit.
Question 66: What are the benefits of using packages in PL/SQL?
Show Explanation
Correct Answer: D. All of the above. Packages offer modularity, reusability, information hiding (public/private), potential performance gains (loading into memory), and easier dependency management.
Question 67: What is the purpose of the package specification in PL/SQL?
Show Explanation
Correct Answer: C. To declare the public interface of the package. The specification acts like a header file, declaring the elements (procedures, functions, types, variables) that are accessible from outside the package.
Question 68: What is the purpose of the package body in PL/SQL?
Show Explanation
Correct Answer: B. To provide the implementation for the procedures and functions declared in the package specification. It contains the actual code for the public subprograms and can also contain private declarations and implementations.
Question 69: How do you access a procedure or function within a package in PL/SQL?
Show Explanation
Correct Answer: A. By using the package name followed by the procedure or function name, separated by a dot (.). For example: package_name.procedure_name(parameters);
.
Question 70: What happens if you try to access a private element of a package from outside the package?
Show Explanation
Correct Answer: B. It will result in a compilation error. Private elements declared within the package body are not visible and cannot be accessed directly from outside the package.
Question 71: What is a trigger in PL/SQL?
Show Explanation
Correct Answer: C. A block of code that is automatically executed in response to a database event. These events are typically DML operations (INSERT, UPDATE, DELETE) on a specific table, or system events.
Question 72: What are some common use cases for triggers in PL/SQL?
Show Explanation
Correct Answer: D. All of the above. Triggers are used for enforcing complex business rules, auditing modifications, maintaining data consistency, logging events, and more.
Question 73: Which type of trigger is fired in response to DML statements (INSERT, UPDATE, DELETE)?
Show Explanation
Correct Answer: A. DML trigger. These triggers are specifically associated with data manipulation language operations on tables or views.
Question 74: What is the purpose of an “instead of” trigger in PL/SQL?
Show Explanation
Correct Answer: A. To replace the default action of a DML statement. They are typically used on non-updatable views to provide the logic for how DML operations should affect the underlying base tables.
Question 75: Which keyword specifies that a trigger should fire before the triggering event?
Show Explanation
Correct Answer: A. BEFORE
. A BEFORE
trigger executes before the triggering DML statement starts operating on the data.
Question 76: What does the FOR EACH ROW
clause indicate in a trigger definition?
Show Explanation
Correct Answer: B. The trigger will fire once for each row affected by the DML statement. This is known as a row-level trigger, allowing access to :OLD and :NEW values.
Question 77: Which of the following is NOT a valid trigger type in PL/SQL?
Show Explanation
Correct Answer: D. ON COMMIT
. While database actions can occur upon commit (like materialized view refreshes), ON COMMIT
is not a direct trigger definition type in the same way as BEFORE/AFTER/INSTEAD OF
DML or system event triggers.
Question 78: What does the :OLD
pseudo-record refer to in a trigger?
Show Explanation
Correct Answer: B. The original values of the row before the DML statement. Available in UPDATE
and DELETE
triggers fired FOR EACH ROW
.
Question 79: What does the :NEW
pseudo-record refer to in a trigger?
Show Explanation
Correct Answer: A. The new values of the row after the DML statement. Available in INSERT
and UPDATE
triggers fired FOR EACH ROW
.
Question 80: Can a trigger call a stored procedure?
Show Explanation
Correct Answer: B. Yes, triggers can call stored procedures. This is a common practice to encapsulate complex logic invoked by the trigger.
Question 81: What is a nested table in PL/SQL?
Show Explanation
Correct Answer: B. A collection that can store a variable number of elements of the same data type. Nested tables are unbounded collections (no predefined maximum size) and can be sparse.
Question 82: How do you declare a nested table in PL/SQL?
Show Explanation
Correct Answer: A. Using the TABLE OF
clause. The syntax is TYPE type_name IS TABLE OF element_type;
.
Question 83: What is the main difference between a nested table and a VARRAY in PL/SQL?
Show Explanation
Correct Answer: B. A nested table can be resized dynamically (unbounded), while a VARRAY has a fixed maximum size. Nested tables are stored as separate database objects if used as table columns, while VARRAYs can be stored inline.
Question 84: How do you access an element in a nested table?
Show Explanation
Correct Answer: B. Using array indices enclosed in parentheses. For example: nested_table_variable(index)
.
Question 85: What is a VARRAY in PL/SQL?
Show Explanation
Correct Answer: B. A collection that can store a fixed maximum number of elements of the same data type. The maximum size is specified during type declaration.
Question 86: How do you declare a VARRAY in PL/SQL?
Show Explanation
Correct Answer: B. Using the VARRAY
keyword followed by the size and data type of the elements. The syntax is TYPE type_name IS VARRAY(max_size) OF element_type;
.
Question 87: What happens if you try to add more elements to a VARRAY than its declared size?
Show Explanation
Correct Answer: B. A runtime exception is raised. Attempting to access or assign an element beyond the defined limit typically raises a SUBSCRIPT_BEYOND_COUNT
exception.
Question 88: How do you determine the number of elements currently in a VARRAY?
Show Explanation
Correct Answer: A. Using the COUNT
attribute. varray_variable.COUNT
returns the current number of elements in the VARRAY. LIMIT
returns the maximum declared size.
Question 89: What is an associative array (index-by table) in PL/SQL?
Show Explanation
Correct Answer: B. A collection that uses strings or numbers (usually VARCHAR2 or PLS_INTEGER) as keys to access its elements. They are similar to hash maps or dictionaries in other languages.
Question 90: What are some advantages of using associative arrays in PL/SQL?
Show Explanation
Correct Answer: D. All of the above. They provide fast key-based lookup and are very flexible for mapping keys to values.
Question 91: Which keyword is used to declare an associative array in PL/SQL?
Show Explanation
Correct Answer: C. INDEX BY
. The syntax is TYPE type_name IS TABLE OF element_type INDEX BY key_type;
(e.g., INDEX BY PLS_INTEGER
or INDEX BY VARCHAR2(100)
).
Question 92: What types of keys can be used with associative arrays?
Show Explanation
Correct Answer: C. Both numeric (PLS_INTEGER, BINARY_INTEGER) and string (VARCHAR2, STRING) values. The key type is specified in the INDEX BY
clause.
Question 93: How do you add an element to an associative array?
Show Explanation
Correct Answer: B. By assigning a value to an element using its key. For example: assoc_array_variable(key_value) := element_value;
.
Question 94: Which predefined exception is raised when you try to access an element in an associative array that does not exist?
Show Explanation
Correct Answer: A. NO_DATA_FOUND
. When referencing a key that has not been assigned a value in an associative array, NO_DATA_FOUND
is raised. (SUBSCRIPT_BEYOND_COUNT
is typically for nested tables/VARRAYs when using an index outside the populated range).
Question 95: What is a record in PL/SQL?
Show Explanation
Correct Answer: B. A composite data type that groups together related variables (fields) of potentially different data types. Records represent a structure.
Question 96: How do you declare a record in PL/SQL?
Show Explanation
Correct Answer: C. Using the RECORD
keyword followed by the field definitions. Syntax: TYPE record_type_name IS RECORD (field1 datatype1, field2 datatype2, ...);
.
Question 97: How do you access individual fields within a record?
Show Explanation
Correct Answer: B. Using dot notation (.). You reference a field using record_variable_name.field_name
.
Question 98: Can a record contain another record as a field?
Show Explanation
Correct Answer: B. Yes, records can be nested to create complex data structures. A field within a record can be of another record type.
Question 99: What is the purpose of the %ROWTYPE
attribute in PL/SQL?
Show Explanation
Correct Answer: A. To declare a record with the same structure as a row in a database table or cursor. This ensures the record has fields matching the table/cursor columns.
Question 100: How do you assign values to the fields of a record?
Show Explanation
Correct Answer: D. All of the above. You can assign field by field (record.field := value;
), populate the entire record from a query (SELECT ... INTO record FROM ...;
), or assign a whole record variable to another (record1 := record2;
).