PL/SQL Questions and Answers – PL/SQL Strings

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

1. How many different types of strings are found in PL/SQL?
a) 2
b) 3
c) 4
d) 5
View Answer

Answer: b
Explanation: PL/SQL offers 3 different types of strings. These are Fixed length strings, Variable length strings and Character large objects.

2. In PL/SQL, a variable or a literal is called as a string if it is placed inside which of the following symbol?
a) Round Brackets ()
b) Curly Brackets {}
c) Single Quotes (‘’)
d) Double Quotes (“”)
View Answer

Answer: c
Explanation: In PL/SQL, strings could be either variables or literals. A string literal is enclosed within a pair of single quotes (‘’). For example, ‘Sanfoundry’, ‘123’.

3. Which of the following method can be used to include a single quote inside a string in PL/SQL?
a) Place single quote inside curly brackets to consider them
b) Place single quote inside round brackets to consider them
c) A double quote has to be placed where a single quote has to be considered
d) Single quotes cannot be included in the strings
View Answer

Answer: c
Explanation: To include a single quote inside a string literal, you need to type two single quotes next to one another. For example, ‘This isn’’t Ram’’s book’.
advertisement
advertisement

4. Which of the following datatype can be used to declare a fixed-length string in PL/SQL?
a) CHAR
b) VARCHAR2
c) CLOB
d) NCLOB
View Answer

Answer: a
Explanation: In PL/SQL, to declare a fixed-length string, we can use the CHAR datatype. For this datatype we do not have to specify a maximum length for a fixed-length variable. If we leave off the length constraint, Database automatically uses the maximum length required.

5. In PL/SQL, the ASCII(x) function is used to return the character with the ASCII value x.
a) True
b) False
View Answer

Answer: b
Explanation: The ASCII(x) function is used to return the ASCII value of the character x. Whereas, the CHR(x) function is used to return the character with the ASCII value x.

6. Select the correct output for the following PL/SQL code.

DECLARE 
   var1 varchar2(20) := 'hello sanfoundry'; 
BEGIN 
   dbms_output.put_line(INITCAP(var1));
END;
/

a)

advertisement
HELLO SANFOUNDRY
PL/SQL PROCEDURE successfully completed.

b)

advertisement
Hello sanfoundry
PL/SQL PROCEDURE successfully completed.

c)

Hello Sanfoundry
PL/SQL PROCEDURE successfully completed.

d)

HELLO sanfoundry
PL/SQL PROCEDURE successfully completed.
View Answer
Answer: c
Explanation: The correct output of the given PL/SQL code would be –

Hello Sanfoundry
PL/SQL PROCEDURE successfully completed.

The INITCAP() function is used to convert starting letter of each word in the string to capital form. The statement PL/SQL procedure successfully completed will be printed after the code will be perfectly executed.

7. Select the correct output for the following PL/SQL code.

DECLARE 
   var1 varchar2(20) := 'hello sanfoundry'; 
BEGIN 
   dbms_output.put_line(INSTR(var1, ‘s’));
END;
/

a)

6
PL/SQL PROCEDURE successfully completed.

b)

16
PL/SQL PROCEDURE successfully completed.

c)

20
PL/SQL PROCEDURE successfully completed.

d)

7
PL/SQL PROCEDURE successfully completed.
View Answer
Answer: d
Explanation: The correct output of the given PL/SQL code would be –

7
PL/SQL PROCEDURE successfully completed.

The INSTR(x, ‘y’) function is used to find the specified string(y) in the string x and returns the position of y at which it occurs in x. The statement PL/SQL procedure successfully completed will be printed after the code will be perfectly executed.

8. Select the correct output for the following PL/SQL code.

DECLARE 
   var1 varchar2(30) := '????hello sanfoundry????'; 
BEGIN 
   dbms_output.put_line(TRIM(‘?’ FROM var1));
END;
/

a)

hello sanfoundry
PL/SQL PROCEDURE successfully completed.

b)

????hello sanfoundry
PL/SQL PROCEDURE successfully completed.

c)

hello sanfoundry????
PL/SQL PROCEDURE successfully completed.

d)

????hello sanfoundry????
PL/SQL PROCEDURE successfully completed.
View Answer
Answer: a
Explanation: The correct output of the given PL/SQL code would be –

hello sanfoundry
PL/SQL PROCEDURE successfully completed.

The TRIM() function is used to trim the specified character from the left and right of the given string. The statement PL/SQL procedure successfully completed will be printed after the code will be perfectly executed.


9. Select the correct output for the following PL/SQL code.

DECLARE 
   var1 varchar2(20) := 'hello sanfoundry'; 
BEGIN 
   dbms_output.put_line(SUBSTR (var1, 8, 4));
END;
/

a)

nfou
PL/SQL PROCEDURE successfully completed.

b)

hell
PL/SQL PROCEDURE successfully completed.

c)

anfo
PL/SQL PROCEDURE successfully completed.

d)

ndry
PL/SQL PROCEDURE successfully completed.
View Answer
Answer: c
Explanation: The correct output of the given PL/SQL code would be –

anfo
PL/SQL PROCEDURE successfully completed.

The SUBSTR(string, value1, value2) function is used to return the substring from the given string that begins at the position specified by value1 and has a length specified by value2. The statement PL/SQL procedure successfully completed will be printed after the code will be perfectly executed.

10. In PL/SQL, the UPPER(x) function is used to convert the string x to uppercase and return it as output.
a) True
b) False
View Answer

Answer: a
Explanation: In PL/SQL, the UPPER(x) function is used to convert the string x to uppercase and return it as output. For example – UPPER(‘sanfoundry’) will return SANFOUNDRY as output.

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.