Pagini

joi, 14 februarie 2013

PLSQL Final Exam Semester 1


Section 6

1. A user-defined exception is raised by using: (1) Points
FLAG exception_name;
RAISE exception-name; (*)
PRAGMA EXCEPTION_INIT
RAISE(error_number, exception_name);

2. A user-defined exception can be raised: (1) Points
A. In the declaration section
B. In the executable section
C. In the exception section
B
C
A and B
B and C (*)
A and C

3. Which of the following will successfully return a user - defined error message? (1) Points
RAISE_APPLICATION_ERROR('Error Raised',22001);
RAISE_APPLICATION_ERROR(-20257,'Error raised'); (*)
RAISE_APPLICATION_ERROR(-22001,'Error Raised');
RAISE_APPLICATION_ERROR('Error Raised',20257);

4. Department-id 99 does not exist. What will be displayed when the following code is executed?
DECLARE
v_deptname departments.department_name%TYPE;
BEGIN
SELECT department_name INTO v_deptname
FROM departments WHERE department_id = 99;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20201,'Department
does not exist');
END;

ORA-01403: No Data Found ORA-20201: Department does not exist
ORA-01403: No Data Found
ORA-20201: Department does not exist (*)
None of the above

5. Which of the following EXCEPTION sections are constructed correctly? (Choose two.) (Choose all correct answers)
EXCEPTION
WHEN NO_DATA_FOUND THEN statement_1;
WHEN OTHERS THEN statement_2;
END; (*)

EXCEPTION
WHEN OTHERS THEN statement_2;
WHEN NO_DATA_FOUND THEN statement_1;
END;

EXCEPTION
WHEN NO_DATA_FOUND THEN statement_1;
WHEN NO_DATA_FOUND THEN statement_2;
WHEN OTHERS THEN statement_3;
END;


EXCEPTION
WHEN OTHERS THEN statement_1;
END; (*)

6. Examine the following code. Why does the exception handler not follow good practice guidelines?
DECLARE
v_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO v_salary FROM employees
WHERE employee_id = 999;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;

You should not use DBMS_OUTPUT.PUT_LINE in an exception handler.
employee_id 999 does not exist in the employees table.
The exception handler should test for the named exception NO_DATA_FOUND. (*)
The exception handler should COMMIT the transaction.

7. The following EXCEPTION section is constructed correctly. True or False?
EXCEPTION
WHEN NO_DATA_FOUND OR TOO_MANY_ROWS
THEN statement_1;
statement_2;
WHEN OTHERS
THEN statement_3;
END;

True (*)
False

8. Which of the following are good practice guidelines for exception handling? (Choose three.) (Choose all correct answers)
Test your code with different combinations of data to see what potential errors can happen. (*)
Use an exception handler whenever there is any possibility of an error occurring. (*)
Include a WHEN OTHERS handler as the first handler in the exception section.
Allow exceptions to propagate back to the calling environment.
Handle specific named exceptions where possible, instead of relying on WHEN OTHERS. (*)

9. What will happen when the following code is executed?
BEGIN --outer block
DECLARE --inner block
CURSOR emp_curs IS SELECT * FROM employees;
v_emp_rec emp_curs%ROWTYPE;
BEGIN
OPEN emp_curs;
LOOP
FETCH emp_curs INTO v_emp_rec;
DBMS_OUTPUT.PUT_LINE(v_emp_rec.salary);
END LOOP;
END;
CLOSE emp_curs;
END;

The code will fail because you cannot declare a cursor in an inner block.
The code will fail because the cursor is declared in the inner block but is referenced in the outer block. (*)
The code will execute successfully and display all the employees' salaries.
The code will execute forever because there is no statement to EXIT from the loop.

10.What will be displayed when the following code is executed? What will be displayed when the following code is executed?
<<outer>>
DECLARE
v_myvar NUMBER;
BEGIN
v_myvar := 10;
DECLARE
v_myvar NUMBER := 200;
BEGIN
outer.v_myvar := 20;
v_myvar := v_myvar / 0; --this raises a ZERO_DIVIDE error
outer.v_myvar := 30;
END;
v_myvar := 40;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE(v_myvar);
END;

10
20 (*)
30
40
200

11. Using two nested blocks, a TOO_MANY_ROWS exception is raised within the inner block. Which of the following exception handlers will successfully handle the exception?

WHEN TOO_MANY_ROWS in the inner block
WHEN TOO_MANY_ROWS in either block
WHEN OTHERS in either block
WHEN OTHERS in the inner block
All of the above (*)

12. The following code will execute correctly. True or False?
DECLARE
v_myvar1 NUMBER;
BEGIN
DECLARE
v_myvar2 NUMBER;
BEGIN
v_myvar1 := 100;
END;
v_myvar2 := 100; v END;

True
False (*)

13. Examine the following code. What is the scope and visibility of the outer block's v_last_name?
DECLARE
v_last_name VARCHAR2(20);
BEGIN
DECLARE
v_last_name VARCHAR2(20);
BEGIN
...
END:
...

END;

It is in scope and visible in both blocks.
It is in scope and visible in the outer block only.
It is in scope in both blocks, but visible only in the outer block. (*)
It is visible in both blocks, but in scope only in the outer block.


14. The following code does not violate any constraints and will not raise an ORA-02292 error. What will happen when the code is executed?
BEGIN
DECLARE
e_constraint_violation EXCEPTION;
PRAGMA EXCEPTION_INIT(e_constraint_violation, -2292);
BEGIN
DBMS_OUTPUT.PUT_LINE('Inner block message');
END;
EXCEPTION
WHEN e_constraint_violation THEN
DBMS_OUTPUT.PUT_LINE('Outer block message');
END;

Inner block message' will be displayed.
The code will fail because the exception is declared in the inner block but is referenced in the outer block.(*)
Outer block message' will be displayed.
The code will fail because line 4 should read: PRAGMA EXCEPTION_INIT(-2292, e_constraint_violation);

15. Examine the followiing code. Which exception handlers would successfully trap the exception which will be raised when this code is executed? (Choose two.)
DECLARE
CURSOR emp_curs IS SELECT * FROM employees;
v_emp_rec emp_curs%ROWTYPE;
BEGIN
FETCH emp_curs INTO v_emp_rec;
OPEN emp_curs;
CLOSE emp_curs;
EXCEPTION ...
END;
(1) Points (Choose all correct answers)


WHEN CURSOR_NOT_OPEN
WHEN INVALID_CURSOR (*)
WHEN OTHERS (*)
WHEN NO_DATA_FOUND
WHEN INVALID_FETCH

16. Which of the following best describes a predefined Oracle Server error? (1) Points

Has a standard Oracle error number but must be named by the PL/SQL programmer
Is not raised automatically but must be declared and raised explicitly by the PL/SQL programmer
Has a standard Oracle error number and a standard name which can be referenced in the EXCEPTION section (*)
Is associated with an Oracle error number using PRAGMA EXCEPTION_INIT

17. Examine the following code. What message or messages will be displayed when this code is executed?
DECLARE
v_last_name employees.last_name%TYPE;
v_number NUMBER := 27;
BEGIN
v_number := v_number / 0;
SELECT last_name INTO v_last_name FROM employees
WHERE employee_id = 999;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No rows were found');
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Attempt to divide by zero');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;

No rows were found
Attempt to divide by zero (*)
Attempt to divide by zero No rows were found
An error occurred
No message will be displayed

18. Which kinds of exceptions are raised implicitly (i.e., automatically)? (Choose two.) (1) Points
(Choose all correct answers)
Predefined Oracle Server errors such as NO_DATA_FOUND (*)
User-defined errors
All errors
Non-predefined Oracle Server errors such as ORA-01400 (*)

19. Examine the following code fragment. At Line A, you want to raise an
exception if the fetched salary value is greater than 30000. How can you do this?
DECLARE
v_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO v_salary FROM employees
WHERE employee_id = 100;
IF v_salary > 30000 THEN
--Line A
END IF;
...

Test for WHEN VALUE_TOO_HIGH in the exception section.

Use RAISE_APPLICATION_ERROR to raise an exception explicitly. (*)
Test for WHEN OTHERS in the exception section, because WHEN OTHERS traps all exceptions.
Define an EXCEPTION variable and associate it with an Oracle Server error number using PRAGMA EXCEPTION_INIT.

20. An attempt to update an employee's salary to a negative value will violate a check constraint and raise an ORA-02290 exception. Which of the following is a correct definition of a handler for this exception?

DECLARE
e_sal_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(-02290,e_sal_excep);

DECLARE
PRAGMA EXCEPTION_INIT(e_sal_excep,-02290);
e_sal_excep EXCEPTION;

DECLARE
e_sal_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(e_sal_excep,-02290); (*)

DECLARE
e_sal_excep EXCEPTION;
PRAGMA_EXCEPTION_INIT(e_sal_exception,-02290);

DECLARE
e_sal_excep EXCEPTION;
PRAGMA EXCEPTION_INIT(e_sal_excep,02290);

21. You have created a procedure named MYPROC that accepts three
IN parameters A, B, and C (all numbers). Which of the following calls to MYPROC is NOT correct?

myproc(5,10,20);
myproc(a=>5,b=>10,20) (*)
myproc(a=>5,b=>10,c=>20)
myproc(5,10,c=>20)

22. You have created procedure MYPROC with a single parameter PARM1 NUMBER. Now you want to add a second parameter to the procedure. Which of the following will change the procedure successfully?

ALTER PROCEDURE myproc ADD (parm2 NUMBER);
The procedure cannot be modified. Once a procedure has been created, the number of
parameters cannot be changed.

CREATE OR REPLACE PROCEDURE someproc
(parm1 NUMBER, parm2 NUMBER);
(You do not need to repeat the detailed code of the procedure, only the header)

REPLACE PROCEDURE someproc
(parm1 NUMBER, parm2 NUMBER)
IS
BEGIN ...

CREATE OR REPLACE PROCEDURE MYPROC
(parm1 NUMBER, parm2 NUMBER)
IS
BEGIN ... (*)

23. Which of the following best describes how an IN parameter affects a procedure?

It describes the order in which the procedure's statements should be executed.
It describes which parts of the procedure's code are optional or conditional.
It makes the procedure execute faster.
It passes a value into the procedure when the procedure is invoked. (*)
It allows complex calculations to be executed inside the procedure.


24. Which of the following is NOT correct coding for a procedure parameter?

(p_param IN VARCHAR2)
(p_param VARCHAR2)
(p_param VARCHAR2(50)) (*)
(p_param employees.last_name%TYPE)
(p_param IN OUT VARCHAR2)

25. Which of the following statements about actual parameters is NOT true?

An actual parameter is declared in the calling environment, not in the called procedure
An actual parameter must be the name of a variable (*)
An actual parameter can have a Boolean datatype
The datatypes of an actual parameter and its formal parameter must be compatible
An actual parameter can have a TIMESTAMP datatype

26. Examine the following procedure:
CREATE OR REPLACE PROCEDURE smallproc
(p_param IN NUMBER)
IS
BEGIN ....
The procedure is invoked by:
DECLARE
v_param NUMBER := 20;
BEGIN
smallproc(v_param);
END;
Which of the following statements is true?

p_param is a parameter and v_param is an argument
p_param is a formal parameter and 20 is an actual parameter
p_param is a formal parameter and v_param is an actual parameter (*)
p_param and v_param are both formal parameters, while 20 is an actual parameter
p_param is an actual parameter and v_param is a formal parameter

27. Which of the following are characteristics of PL/SQL subprograms but not
of anonymous PL/SQL blocks? (Choose three.) (Choose all correct answers)

Can take parameters (*)
Are stored in the database (*)
Can begin with the keyword DECLARE
Are named (*)
Are compiled every time they are executed

28. One PL./SQL subprogram can be invoked from within many applications. True or False?

True (*)
False

29. A programmer wants to create a PL/SQL procedure named EMP_PROC.
What will happen when the following code is executed?
CREATE OR REPLACE PROCEDURE emp_proc IS
v_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO v_salary FROM employees
WHERE employee_id = 999;
DBMS_OUTPUT.PUT_LINE('The salary is: ' || v_salary);
END; END;

The statement will raise a NO_DATA_FOUND exception because employee_id 999 does not exist.
The statement will fail because the last line of code should be END emp_proc;
The statement will fail because you cannot declare variables such as v_salary inside a procedure.
The procedure will be created successfully. (*)
The statement will fail because the procedure does not have any parameters.

30. A programmer creates a PL/SQL subprogram which is compiled and stored in the database. Two separate users then execute an application which invokes this subprogram four times. How many times must the subprogram be recompiled?

Twice
Four times
None (*)
Eight times
Once

31. A PL/SQL stored procedure can accept one or more input parameters and can return one or more output values to the calling environment. True or False?

True (*)
False

32. A PL/SQL procedure named MY_PROC1 has been successfully created in the database. The procedure has no parameters. Which of the following will successfully invoke the procedure in Application Express? (Choose two.) (Choose all correct answers)

DECLARE
v_var1 NUMBER := 20;
BEGIN
my_proc1(v_var1);
END;

EXECUTE my_proc1;
BEGIN
my_proc1;
END; (*)

CREATE OR REPLACE PROCEDURE my_proc2 IS
BEGIN
my_proc1;
END my_proc2; (*)

SELECT my_proc1 FROM DUAL;

33. Which parameter mode is the default?

IN (*)
OUT
NUMBER
VARIABLE
CONSTANT

34. The following procedure has been created:

CREATE OR REPLACE PROCEDURE defproc
(A IN NUMBER := 50,
B IN NUMBER,
C IN NUMBER DEFAULT 40)
IS .....
Which one of the following will invoke the procedure correctly?

defproc(30 => A);
defproc(30, 60 => C);
defproc(40, 70); (*)
defproc(10 => A, 25 => C);
defproc;

35. Procedure SOMEPROC has five parameters named A, B, C, D, E in that order. The procedure was called as follows:
SOMEPROC(10,20,D=>50);
How was parameter D referenced?

Positionally
Named (*)
A combination of positionally and named
A combination of named and defaulted
Defaulted

36. The following procedure has been created:

CREATE OR REPLACE PROCEDURE myproc
(A IN NUMBER := 20,
B IN NUMBER,
C IN NUMBER DEFAULT 30)
IS .....
Which of the following will invoke the procedure correctly?

myproc(40);
myproc(10, B => 30, 50);
myproc(C => 25);
All of the above
None of the above (*)

37. Examine the following code: CREATE
PROCEDURE parent
IS BEGIN
child1;
child2;
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL;
END parent;

Neither CHILD1 nor CHILD2 has an exception handler.
When PARENT is invoked, CHILD1 raises a NO_DATA_FOUND exception. What
happens next?

PARENT handles the exception, then CHILD1 continues to execute.
CHILD1 ends abruptly. PARENT handles the exception and then ends. CHILD2
does not execute. (*)

CHILD1 ends abruptly, PARENT handles the exception, then CHILD2 executes.

CHILD1 ends abruptly, PARENT also ends abruptly and returns an unhandled exception. xception.

PARENT does not compile because you cannot use NULL; in an exception handler.

38. You want to see the names, modes and data types of the formal parameters of function MY_FUNC in your schema. How can you do this? (Choose two) (Choose all correct answers)

Query USER_PARAMETERS
Query USER_SOURCE (*)
Query USER_FUNCTIONS
SHOW PARAMETER my_funct;
DESCRIBE my_funct; (*)

39. You want to remove the procedure NO_NEED from your schema. You execute:
DROP PROCEDURE no_need;
Which Data Dictionary views are updated automatically?

USER_PROCEDURES
USER_OBJECTS
USER_SOURCE
All of the above. (*)
None of the above.

40. A function named MYFUNC has been created. This function accepts one IN parameter of datatype VARCHAR2 and returns a NUMBER.
You want to invoke the function within the following anonymous block:

DECLARE
v_var1 NUMBER(6,2);
BEGIN
--Line A
END;
What could be coded at Liine A?

myfunc('Crocodile') := v_var1;
myfunc(v_var1) := 'Crocodile';
myfunc(v_var1, 'Crocodile');
v_var1 := myfunc('Crocodile'); (*)
myfunc('Crocodile', v_var1);


41. Examine the following code:
CREATE OR REPLACE FUNCTION add_func
(p_param1 NUMBER, p_param2 NUMBER)
RETURN NUMBER
IS
BEGIN
RETURN (p_param1 + p_param2);
END;
What will be displayed when the following SQL statement is executed?
SELECT add_func(6, add_func(3,8)) FROM dual;

23
11
66
17 (*)


42. You have created a function named NEWFUNC. You now change some of  the function code, and try to recreate the function by executing:
CREATE OR REPLACE FUNCTION newfunc .... ;
What happens?

The command fails because the function already exists.
The function is automatically dropped and then recreated. (*)
The command fails because you should execute: CREATE AND REPLACE ....;
A second function named NEWFUNC_2 is created.
The function is dropped but not recreated.

43. Which of the following is a difference between a procedure and a function?

A procedure can include DML statements, but a function cannot.
A function must have at least one IN parameter, while parameters are optional for a procedure.
A procedure can return a BOOLEAN datatype, while a function cannot.
A function can be used inside a SQL statement, while a procedure cannot. (*)
A procedure can include an EXCEPTION section, while a function cannot.

44. You try to create a function named MYFUNC. The function does not compile correctly because there are errors in your code. Which Dictionary view can you query to see the errors?

USER_SOURCE

USER_ERRORS (*)
USER_OBJECTS
USER_DEPENDENCIES
USER_COMPILES
USER_OBJECTS
USER_DEPENDENCIES
USER_COMPILES

45. Consider the following function:
CREATE FUNCTION ADD_EM (a NUMBER := 1, b NUMBER := 2 )
RETURN NUMBER
IS BEGIN
RETURN (a+b);
END ADD_EM;
Which one of the following blocks will NOT work correctly?

DECLARE
x NUMBER;
BEGIN
x:= add_em(b=4);
END; (*)

DECLARE
x NUMBER;
BEGIN
x:= add_em(4);
END;

DECLARE
x NUMBER;
BEGIN
x:= add_em(4,5);
END;


DECLARE
x NUMBER;
BEGIN
x:= add_em;
END;

None of them will work.

46. In which DML statements can user-defined functions be used?

INSERT and UPDATE, but not DELETE.
INSERT only.
All DML statements. (*)
UPDATE only
DELETE only

47. What is one of the advantages of using user-defined functions in a SQL
statement?

They automate repetitive formulas which otherwise you would have to type in full
every time you used them. (*)
They execute faster than system-defined functions such as UPPER and LOWER.
They allow you to execute DML from inside a SELECT statement.
They allow you to use functions which return a BOOLEAN.
They are stored on your local PC, not in the database.

48. Where can a function be used in a query?

Nowhere in a query.

Anywhere in a query. (*)
Only in the SELECT clause
Only in the WHERE clause
In the SELECT or WHERE clauses, but not in the ORDER BY clause.

49. How do you specify that you want a procedure MYPROCA to use Invoker's
Rights?

CREATE OR REPLACE PROCEDURE myproca
AUTHID CURRENT_USER IS... (*)
Invoker's Rights are the default, therefore no extra code is needed.
GRANT INVOKER TO myprocA;
ALTER PROCEDURE myproca TO INVOKER;
CREATE OR REPLACE PROCEDURE myproca
AUTHID OWNER IS...

50. How do you specify that you want a procedure MYPROCA to use "Definer's
Rights"?

CREATE OR REPLACE PROCEDURE myproca
AUTHID CURRENT_USER IS...
CREATE OR REPLACE PROCEDURE myproca
AUTHID OWNER IS...
GRANT DEFINER TO myprocA;
ALTER PROCEDURE myproca TO DEFINER;
Definer's Rights are the default, therefore no extra code or commands are needed. (*)

Niciun comentariu:

Trimiteți un comentariu