PL/SQL Questions and Answers – PL/SQL Loops

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

1. Which of the following symbol is used to label a loop in PL/SQL?
a) ()
b) <>
c) “”
d) <<>>
View Answer

Answer: d
Explanation: In PL/SQL, the loops can be labelled. The label should be enclosed between a pair of twin angular brackets (<< and >>) and should appear before the loop starts.

2. Which of the following statement is used to come out of the loop when encountered and pass the program control to the statement following the loop?
a) BREAK
b) CONTINUE
c) EXIT
d) GOTO
View Answer

Answer: c
Explanation: The EXIT statement is used to exit the loop after being encountered and pass the program control to the statement following the loop. When used in a nested loop, it will stop the execution of only the innermost loop and start executing the next statement after the loop.

3. Which of the following statement is used to skip the remaining part of the loop when encountered and forces the next iteration of the loop to take place?
a) BREAK
b) CONTINUE
c) EXIT
d) GOTO
View Answer

Answer: b
Explanation: The CONTINUE statement is used to skip the part of the loop when encountered and then forces the next iteration of loop to take place and skip anything in between.

4. Which of the following is the correct syntax for the basic loop statement in PL/SQL?
a)

LOOP 
SEQUENCE OF statements; 
END;

b)

LOOP 
SEQUENCE OF statements;

c)

advertisement
advertisement
LOOP 
SEQUENCE OF statements; 
END LOOP;

d)

SEQUENCE OF statements; 
END LOOP;
View Answer
Answer: c
Explanation: The correct syntax for the basic loop statement in PL/SQL is –

advertisement
LOOP 
SEQUENCE OF statements; 
END LOOP;

Basic loop structure encloses sequence of statements in between the LOOP and END LOOP statements.

advertisement

5. What will be the output of the following PL/SQL code?

DECLARE 
    x NUMBER := 1; 
BEGIN 
    LOOP 
        dbms_output.put_line(x); 
        x := x + 1; 
        IF x > 3 THEN 
            exit; 
        END IF; 
    END LOOP; 
    dbms_output.put_line('After Exit x is: ' || x); 
END; 
/

a)

1 
2 
3
After Exit x IS: 3
PL/SQL PROCEDURE successfully completed.

b)

1 
2 
3
4
After Exit x IS: 4  
PL/SQL PROCEDURE successfully completed.

c)

1 
2 
3
After Exit x IS: 4

d)

1 
2 
3
After Exit x IS: 4
PL/SQL PROCEDURE successfully completed.
View Answer
Answer: d
Explanation: The correct output for the given PL/SQL code will be –

1 
2 
3
After Exit x IS: 4  
PL/SQL PROCEDURE successfully completed.

The code will run till value 4 but print the number sequence only till 3, 4 will be printed with the statement – “After Exit x is:”.

6. Which of the following is the correct syntax for the for-loop statement in PL/SQL?
a)

FOR initial_value .. final_value 
LOOP 
sequence_of_statements; 
END LOOP;

b)

FOR counter IN initial_value .. final_value 
LOOP 
sequence_of_statements; 
END LOOP;

c)

FOR counter IN initial_value .. final_value 
sequence_of_statements; 
END LOOP;

d)

FOR counter IN initial_value .. final_value 
LOOP 
sequence_of_statements;
View Answer
Answer: b
Explanation: The correct syntax for the FOR-Loop statement in PL/SQL is –

FOR counter IN initial_value .. final_value 
LOOP 
sequence_of_statements; 
END LOOP;

7. We can reverse the for loop using the REVERSE keyword.
a) True
b) False
View Answer

Answer: a
Explanation: We can reverse this order by using the REVERSE keyword. In this case, iteration proceeds the opposite way. After each iteration, the loop counter is decreased.

8. What will be the correct output for the following PL/SQL code?

DECLARE 
    a NUMBER(1) := 1; 
BEGIN 
    WHILE a <5 LOOP 
        dbms_output.put_line('value of a: ' || a); 
        a := a + 1; 
    END LOOP; 
END; 
/

a)

VALUE OF a: 1 
VALUE OF a:2 
VALUE OF a:3 
VALUE OF a: 4
PL/SQL PROCEDURE successfully completed.

b)

VALUE OF a: 1 
VALUE OF a: 2 
VALUE OF a: 3 
VALUE OF a: 4

c)

VALUE OF a: 1 
VALUE OF a: 2 
VALUE OF a: 3 
VALUE OF a: 4
VALUE OF a: 5
PL/SQL PROCEDURE successfully completed.

d)

VALUE OF a: 1 
VALUE OF a: 2 
VALUE OF a: 3 
VALUE OF a: 4
VALUE OF a: 5
View Answer
Answer: a
Explanation: The correct output of the given PL/SQL code will be –

VALUE OF a: 1 
VALUE OF a: 2 
VALUE OF a: 3 
VALUE OF a: 4
PL/SQL PROCEDURE successfully completed.

The loop will be printed till value 4 because of the given condition (a<5). The last line PL/SQL procedure successfully completed, will always be printed after execution of any program.

9. What is the correct syntax for the NESTED LOOP Statements in PL/SQL?
a)

LOOP 
    LOOP
        SEQUENCE OF statements1 
        SEQUENCE OF statements2 
    END LOOP; 
END LOOP;

b)

LOOP 
    SEQUENCE OF statements1 
END LOOP;
LOOP 
    SEQUENCE OF statements2 
END LOOP;

c)

LOOP 
    SEQUENCE OF statements1 
    LOOP 
        SEQUENCE OF statements2 
    END LOOP; 
END LOOP;

d)

LOOP 
    SEQUENCE OF statements1 
    LOOP 
        SEQUENCE OF statements2  
END LOOP;
View Answer
Answer: c
Explanation: The correct syntax for the NESTED LOOP statements in PL/SQL is –

LOOP 
    SEQUENCE OF statements1 
    LOOP 
        SEQUENCE OF statements2 
    END LOOP; 
END LOOP;

Nested loops allow use of one loop inside another loop. The nested loop statements will use 2 LOOP and 2 END LOOP keywords.

10. Which of the following statements is used to provide an unconditional jump from the statement to any specified label in the same subprogram?
a) CONTINUE
b) BREAK
c) EXIT
d) GOTO
View Answer

Answer: d
Explanation: The GOTO statement is used to provide an unconditional jump from the GOTO statement to any specified label in the same subprogram. But it is not recommended in any programming language because it makes it difficult to trace the control flow of a program, making the program hard to understand and hard to modify.

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.