PL/SQL Questions and Answers – PL/SQL Conditions

This set of PL/SQL Multiple Choice Questions & Answers (MCQs) focuses on “PL/SQL Conditions”.

1. Which of the following part of the IF-THEN statement gets executed if the condition is false or NULL?
a) IF
b) THEN
c) ELSE
d) IF statement does nothing
View Answer

Answer: d
Explanation: In IF-THEN statement, if the given condition evaluates to true, then the block of code inside the if statement will be executed. If the expression evaluates to false, then the first set of code after the end of the if statement (IF statement does nothing) will be executed.

2. Consider the PL/SQL code below and select the appropriate output –

DECLARE 
     n NUMBER(3) := 100; 
BEGIN 
     n:= 100; 
     IF( n<50 ) THEN   
         dbms_output.put_line('n is less than 50 ' ); 
     END IF; 
         dbms_output.put_line('value of n is : ' || n); 
END; 
/

a)

advertisement
advertisement
n IS less than 50

b)

n IS less than 50
VALUE OF n IS : 100 
PL/SQL PROCEDURE successfully completed.

c)

advertisement
VALUE OF n IS : 100  
PL/SQL PROCEDURE successfully completed.

d)

advertisement
n IS less than 50
VALUE OF n IS : 100
View Answer
Answer: c
Explanation: The output of the given PL/SQL code will be –

VALUE OF n IS : 100

PL/SQL procedure successfully completed.
Since, the given condition is false so, the IF-THEN statement will be not executed.

3. Which of the following part of the IF-THEN-ELSE statement gets executed if the condition is false or NULL?
a) IF
b) THEN
c) ELSE
d) END IF
View Answer

Answer: c
Explanation: In IF-THEN-ELSE statement, if the given condition evaluates to true, then the block of code inside the if statement will be executed. If the expression evaluates to false, then the block of code inside the else statement will be executed.

4. Consider the PL/SQL code below and select the appropriate output.

DECLARE 
     n NUMBER(3) := 100; 
BEGIN 
     n:= 100; 
     IF( n < 50 ) THEN   
         dbms_output.put_line('n is less than 50 ' ); 
     ELSE
         dbms.output.put_line(‘n IS greater than 50);
     END IF; 
         dbms_output.put_line('value of n is : ' || n); 
END; 
/

a)

n IS less than 50 
n IS greater than 50

b)

n IS greater than 50 
VALUE OF n IS : 100 
PL/SQL PROCEDURE successfully completed.

c)

VALUE OF n IS : 100  
PL/SQL PROCEDURE successfully completed.

d)

n IS less than 50 
VALUE OF n IS : 100
View Answer
Answer: b
Explanation: The output of the given PL/SQL code will be –

n IS greater than 50 
VALUE OF n IS : 100

PL/SQL procedure successfully completed.
Since, the given condition is false so, the ELSE statement will be executed.

5. How many ELSIF’s can an IF-THEN-ELSIF statement contain in PL/SQL?
a) 1
b) 5
c) 100
d) No specified limit
View Answer

Answer: d
Explanation: An IF-THEN-ELSIF statement can have zero to many numbers of ELSIF’s but they must come before the ELSE part.

6. Consider the PL/SQL code below and select the appropriate output.

DECLARE 
     n NUMBER(2) := 40; 
BEGIN
     IF( n = 10 ) THEN   
         dbms_output.put_line('Value of n is 10 ' ); 
     ELSE IF ( n = 20 ) THEN 
         dbms_output.put_line('Value of n is 20' ); 
     ELSE IF ( n = 30 ) THEN 
         dbms_output.put_line('Value of n is 30' );
     ELSE IF ( n = 40 ) THEN 
         dbms_output.put_line('Value of n is 40' );
     ELSE
         dbms.output.put_line(VALUE OF n NOT known’);
     END IF; 
         dbms_output.put_line('value of n is : ' || n); 
END; 
/

a)

VALUE OF n IS 40 
VALUE OF n IS : 40
PL/SQL PROCEDURE successfully completed.

b)

VALUE OF n NOT known
VALUE OF n IS : 40 
PL/SQL PROCEDURE successfully completed.

c)

VALUE OF n IS 10  
PL/SQL PROCEDURE successfully completed.

d)

VALUE OF n IS 40 
VALUE OF n IS : 40
View Answer
Answer: a
Explanation: The output of the given PL/SQL code will be –

VALUE OF n IS 40 
VALUE OF n IS : 40

PL/SQL procedure successfully completed.
Since, the given condition is matched by the last ELSIF so, the corresponding ELSIF statement will be executed. Also, the statement after the END IF will be executed.

7. What is the expression called which is used by the CASE statement to select one statement to execute from the given statements.
a) Boolean Expression
b) Selector
c) Condition
d) Relation
View Answer

Answer: b
Explanation: The CASE statement uses a selector rather than multiple Boolean expressions. A selector is an expression, the value of which is used to select one of several alternatives.

8. Which of the following is the correct syntax for the CASE statement?
a)

CASE selector 
   IF 'value1' THEN Statement1; 
   IF 'value2' THEN Statement2; 
   IF 'value3' THEN Statement3; 
   ... 
   ELSE Statementn;   
END CASE;

b)

CASE selector 
   WHEN 'value1' Statement1; 
   WHEN 'value2' Statement2; 
   WHEN 'value3' Statement3; 
   ... 
   ELSE Statementn;   
END CASE;

c)

CASE selector 
   WHEN 'value1' THEN Statement1; 
   WHEN 'value2' THEN Statement2; 
   WHEN 'value3' THEN Statement3; 
   ... 
   ELSE Statementn;   
END CASE;

d)

CASE selector 
   'value1' THEN Statement1; 
   'value2' THEN Statement2; 
   'value3' THEN Statement3; 
   ... 
   ELSE Statementn;   
END CASE;
View Answer
Answer: c
Explanation: The correct syntax for the CASE statement is-

CASE selector 
   WHEN 'value1' THEN Statement1; 
   WHEN 'value2' THEN Statement2; 
   WHEN 'value3' THEN Statement3; 
   ... 
   ELSE Statementn;   
END CASE;

The WHEN and THEN keywords are used with the CASE statement.

9. What is the CASE statement called when it has no selector and the WHEN clause of the statement contain search conditions.
a) Un-CASE statement
b) Conditional CASE statement
c) Unsearched CASE statement
d) Searched CASE statement
View Answer

Answer: d
Explanation: The Searched CASE statement has no selector and the WHEN clauses of the statement contain search conditions that give Boolean values which helps the statement to decide which of the given statement is to be printed.

10. We can use nested IF-ELSE statements in PL/SQL.
a) True
b) False
View Answer

Answer: a
Explanation: Nested IF-ELSE statements can be used in PL/SQL. Nested IF-ELSE statement means using one IF or ELSE IF statement inside another IF or ELSE IF statement/statements.

Sanfoundry Global Education & Learning Series – PL/SQL.

To practice all areas of PL/SQL, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.