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.
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?
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.
What is the purpose of the ELSE clause in a PL/SQL 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.
In a PL/SQL CASE statement, what does the following code illustrate? CASE grade WHEN 'A' THEN ... WHEN 'B' THEN ... ELSE ... END CASE;
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.
What does the EXIT statement do when placed inside a PL/SQL loop?
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.
Which of the following correctly defines a numeric FOR loop in PL/SQL to iterate a variable 'i' from 1 to 5?
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.
What is a key difference between a WHILE loop and a FOR loop in PL/SQL?
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.
How would you add an additional condition to an IF statement in PL/SQL after the initial condition has failed?
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.
What distinguishes a searched CASE statement in PL/SQL from a simple CASE statement?
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.
Which of the following demonstrates the correct way to start a basic LOOP in PL/SQL?
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.
How do you properly end an IF statement in PL/SQL?
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.