Wednesday, November 14, 2012

PL/SQL Interview Questions - New Questions Set 2

  1. Which of the following statements is true about implicit cursors?
    1. Implicit cursors are used for SQL statements that are not named.
    2. Developers should use implicit cursors with great care.
    3. Implicit cursors are used in cursor for loops to handle data processing.
    4. Implicit cursors are no longer a feature in Oracle.
  2. Which of the following is not a feature of a cursor FOR loop?
    1. Record type declaration.
    2. Opening and parsing of SQL statements.
    3. Fetches records from cursor.
    4. Requires exit condition to be defined.
  3. A developer would like to use referential datatype declaration on a variable. The variable name is EMPLOYEE_LASTNAME, and the corresponding table and column is EMPLOYEE, and LNAME, respectively. How would the developer define this variable using referential datatypes?
    1. Use employee.lname%type.
    2. Use employee.lname%rowtype.
    3. Look up datatype for EMPLOYEE column on LASTNAME table and use that.
    4. Declare it to be type LONG.
  4. Which three of the following are implicit cursor attributes?
    1. %found
    2. %too_many_rows
    3. %notfound
    4. %rowcount
    5. %rowtype
  5. If left out, which of the following would cause an infinite loop to occur in a simple loop?
    1. LOOP
    2. END LOOP
    3. IF-THEN
    4. EXIT
  6. Which line in the following statement will produce an error?
    1. cursor action_cursor is
    2. select name, rate, action
    3. into action_record
    4. from action_table;
    5. There are no errors in this statement.
  7. The command used to open a CURSOR FOR loop is
    1. open
    2. fetch
    3. parse
    4. None, cursor for loops handle cursor opening implicitly.
  8. What happens when rows are found using a FETCH statement
    1. It causes the cursor to close
    2. It causes the cursor to open
    3. It loads the current row values into variables
    4. It creates the variables to hold the current row values
  9. Read the following code:
(v_movie_id {Argument Mode} NUMBER, v_cost_per_ticket {argument mode} NUMBER)
           IF v_cost_per_ticket  > 8.5 THEN
SELECT  cost_per_ticket
INTO            v_cost_per_ticket
FROM            gross_receipt
WHERE   movie_id = v_movie_id;
Which mode should be used for V_COST_PER_TICKET?
    1. IN
    2. OUT
    3. RETURN
    4. IN OUT
  1. Read the following code:
      {trigger information}
      {additional code}
The trigger code should only execute when the column, COST_PER_TICKET, is greater than $3. Which trigger information will you add?
    1. WHEN (new.cost_per_ticket > 3.75)
    2. WHEN (:new.cost_per_ticket > 3.75
    3. WHERE (new.cost_per_ticket > 3.75)
    4. WHERE (:new.cost_per_ticket > 3.75)
  1. What is the maximum number of handlers processed before the PL/SQL block is exited when an exception occurs?
    1. Only one
    2. All that apply
    3. All referenced
    4. None
  2. For which trigger timing can you reference the NEW and OLD qualifiers?
    1. Statement and Row
    2. Statement only
    3. Row only
    4. Oracle Forms trigger
  3. Read the following code:
RETURN number IS
v_yearly_budget NUMBER;
       SELECT  yearly_budget
       INTO            v_yearly_budget
       FROM            studio
       WHERE   id = v_studio_id;
       RETURN v_yearly_budget;
Which set of statements will successfully invoke this function within SQL*Plus?
    1. VARIABLE g_yearly_budget NUMBER
      EXECUTE g_yearly_budget := GET_BUDGET(11);
    2. VARIABLE g_yearly_budget NUMBER
      EXECUTE :g_yearly_budget := GET_BUDGET(11);
    3. VARIABLE :g_yearly_budget NUMBER
      EXECUTE :g_yearly_budget := GET_BUDGET(11);
    4. VARIABLE g_yearly_budget NUMBER
      :g_yearly_budget := GET_BUDGET(11);
14.    CREATE OR REPLACE PROCEDURE update_theater
(v_name IN VARCHAR v_theater_id IN NUMBER) IS
       UPDATE  theater
       SET             name = v_name
       WHERE   id = v_theater_id;
END update_theater;
        When invoking this procedure, you encounter the error: 
ORA-000: Unique constraint(SCOTT.THEATER_NAME_UK) violated.
How should you modify the function to handle this error?
    1. An user defined exception must be declared and associated with the error code and handled in the EXCEPTION section.
    2. Handle the error in EXCEPTION section by referencing the error code directly.
    3. Handle the error in the EXCEPTION section by referencing the UNIQUE_ERROR predefined exception.
    4. Check for success by checking the value of SQL%FOUND immediately after the UPDATE statement.
  1. Read the following code:
v_budget        studio.yearly_budget%TYPE;
       v_budget := get_budget(11);
       IF v_budget < 30000
       END IF;
You are about to add an argument to CALCULATE_BUDGET. What effect will this have?
    1. The GET_BUDGET function will be marked invalid and must be recompiled before the next execution.
    2. The SET_BUDGET function will be marked invalid and must be recompiled before the next execution.
    3. Only the CALCULATE_BUDGET procedure needs to be recompiled.
    4. All three procedures are marked invalid and must be recompiled.
  1. Which procedure can be used to create a customized error message?
    2. SQLERRM
  2. The CHECK_THEATER trigger of the THEATER table has been disabled. Which command can you issue to enable this trigger?
    1. ALTER TRIGGER check_theater ENABLE;
    2. ENABLE TRIGGER check_theater;
    3. ALTER TABLE check_theater ENABLE check_theater;
    4. ENABLE check_theater;
  3. Examine this database trigger
CREATE OR REPLACE TRIGGER prevent_gross_modification
{additional trigger information}
       IF TO_CHAR(sysdate, DY) = MON
   RAISE_APPLICATION_ERROR(-20000,Gross receipts cannot be deleted on Monday);
       END IF;
This trigger must fire before each DELETE of the GROSS_RECEIPT table. It should fire only once for the entire DELETE statement. What additional information must you add?
    1. BEFORE DELETE ON gross_receipt
    2. AFTER DELETE ON gross_receipt
    3. BEFORE (gross_receipt DELETE)
    4. FOR EACH ROW DELETED FROM gross_receipt
  1. Examine this function:
(v_studio_id IN NUMBER, v_new_budget IN NUMBER) IS
       UPDATE  studio
       SET             yearly_budget = v_new_budget
       WHERE   id = v_studio_id;
               RETURN TRUEl;
               RETURN FALSE;
       END IF;
Which code must be added to successfully compile this function?
    1. Add RETURN right before the IS keyword.
    2. Add RETURN number right before the IS keyword.
    3. Add RETURN boolean right after the IS keyword.
    4. Add RETURN boolean right before the IS keyword.
  1. Under which circumstance must you recompile the package body after recompiling the package specification?
    1. Altering the argument list of one of the package constructs
    2. Any change made to one of the package constructs
    3. Any SQL statement change made to one of the package constructs
    4. Removing a local variable from the DECLARE section of one of the package constructs
  2. Procedure and Functions are explicitly executed. This is different from a database trigger. When is a database trigger executed?
    1. When the transaction is committed
    2. During the data manipulation statement
    3. When an Oracle supplied package references the trigger
    4. During a data manipulation statement and when the transaction is committed
  3. Which Oracle supplied package can you use to output values and messages from database triggers, stored procedures and functions within SQL*Plus?
    3. DBMS_LIST
  4. What occurs if a procedure or function terminates with failure without being handled?
    1. Any DML statements issued by the construct are still pending and can be committed or rolled back.
    2. Any DML statements issued by the construct are committed
    3. Unless a GOTO statement is used to continue processing within the BEGIN section, the construct terminates.
    4. The construct rolls back any DML statements issued and returns the unhandled exception to the calling environment.
  5. Examine this code
       theater_pck.v_total_seats_sold_overall := theater_pck.get_total_for_year;
For this code to be successful, what must be true?
    1. Both the V_TOTAL_SEATS_SOLD_OVERALL variable and the GET_TOTAL_FOR_YEAR function must exist only in the body of the THEATER_PCK package.
    2. Only the GET_TOTAL_FOR_YEAR variable must exist in the specification of the THEATER_PCK package.
    3. Only the V_TOTAL_SEATS_SOLD_OVERALL variable must exist in the specification of the THEATER_PCK package.
    4. Both the V_TOTAL_SEATS_SOLD_OVERALL variable and the GET_TOTAL_FOR_YEAR function must exist in the specification of the THEATER_PCK package.
  1. A stored function must return a value based on conditions that are determined at runtime. Therefore, the SELECT statement cannot be hard-coded and must be created dynamically when the function is executed. Which Oracle supplied package will enable this feature?
    1. DBMS_DDL
    2. DBMS_DML
    3. DBMS_SYN
    4. DBMS_SQL

Thursday, November 8, 2012

PL/SQL Interview Questions - New Questions Set 1

Q1. I can SELECT from SQL*Plus but not from PL/SQL. What is wrong?
PL/SQL respect object privileges given directly to the user, but does not observe privileges given through roles. The consequence is that a SQL statement can work in SQL*Plus, but will give an error in PL/SQL. Choose one of the following solutions:
  • Grant direct access on the tables to your user. Do not use roles!
GRANT select ON scott.emp TO my_user;
  • Define your procedures with invoker rights (Oracle 8i and higher);
create or replace procedure proc1
authid current_user is

  • Move all the tables to one user/schema.
Q2. What is a mutating and constraining table?
“Mutating” means “changing”. A mutating table is a table that is currently being modified by an update, delete, or insert statement. When a trigger tries to reference a table that is in state of flux (being changed), it is considered “mutating” and raises an error since Oracle should not return data that has not yet reached its final state.
Another way this error can occur is if the trigger has statements to change the primary, foreign or unique key columns of the table off which it fires. If you must have triggers on tables that have referential constraints, the workaround is to enforce the referential integrity through triggers as well.
There are several restrictions in Oracle regarding triggers:
  • A row-level trigger cannot query or modify a mutating table. (Of course, NEW and OLD still can be accessed by the trigger).
  • A statement-level trigger cannot query or modify a mutating table if the trigger is fired as the result of a CASCADE delete.

Q3. Explain the difference between a FUNCTION, PROCEDURE and PACKAGE.
A function and procedure are the same in that they are intended to be a collection of PL/SQL code that carries a single task. While a procedure does not have to return any values to the calling application, a function will return a single value. A package on the other hand is a collection of functions and procedures that are grouped together based on their commonality to a business function or application.

Q4. Is there a PL/SQL Engine in SQL*Plus?
No. Unlike Oracle Forms, SQL*Plus does not have an embedded PL/SQL engine. Thus, all your PL/SQL code is sent directly to the database engine for execution. This makes it much more efficient as SQL statements are not stripped off and sent to the database individually.

Q5. What is the difference between %TYPE and %ROWTYPE?
Both %TYPE and %ROWTYPE are used to define variables in PL/SQL as it is defined within the database. If the datatype or precision of a column changes, the program automatically picks up the new definition from the database without having to make any code changes.
The %TYPE and %ROWTYPE constructs provide data independence, reduces maintenance costs, and allows programs to adapt as the database changes to meet new business needs.
  • %TYPE is used to declare a field with the same type as that of a specified table’s column.
  • %ROWTYPE is used to declare a record with the same types as found in the specified database table, view or cursor.
Q6. What Oracle Packages that are commonly available for PL/SQL developers?

Q7. What are SQLCODE and SQLERRM and why are they important for PL/SQL developers?
SQLCODE – returns the value of the error number for the last error encountered.
– returns the actual error message for the last error encountered.
They can be used in exception handling to report, or, store in an error log table, the error that occurred in the code. These are especially useful for the WHEN OTHERS exception.

Q8. What is PL/SQL table?
PL/SQL tables are PL/SQL’s way of providing arrays. Arrays are like temporary tables in memory and thus are processed very quickly. It is important for you to realize that they are not database tables, and DML statements cannot be issued against them. This type of table is indexed by a binary integer counter (it cannot be indexed by another type of number) whose value can be referenced using the number of the index.
Remember that PL/SQL tables exist in memory only, and therefore don’t exist in any persistent way, disappearing after the session ends.

Q9. What is a Cursor?
Cursors are work areas used for internal processing in order to execute an SQL statement. In other word, it is a point to context area which has information for Oracle to process SQL statements. It allows fetching rows returned by a select statement.
There are two types of Cursors.
  • Implicit Cursor which occurs on behalf of every SQL statement and it is declared automatically.
  • Explicit Cursor is a Named Cursor used in PL SQL which is defined by programmer.

Q10. What is the difference between an explicit cursor and select into statement?
When a cursor name is explicitly assigned to a SELECT statement through CURSOR statement it is called an explicit cursor. Explicit cursors are generally used to work with more than one row within PL/SQL. It has got the following attributes:
1. Defining the Cursor
2. Opening the Cursor
3. Fetching rows(values) from Cursor (one at a time)
4. Closing the Cursor

SELECT into statement allows you to retrives the query result in user defined variables. But the limitation here is that the query must return a single row. Its a best practice to handle NO_DATA_FOUND and TOO_MANY_ROWS exception when dealing with “SELECT INTO” statement.