Oracle PLSQL Semester 1 Mid Term Review

download Oracle PLSQL Semester 1 Mid Term Review

of 16

Transcript of Oracle PLSQL Semester 1 Mid Term Review

  • 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 ____________________.

    2. PL/SQL is a ______ programming language.

    3. The PL/SQL language uses which of the following (circle answer):

    a. Variables b. Cursors c. Conditional logic d. Data definition language

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

    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. ____________________

    ____________________

    ____________________

    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 (circle answer):

    a. Anonymous b. Procedures c. Manipulative d. Functions

  • Oracle Academy 2 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

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

    DECLARE

    v_date DATE := SYSDATE;

    BEGIN

    DBMS_OUTPUT.PUT_LINE(v_date);

    END;

    10. Where are subprograms stored? ____________________

    11. In the LIKE operator, which symbol is used to represent a single text character or numeric digit (circle answer)?

    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 (circle answer)?

    a. ALL b. Columns c. * d. DISTINCT

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

    14. __________ 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 (circle answer)?

    a. one b. two c. three d. four

    16. PL/SQL variables can be used for (circle answer):

    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)

  • Oracle Academy 3 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

    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;

    19. Why is the following statement invalid?

    v_valid BOOLEAN NOT NULL;

    __________________________________________________

    20. Select the correct method for declaring a variable of a previously declared variable or database column (circle answer).

    a. %LIKE b. %TYPE c. %COLTYPE

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

    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 (circle answer)?

    a. SUM b. MAX c. MIN d. AVG e. COUNT

  • 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 (circle answers)?

    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?

    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 (circle answer).

    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

  • 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 variables __________ 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? __________

    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;

  • 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? __________

    __________

    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 (circle answer)?

    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? ______________________________

    ______________________________

    42. Which of the following statements can you use in PL/SQL (circle answer)?

    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);

  • Oracle Academy 7 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

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

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

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

    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 __________________.

    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? ____________________

    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;

  • Oracle Academy 8 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

    Name the control structures in PL/SQL that change the logical flow of statements (circle

    answers).

    a. IF statement b. Loop statement c. ROLLBACK statement d. MERGE statement e. CASE statement

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

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

    54. 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;

    55. What is the maximum number of ELSIF statements you can use in an IF conditional statement? __________

  • Oracle Academy 9 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

    56. Choose the correct statements about NULL values (circle answer).

    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.

    57. 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

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

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

    60. How do you terminate a case statement? ____________________

    61. 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;

  • Oracle Academy 10 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

    c. v_appraisal := CASE

    WHEN v_grade = 'A' THEN 'Excellent'

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

    ELSE 'No such grade'

    END CASE;

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

    63. What is the value for v_flag in the following example? __________

    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;

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

    65. 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

    66. When will a WHILE loop quit? ____________________________________________________________

  • Oracle Academy 11 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

    67. The value for v_counter is initialized to 1. How many rows will be inserted into the locations table? _____

    WHILE v_counter

  • Oracle Academy 12 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

    71. 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

    72. 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

    73. 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;

    74. OPEN is an executable statement that performs which of the following operations (circle answer)?

    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.

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

    ____________________________________________________________

  • Oracle Academy 13 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

    76. What is wrong with the following code? ____________________________________________________________

    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;

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

    78. The example below uses what type of composite data type structure? __________

    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;

    ...

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

    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;

    ...

  • Oracle Academy 14 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

    80. Which are the cursor attributes that return useful information about the execution of a cursor manipulation statement (circle answer)?

    a. %OPEN b. %FOUND c. %NOTFOUND d. %ROWTYPE e. %ROWCOUNT

    81. 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.

    __________________________________________________

    82. To display the last name of an employee, what code should you write at Point A in the example below? ______________________________

    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;

    83. Select the correct method for opening the cursor in the example below (circle answer).

    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

    84. 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;

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

  • Oracle Academy 15 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

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

    87. 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

    88. 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?

    __________________________________________________

    89. 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);

  • Oracle Academy 16 Database Programming with PL/SQL

    Copyright 2010, Oracle. All rights reserved.

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