PLSQL Semester 1 Mid Term Review Answers

16
Oracle Academy 1 Database Programming with PL/SQL Copyright © 2010, Oracle. All rights reserved. Database Programming with PL/SQL Semester 1 Mid Term Review Name: _________________________________________ 1. PL/SQL stands for Procedural Language extension to SQL . 2. PL/SQL is a 3GL programming language. 3. The PL/SQL language uses which of the following: a. Variables b. Cursors c. Conditional logic d. Data definition language 4. The basic unit in a PL/SQL program is a block . 5. PL/SQL allows you to logically combine multiple SQL statements as one unit or block. The application can send the entire block to the database instead of sending the SQL statements one at a time. This significantly reduces the number of database calls. (True or False) 6. Name the three sections of a PL/SQL block. Declarative, executable, exception handling 7. The declarative section contains SQL statements and PL/SQL statements to manipulate data in the database. (True or False) 8. The three types of blocks that can make up a PL/SQL program are: a. Anonymous b. Procedures c. Manipulative d. Functions 9. The following is what type of PL/SQL program? Anonymous DECLARE v_date DATE := SYSDATE; BEGIN DBMS_OUTPUT.PUT_LINE(v_date); END;

Transcript of PLSQL Semester 1 Mid Term Review Answers

Page 1: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 1 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

Database Programming with PL/SQL

Semester 1 Mid Term Review

Name: _________________________________________

1. PL/SQL stands for Procedural Language extension to SQL .

2. PL/SQL is a 3GL programming language.

3. The PL/SQL language uses which of the following:

a. Variables

b. Cursors

c. Conditional logic

d. Data definition language

4. The basic unit in a PL/SQL program is a block .

5. PL/SQL allows you to logically combine multiple SQL statements as one unit or block. The

application can send the entire block to the database instead of sending the SQL statements

one at a time. This significantly reduces the number of database calls. (True or False)

6. Name the three sections of a PL/SQL block.

Declarative, executable, exception handling

7. The declarative section contains SQL statements and PL/SQL statements to manipulate data

in the database. (True or False)

8. The three types of blocks that can make up a PL/SQL program are:

a. Anonymous

b. Procedures

c. Manipulative

d. Functions

9. The following is what type of PL/SQL program?

Anonymous DECLARE

v_date DATE := SYSDATE;

BEGIN

DBMS_OUTPUT.PUT_LINE(v_date);

END;

Page 2: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 2 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

10. Where are subprograms stored?

In the database

11. In the LIKE operator, which symbol is used to represent a single text character or numeric

digit?

a. "_ "

b. "/"

c. "&"

d. "%" asdf

12. Which of the following can be used in the SELECT statement to return all columns of data in

a table?

a. ALL

b. Columns

c. *

d. DISTINCT

13. The DISTINCT keyword is used to eliminate duplicate rows from the output of a SQL

statement.

14. NVL is a general function that converts a null value to a date, a character, or a number.

15. To avoid a Cartesian product, if there are four tables in the FROM clause, what is the

minimum joins that must be specified?

a. one

b. two

c. three

d. four

16. PL/SQL variables can be used for:

a. Reusability

b. Comments

c. Stored values

d. Default values

17. A reserved word can be used as an identifier in a PL/SQL program. (True or False)

18. Which are valid declaration statements?

a. v_first_name := 'John';

b. v_date DATE := TODAY;

c. age_c NUMBER (3) != 25;

d. count_loop BINARY_INTEGER := 0;

Page 3: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 3 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

19. Why is the following statement invalid?

A NOT NULL variable must be assigned a value.

v_valid BOOLEAN NOT NULL;

20. Select the correct method for declaring a variable of a previously declared variable or

database column.

a. %LIKE

b. %TYPE

c. %COLTYPE

21. AVG, COUNT, and SUM are what type of functions? Group

22. Choose the statement that will run without error.

a. SELECT region_id, COUNT(country_id)

GROUP BY region_id

FROM wf_countries

WHERE region_id < 15;

b. SELECT region_id, COUNT(country_id)

FROM wf_countries

WHERE region_id < 15

GROUP BY COUNT(country_id);

c. SELECT region_id, COUNT(country_id)

FROM wf_countries

WHERE region_id < 15

GROUP BY region_id;

23. Group functions can be used in subqueries. (True or False)

24. Subqueries must contain only one row to the main query. (True or False)

25. Which group functions below act on text, number and date datatypes?

a. SUM

b. MAX

c. MIN

d. AVG

e. COUNT

Page 4: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 4 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

26. In which instances listed below is a SQL function allowed in a PL/SQL procedural

statement?

a. Single-row character

b. Date

c. Group functions

d. Data type conversion

27. Consider the following PL/SQL block. What occurs for this block to be valid?

PL/SQL implicitly converts v_sal_increase to a number.

DECLARE

v_salary NUMBER(6):=6000;

v_sal_increase VARCHAR2(5):='1000';

v_total_salary v_salary%TYPE;

BEGIN

v_total_salary:= v_salary + v_sal_increase;

DBMS_OUTPUT.PUT_LINE(v_total_salary);

END;

28. Indicate which statements are valid.

a. v_new_date DATE := '02-JUN-1992';

b. v_new_date DATE := 'Yesterday';

c. v_my_number NUMBER := '123';

d. v_my_number NUMBER := '123abc';

29. Is the following a valid statement? Yes or No

v_date_of_joining DATE:= 'February 02,2000';

30. Is the following a valid statement? Yes or No

v_salary NUMBER;

31. Is the following a valid statement? Yes or No

v_address VARCHAR2(30);

32. What is a lexical unit?

a. A building block of a PL/SQL block

b. A type of variable

c. A data type for a variable

Page 5: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 5 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

33. Which of the following is a composite data type?

a. VARCHAR2

b. RECORD

c. CLOB

34. It is good programming practice to allow implicit data type conversions since they do not

affect performance. (True or False)

35. The operators in SQL (logical, arithmetic, parentheses control, etc.) are the same for

PL/SQL. (True or False)

36. In PL/SQL, a variable’s scope is the block in which it is declared plus all blocks nested

within the declaring block.

37. If PL/SQL does not find the variable declared locally, it looks upward in the declarative

section of the parent blocks. PL/SQL does not look downward in the child blocks. (True or

False)

38. What is the value of the highlighted line of code in this block of code? 12-Dec-2002

DECLARE

v_father_name VARCHAR2(20):='Patrick';

v_date_of_birth DATE:='20-Apr-1972';

BEGIN

DECLARE

v_child_name VARCHAR2(20):='Mike';

v_date_of_birth DATE:='12-Dec-2002';

BEGIN

DBMS_OUTPUT.PUT_LINE('Father''s Name: '||v_father_name);

DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);

DBMS_OUTPUT.PUT_LINE('Child''s Name: '||v_child_name);

END;

DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);

END;

Page 6: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 6 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

39. What values will be displayed when the following code is executed?

Null, Jay

DECLARE

v_name VARCHAR2(10);

BEGIN

v_name := 'Jay';

DECLARE

v_name VARCHAR2(10);

BEGIN

DBMS_OUTPUT.PUT_LINE (v_name);

v_name := 'Jayne';

END;

DBMS_OUTPUT.PUT_LINE (v_name);

END;

40. An exception can be dealt with in which of the following ways?

a. Handling it in the block in which it occurs

b. Propogating it to the calling environment

c. Handling it in an outer block in which it occurs

41. What are the two methods for adding comments to PL/SQL code?

-- for a single line of comment. /* and */ around multi-line comments.

42. Which of the following statements can you use in PL/SQL?

a. SELECT to retrieve data from the database.

b. DML statements such as INSERT to make changes to rows in the database.

c. DDL statements such as CREATE TABLE to manage objects in the database.

d. Transaction control statements such as SAVEPOINT to control transactional

statements in the database.

e. DCL statements such as REVOKE to adjust privileges to the database objects.

43. Choose the valid statements.

a. SELECT first_name INTO v_fname

FROM employees WHERE employee_id=200;

b. SELECT first_name INTO v_fname

FROM employees WHERE employee_id IN (200, 201);

c. SELECT first_name FROM employees

INTO v_fname WHERE employee_id=200;

d. SELECT first_name FROM employees

INTO v_fname WHERE employee_id IN (200, 201);

44. What is the unique characteristic about the returned value of a PL/SQL SELECT statement?

It must return exactly one row of data.

Page 7: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 7 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

45. What are the two most common error messages for a PL/SQL SELECT statement?

TOO_MANY_ROWS and NO_ROWS_FOUND

46. What would be the result of the following statement?

All rows in the employees table will be deleted

DELETE employees;

47. To determine how many rows are affected by a statement, use the following implicit cursor

attribute:

a. SQL%FOUND

b. SQL%NOTFOUND

c. SQL%ROWCOUNT

d. SQL%COUNT

48. You can use implicit cursor attributes such as SQL%ROWCOUNT directly inside a DML

statement as in the following example. (True or False)

INSERT INTO log_table VALUES (USER, SYSDATE, SQL%ROWCOUNT);

49. To SELECT more than one row, you must declare and use an explicit cursor .

50. Transaction control commands are valid in PL/SQL and therefore can be directly used in the

executable section only of a PL/SQL block. (True or False)

51. What values are inserted into the pairtable table based on this PL/SQL code?

7,8 and 13,14

BEGIN

INSERT INTO pairtable VALUES (7, 8);

SAVEPOINT my_sp_1;

INSERT INTO pairtable VALUES (9, 10);

SAVEPOINT my_sp_2;

INSERT INTO pairtable VALUES (11, 12);

ROLLBACK to my_sp_1;

INSERT INTO pairtable VALUES (13, 14);

COMMIT;

END;

52. Name the control structures in PL/SQL that change the logical flow of statements.

a. IF statement

b. Loop statement

c. ROLLBACK statement

d. MERGE statement

e. CASE statement

Page 8: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 8 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

53. An IF statement requires an ELSE condition. (True or False)

54. ELSEIF is the keyword that introduces a conditional expression. (True or False)

55. Select the valid IF statement.

a. DECLARE

v_myage NUMBER :=31;

BEGIN

IF v_myage < 11 THEN

DBMS_OUTPUT.PUT_LINE(' I am a child ');

ELSE

DBMS_OUTPUT.PUT_LINE(' I am not a child ');

END IF;

END;

b. DECLARE

v_myage NUMBER :=31;

BEGIN

IF v_myage < 11 THEN

DBMS_OUTPUT.PUT_LINE(' I am a child ');

ELSIF

DBMS_OUTPUT.PUT_LINE(' I am not a child ');

END IF;

END;

c. DECLARE

v_myage NUMBER :=31;

BEGIN

IF v_myage < 11 THEN

DBMS_OUTPUT.PUT_LINE(' I am a child ');

ELSE

DBMS_OUTPUT.PUT_LINE(' I am not a child ');

ENDIF;

END;

56. What is the maximum number of ELSIF statements you can use in an IF conditional

statement? Unlimited

57. Choose the correct statements about NULL values.

a. Simple comparisons involving nulls always yield FALSE.

b. In conditional control statements, if a condition yields NULL, it behaves just like a

FALSE.

c. Applying the logical operator NOT to a null yields NULL.

Page 9: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 9 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

58. What will be displayed when this block is executed?

DECLARE

v_alpha BOOLEAN := TRUE;

v_beta BOOLEAN;

v_char VARCHAR(4) := 'up';

BEGIN

IF (v_alpha AND v_beta) THEN

v_char:='down';

ELSE v_char:='left';

END IF;

DBMS_OUTPUT.PUT_LINE(v_char);

END;

a. Up

b. Down

c. Left

d. Null

59. A CASE expression is just like an IF statement but with fewer words. (True or False)

60. A CASE expression returns one of a number of values into a variable. (True or False)

61. How do you terminate a case statement? END CASE;

62. Select the correct CASE expression.

a. v_appraisal :=

CASE

WHEN v_grade = 'A' THEN 'Excellent';

WHEN v_grade IN ('B', 'C') THEN 'Good';

ELSE 'No such grade';

END;

b. v_appraisal :=

CASE

WHEN v_grade = 'A' THEN 'Excellent'

WHEN v_grade IN (('B', 'C') THEN 'Good'

ELSE 'No such grade'

END;

c. v_appraisal :=

CASE

WHEN v_grade = 'A' THEN 'Excellent'

WHEN v_grade IN (('B', 'C') THEN 'Good'

ELSE 'No such grade'

END CASE;

Page 10: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 10 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

63. A CASE statement may contain many PL/SQL statements. (True or False)

64. What is the value for v_flag in the following example?

FALSE

DECLARE

v_flag BOOLEAN;

v_reorder_flag BOOLEAN := NULL;

v_available_flag BOOLEAN := FALSE;

BEGIN

v_flag := v_reorder_flag AND v_available_flag;

END;

65. What is the statement required of a basic loop so that it will not be infinite? EXIT

66. How many times will the statement in the loop below execute?

DECLARE

v_counter NUMBER := 1;

BEGIN

LOOP

DBMS_OUTPUT.PUT_LINE('The square of '||v_counter||' is: '||

POWER(v_counter,2));

v_counter := v_counter + 1;

EXIT WHEN v_counter < 10;

END LOOP;

END;

a. 1

b. 3

c. 9

67. When will a WHILE loop quit?

When the controlling condition is no longer True.

Page 11: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 11 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

68. The value for v_counter is initialized to 1. How many rows will be inserted into the locations

table?

WHILE v_counter <= 3 LOOP

INSERT INTO locations (location_id, city, country_id)

VALUES((v_loc_id + v_counter), v_new_city, v_countryid);

v_counter := v_counter + 1;

END LOOP;

a. 1

b. 2

c. 3

d. 4

69. In a WHILE loop, the controlling condition is checked at the start of each iteration.

(True or False)

70. How many lines of output will be displayed? One

DECLARE

i := 2;

BEGIN

WHILE i < 3 LOOP

i := 4;

DBMS_OUTPUT.PUT_LINE('The counter is: ' || i);

END LOOP;

END;

71. How many times will the FOR loop below execute? Once

FOR i in 3..3

LOOP

statement1;

END LOOP;

Page 12: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 12 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

72. What is the last message from this example?

BEGIN

FOR v_outerloop in 1..3 LOOP

FOR v_innerloop in REVERSE 1..5 LOOP

DBMS_OUTPUT.PUT_LINE('Outer loop is: '||v_outerloop||

' and inner loop is: '||v_innerloop);

END LOOP;

END LOOP;

END;

a. Outer loop is: 3 and inner loop is: 5

b. Outer loop is: 1 and inner loop is: 5

c. Outer loop is: 3 and inner loop is: 1

73. What are the steps for using a cursor?

a. Define, select, close

b. Open, fetch, close

c. Define, open, fetch, close

d. Open, select, fetch, close

74. The following statement is a valid cursor declaration. (True or False)

DECLARE

CURSOR dept_emp_cursor IS

SELECT department_name, COUNT(*) AS how_many

FROM departments d, employees e

WHERE d.department_id = e.department_id

GROUP BY d.department_name

HAVING COUNT(*) > 1;

75. OPEN is an executable statement that performs which of the following operations?

a. Executes the SELECT statement in the cursor declaration, returning the results into

the active set (fills the box with data)

b. Dynamically allocates memory for a context area

c. Retrieves the first row from the cursor.

d. Positions the pointer to the first row in the active set.

76. How do you test to see whether the cursor contains rows?

You use the %NOTFOUND attribute to check whether the entire active set has been

retrieved.

Page 13: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 13 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

77. What is wrong with the following code?

FETCH is performed before the cursor is opened.

DECLARE

CURSOR emp_curs IS

SELECT last_name, salary FROM employees;

v_last_name employees.last_name%TYPE;

v_salary employees.salary%TYPE;

BEGIN

FETCH emp_curs INTO v_last_name, v_salary;

OPEN emp_curs;

FETCH emp_curs INTO v_last_name, v_salary;

CLOSE emp_curs;

END;

78. A cursor may be reopened at any time. (True or False)

79. The example below uses what type of composite data type structure? Record

DECLARE

CURSOR emp_cursor IS

SELECT employee_id, last_name

FROM employees

WHERE department_id = 30;

v_emp_record emp_cursor%ROWTYPE;

BEGIN

OPEN emp_cursor;

LOOP

FETCH emp_cursor INTO v_emp_record;

...

80. How do you reference the last_name value inside the loop in the example below?

v_emp_record.last_name

DECLARE

CURSOR emp_cursor IS

SELECT employee_id, last_name

FROM employees

WHERE department_id = 30;

v_emp_record emp_cursor%ROWTYPE;

BEGIN

OPEN emp_cursor;

LOOP

FETCH emp_cursor INTO v_emp_record;

...

Page 14: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 14 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

81. Which are the cursor attributes that return useful information about the execution of a cursor

manipulation statement?

a. %OPEN

b. %FOUND

c. %NOTFOUND

d. %ROWTYPE

e. %ROWCOUNT

82. What type of loop performs the following actions: The cursor is opened, a row is fetched

once for each iteration in the loop, the loop is terminated automatically when the last row is

processed, and the cursor is closed automatically.

A cursor FOR loop

83. To display the last name of an employee, what code should you write at Point A in the

example below? emp_record.last_name

DECLARE

CURSOR emp_cursor IS SELECT * FROM employees;

BEGIN

FOR emp_record IN emp_cursor LOOP

DBMS_OUTPUT.PUT_LINE( --Point A -- );

END LOOP;

END;

84. Select the correct method for opening the cursor in the example below.

CURSOR emp_curs

(p_dept_id employees.department_id%TYPE,

p_job_id employees.job_id%TYPE) IS

SELECT * FROM employees

WHERE department_id = p_dept_id

AND job_id = p_job_id;

a. OPEN emp_curs (20);

b. FOR emp_rec IN emp_curs (20) LOOP

c. OPEN emp_curs ('IT_PROG', 20);

d. FOR emp_rec IN emp_curs (20, 'IT_PROG') LOOP

85. Is this a valid cursor declaration? (Yes or No)

CURSOR dept_curs (p_loc_id NUMBER(4)) IS

SELECT * FROM departments

WHERE location_id = p_loc_id;

86. What is the main purpose of the FOR UPDATE clause in a cursor declaration?

To lock each row as it is fetched.

Page 15: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 15 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

87. In which DML statements would you use the WHEN CURRENT OF clause?

UPDATE or DELETE for a fetched row in a cursor with FOR UPDATE.

88. You want to fetch rows from the EMPLOYEES table. You want to lock the fetched rows, to

prevent other users from updating them. What would you write in Line A?

CURSOR emp_curs IS

SELECT department_name, employee_id, last_name, salary

FROM employees e, departments d

WHERE e.department_id = d.department_id

-- Line A -- ;

a. FOR UPDATE

b. FOR UPDATE OF employee_id

c. FOR UPDATE OF departments

89. You have declared a cursor as SELECT .... FOR UPDATE; You have OPENed the cursor

and locked the FETCHed rows. When are these row locks released?

When COMMIT or ROLLBACK are executed.

90. What would you enter at Line A?

DECLARE

CURSOR region_cur IS

SELECT * FROM regions;

v_region_rec region_cur%ROWTYPE;

CURSOR country_cur (p_region_id NUMBER) IS

SELECT * FROM countries

WHERE region_id = p_region_id;

v_country_rec country_cur%ROWTYPE;

BEGIN

OPEN region_cur;

LOOP

FETCH region_cur INTO v_region_rec;

EXIT WHEN region_cur%NOTFOUND;

DBMS_OUTPUT.PUT_LINE

(v_region_rec.region_name);

-- Line A --

LOOP

FETCH country_cur INTO v_country_rec;

EXIT WHEN country_cur%NOTFOUND;

......

a. OPEN country_cur (p_region_id);

b. OPEN country_cur (region_cur.region_id);

c. OPEN country_cur (region_cur.region_id);

d. OPEN country_cur (v_region_rec.region_id);

Page 16: PLSQL Semester 1 Mid Term Review Answers

Oracle Academy 16 Database Programming with PL/SQL

Copyright © 2010, Oracle. All rights reserved.

91. You cannot use a FOR loop with multiple cursors. (True or False)