PL/SQL Control Structures: IF, CASE, and Loops Essentials Quiz Quiz

Explore foundational concepts of PL/SQL control structures with this quiz, featuring common usage scenarios for IF statements, CASE statements, and various loop types. Strengthen your understanding of logical flow and coding techniques in PL/SQL with key examples and clear explanations.

  1. IF Statement Basic Syntax

    Which of the following is the correct way to start a simple IF statement in PL/SQL to check if the variable 'score' is greater than 50?

    1. IF (score u003E 50) THEN
    2. IF score u003E 50 DO
    3. IF score u003E 50 THEN
    4. IF score u003E 50 {

    Explanation: The correct syntax in PL/SQL to start an IF statement is 'IF score u003E 50 THEN'. Option B is incorrect because 'DO' is not used in PL/SQL IF statements. Option C is wrong as parentheses are not required around the condition in PL/SQL, unlike some other languages. Option D uses curly braces, which are not used in PL/SQL for control structures.

  2. ELSE Clause Purpose

    What is the purpose of the ELSE clause in a PL/SQL IF statement?

    1. It executes when the IF condition is not met
    2. It repeats the IF block in a loop
    3. It checks for multiple conditions
    4. It ends the IF statement

    Explanation: In PL/SQL, the ELSE clause specifies code to run if the IF condition evaluates to FALSE. Option B incorrectly describes END IF, not ELSE. Option C relates to loops, not IF statements. Option D is specific to ELSEIF or CASE statements, not ELSE.

  3. Simple CASE Statement

    In a PL/SQL CASE statement, what does the following code illustrate? CASE grade WHEN 'A' THEN ... WHEN 'B' THEN ... ELSE ... END CASE;

    1. A loop over multiple grades
    2. An error due to missing THEN
    3. A simple CASE comparing the value of 'grade'
    4. An IF statement for 'grade'

    Explanation: This syntax demonstrates a simple CASE statement matching 'grade' to specific values. Option B incorrectly states it's a loop, which it isn't. Option C is inaccurate as the structure is clearly not an IF. Option D is wrong because 'THEN' is present for each case.

  4. EXIT Statement in Loops

    What does the EXIT statement do when placed inside a PL/SQL loop?

    1. It restarts the loop from the beginning
    2. It skips to the next iteration
    3. It immediately exits the loop
    4. It pauses the loop temporarily

    Explanation: Using EXIT in a PL/SQL loop causes the loop to terminate right away, and control continues after the loop. Option B describes a CONTINUE-like behavior, not EXIT. Option C, skipping to the next iteration, is not the function of EXIT. Option D is incorrect since EXIT does not pause loops.

  5. FOR Loop Range

    Which of the following correctly defines a numeric FOR loop in PL/SQL to iterate a variable 'i' from 1 to 5?

    1. FOR i := 1 to 5 LOOP
    2. FOR i IN 1..5 LOOP
    3. FOR (i = 1; i u003C= 5; i++) LOOP
    4. FOR i from 1 to 5 LOOP

    Explanation: PL/SQL uses 'FOR i IN 1..5 LOOP' for a numeric FOR loop. Option B is from C-like languages; Option C incorrectly uses ':=’ and 'to'; Option D uses 'from...to', which is also not PL/SQL syntax.

  6. Difference between WHILE and FOR Loops

    What is a key difference between a WHILE loop and a FOR loop in PL/SQL?

    1. FOR loops can only use string variables
    2. WHILE loops cannot contain IF statements
    3. A WHILE loop checks its condition before each iteration, while a FOR loop runs a fixed number of times
    4. A FOR loop requires a condition to start, while a WHILE loop does not

    Explanation: A WHILE loop evaluates a condition before every iteration, possibly running an unknown number of times, while a FOR loop iterates over a specified range. Option B is wrong; both loops require conditions. Option C is incorrect since any loop can have IF statements. Option D is false as FOR loops use numeric variables.

  7. ELSEIF Usage in IF Statements

    How would you add an additional condition to an IF statement in PL/SQL after the initial condition has failed?

    1. Use ELSEIF with parentheses
    2. Use CASE instead of IF
    3. Place another IF inside ELSE
    4. Use ELSIF for the next condition

    Explanation: In PL/SQL, 'ELSIF' introduces a new condition after the initial IF. Option B can be used but is less readable than ELSIF. Option C is valid for multiple values but not as direct for logical checks. Option D is incorrect as ELSEIF with parentheses is not valid syntax.

  8. Searched CASE Statement

    What distinguishes a searched CASE statement in PL/SQL from a simple CASE statement?

    1. It does not use the ELSE clause
    2. It cannot be used inside a loop
    3. It allows evaluating different Boolean expressions in each WHEN clause
    4. It only matches on a single variable’s value

    Explanation: A searched CASE lets you assess multiple Boolean expressions for each WHEN, like 'WHEN salary u003E 5000'. Option B describes the simple CASE, not searched CASE. The ELSE clause is allowed, so option C is incorrect. Option D is wrong—CASE statements can be used inside loops.

  9. Basic Loop Structure

    Which of the following demonstrates the correct way to start a basic LOOP in PL/SQL?

    1. LOOP
    2. BEGIN LOOP
    3. START LOOP
    4. LOOP BEGIN

    Explanation: You initiate a basic PL/SQL loop with the 'LOOP' keyword. 'BEGIN LOOP' and 'LOOP BEGIN' are not valid in PL/SQL. 'START LOOP' is also not part of PL/SQL syntax; only 'LOOP' is correct.

  10. Ending an IF Statement

    How do you properly end an IF statement in PL/SQL?

    1. ENDIF
    2. IF END;
    3. END IF;
    4. END;

    Explanation: The correct way to end an IF statement in PL/SQL is with 'END IF;'. 'ENDIF' is invalid in PL/SQL, while 'END;' is used for ending blocks, not specific statements. 'IF END;' is not valid syntax.