Mata Kuliah : Pemrograman Basis Data II
Dosen : Titik Lusiani, M.Kom, OCA
Practice 2
- The UPDATE_EMPLOYEE procedure contains an algorithm that calculates an employee's commission multiple times throughout the program. If a change is made to the algorithm, the change must be made multiple times. How can this procedure be modified to simplify the code and reduce duplicated code?
- Add an algorithm exception handler.
- Create a library containing the algorithm.
- Add a local subprogram containing the algorithm.
- Create multiple anonymous blocks containing the algorithm.
- For which reason might you create a subprogram within a procedure?
- to bypass a pragma restriction
- to bypass the CURRENT AUTHID restriction
- to allow execution within a SQL statement
- to store a repeating block of code once without creating a separate construct
- When invoking a procedure, you can specify the arguments using the positional method by listing the values in the order of the argument list. Which method would you use to list values in an arbitrary order?
- FIFO
- list
- type
- named
- Which type of construct should you create to solely perform an action without returning a value?
- view
- function
- procedure
- packaged function
- Examine this procedure:
CREATE OR REPLACE PROCEDURE find_seats_sold
(v_movie_id IN NUMBER DEFAULT 34, v_theater_id IN
NUMBER) IS
v_seats_sold gross_receipt.seats_sold%TYPE;
BEGIN
SELECT seats_sold INTO v_seats_sold FROM gross_receipt
WHERE movie_id = v_movie_id AND theater_id =
v_theater_id;
END;
Which command will successfully invoke this procedure
in SQL*Plus?
- EXECUTE find_seats_sold;
- RUN find_seats_sold (v_theater_id => 500, v_movie_id => 34);
- EXECUTE find_seats_sold (v_theater_id => 500, v_movie_id => 34);
- EXECUTE find_seats_sold (v_theater_id := 500, v_movie_id := 34);
- Examine this procedure:
CREATE OR REPLACE PROCEDURE find_seats_sold
(v_movie_id IN NUMBER) IS
v_seats_sold
gross_receipt.seats_sold%TYPE;
BEGIN
SELECT seats_sold INTO v_seats_sold
FROM gross_receipt
WHERE movie_id = v_movie_id;
END;
Which command will successfully invoke
this procedure in SQL*Plus?
- RUN find_seats_sold(34);
- EXECUTE find_seats_sold;
- EXECUTE find_seats_sold (34);
- find_seats_sold ('Riverplace Theater');
- A stored function can be invoked in many different ways. Which invocation example is NOT valid?
- executing the stored function within an INSERT statement
- executing the stored function within a server-side function
- executing the stored function within a client-side function
- executing the stored function within a CHECK constraint of a table
- Examine this function:
CREATE OR
REPLACE FUNCTION get_budget
(v_studio_id IN
NUMBER) RETURN number IS
v_yearly_budget
NUMBER;
BEGIN
SELECT
yearly_budget INTO v_yearly_budget
FROM studio
WHERE id = v_studio_id;
RETURN
v_yearly_budget;
END;
Which set of
statements will successfully invoke this function within SQL*Plus?
- VARIABLE g_yearly_budget NUMBER
:g_yearly_budget :=
GET_BUDGET(11);
B. VARIABLE g_yearly_budget NUMBER
EXECUTE g_yearly_budget :=
GET_BUDGET(11);
C. VARIABLE g_yearly_budget NUMBER
EXECUTE :g_yearly_budget := GET_BUDGET(11);
D. VARIABLE :g_yearly_budget NUMBER
EXECUTE :g_yearly_budget :=
GET_BUDGET(11
- Examine this function:
CREATE OR REPLACE FUNCTION
get_budget
(v_studio_id IN NUMBER,
v_max_budget IN NUMBER) RETURN number IS
v_yearly_budget NUMBER;
BEGIN
SELECT yearly_budget INTO
v_yearly_budget FROM studio
WHERE id = v_studio_id;
IF v_yearly_budget >
v_max_budget THEN
RETURN v_max_budget;
ELSE
RETURN v_yearly_budget;
END IF;
END;
Which set of statements will
successfully invoke this function within SQL*Plus?
A. SELECT id, name, get_budget(id,200)
FROM studio;
B.
VARIABLE
g_yearly_budget NUMBER
EXECUTE g_yearly_budget :=
get_budget(11, 4000000000);
C.
VARIABLE g_yearly_budget NUMBER
RUN :g_yearly_budget :=
get_budget(v_studio_id => 11,v_max_budget => 4000000000);
D.
VARIABLE
g_yearly_budget NUMBER
EXECUTE g_yearly_budget :=
get_budget(v_max_budget => 4000000000, v_studio_id => 11);
- For which purpose are formal parameters used when creating functions?
A.
restricting
pragma references
B. passing values to the function
C.
bypassing
directives to the compiler
D.
prompting
the end user for information.
- When creating a function in SQL*Plus, you receive an error message stating that the function created with compilation errors. What must you do to see the compilation errors?
A.
Issue the SHOW ERRORS command.
B.
Issue the DISPLAY
ERRORS command.
C.
Query the
FUNCTION_ERRORS data dictionary view.
D.
Do nothing. The errors
will display after a five-second delay.
- Examine this function:
CREATE OR REPLACE FUNCTION
set_budget
(v_studio_id IN NUMBER, v_new_budget
IN NUMBER) IS
BEGIN
UPDATE studio SET
yearly_budget = v_new_budget
WHERE id = v_studio_id;
IF SQL%FOUND THEN RETURN
TRUE;
ELSE RETURN FALSE;
END IF;
COMMIT;
END;
Which code must be added to
successfully compile this function?
A.
Add "RETURN;"
immediately before the IS keyword.
B.
Add "RETURN
NUMBER" immediately before the IS keyword.
C.
Add "RETURN
BOOLEAN" immediately after the IS keyword.
D.
Add "RETURN BOOLEAN"
immediately before the IS keyword.
13. Procedures and functions can be created and
stored in the database or in an Oracle Developer application. How is
performance improved when storing procedures and functions in the database?
A. Network roundtrips are reduced.
B. The
object code is created during execution.
C. Network traffic is
decreased by bundling commands.
D. The
source code is stored externally, and the object code is stored internally.
- Examine this function:
CREATE OR REPLACE FUNCTION
set_budget
(v_studio_id IN NUMBER,
v_new_budget IN NUMBER)
RETURN BOOLEAN IS
BEGIN
UPDATE studio
SET yearly_budget =
v_new_budget
WHERE id = v_studio_id;
IF SQL%FOUND THEN
RETURN TRUE;
ELSE RETURN FALSE;
END IF;
COMMIT;
END;
Which code will successfully
invoke this function?
A.
SELECT
id, set_budget(11,500000000)
FROM studio;
B.
DECLARE
v_updated_flag BOOLEAN;
BEGIN
set_budget(11,500000000);
END;
C.
VARIABLE
g_updated_flag BOOLEAN
BEGIN
g_updated_flag := set_budget(11,500000000);
END;
D.
DECLARE v_updated_flag BOOLEAN;
BEGIN
v_updated_flag := set_budget(11,500000000);
END;
- Which two subprogram headers are correct? (Choose two.)
A.
CREATE OR REPLACE
PROCEDURE get_sal IS (v_sal IN number)
B.
CREATE OR REPLACE PROCEDURE get_sal
(v_sal IN number) IS
C.
CREATE OR REPLACE
FUNCTION calc_comm RETURN number (p_amnt IN number)
D.
CREATE OR REPLACE FUNCTION calc_comm
(p_amnt IN number) RETURN number
E.
CREATE OR REPLACE
FUNCTION calc_comm (p_amnt IN number(3,2)) RETURN number
- Procedures and functions are very similar. For which reason would you choose a function over a procedure?
A.
A function allows
Oracle group functions.
B.
A function can be used in a SQL
statement.
C.
A function guarantees
read consistency, while a procedure cannot.
D.
A function can only be
executed from a previously created procedure
- The GET_BUDGET function is no longer needed and should be removed. Which command will successfully remove this function from the database?
A.
DROP get_budget;
B.
REMOVE get_budget;
C.
DROP FUNCTION get_budget;
D.
ALTER get_budget DROP
FUNCTION;
- Which code successfully calculates commission returning it to the calling environment?
A.
CREATE OR REPLACE
FUNCTION calc_comm (v_emp_id IN NUMBER) RETURN number IS
v_total NUMBER;
BEGIN
SELECT SUM(ord.total) INTO
v_total
FROM ord,customer
WHERE ord.custid =
customer.custid
AND customer.repid = v_emp_id;
END;
B.
CREATE OR
REPLACE FUNCTION calc_comm (v_emp_id IN NUMBER) IS
v_total NUMBER;
BEGIN
SELECT SUM(ord.total) INTO v_total
FROM ord,customer
WHERE ord.custid =
customer.custid
AND customer.repid =
v_emp_id;
RETURN (v_total * .20);
END;
C.
CREATE OR REPLACE
FUNCTION calc_comm (v_emp_id IN NUMBER) IS RETURN number
v_total NUMBER;
BEGIN
SELECT SUM(ord.total) INTO
v_total
FROM ord,customer
WHERE ord.custid =
customer.custid
AND customer.repid =
v_emp_id;
RETURN (v_total * .20);
END;
D.
CREATE OR REPLACE FUNCTION calc_comm
(v_emp_id IN NUMBER) RETURN number IS
v_total NUMBER;
BEGIN
SELECT SUM(ord.total) INTO v_total
FROM ord,customer
WHERE ord.custid = customer.custid
AND customer.repid = v_emp_id;
RETURN (v_total * .20);
END;
- Examine this function:
CREATE OR REPLACE FUNCTION
get_budget
(v_studio_id IN NUMBER,
v_max_budget IN NUMBER) RETURN number IS
v_yearly_budget NUMBER;
BEGIN
SELECT yearly_budget INTO
v_yearly_budget FROM studio
WHERE id = v_studio_id;
IF v_yearly_budget >
v_max_budget THEN RETURN v_max_budget;
ELSE RETURN v_yearly_budget;
END IF;
END;
Which set of statements will
successfully invoke this function within SQL*Plus?
A.
SELECT id, name, get_budget(id,200)
FROM studio;
B.
VARIABLE g_yearly_budget
NUMBER
EXECUTE g_yearly_budget :=
get_budget(11, 4000000000);
C.
VARIABLE
g_yearly_budget NUMBER
RUN :g_yearly_budget :=
get_budget(v_studio_id => 11,v_max_budget => 4000000000);
D.
VARIABLE
g_yearly_budget NUMBER
EXECUTE g_yearly_budget :=
get_budget(v_max_budget => 4000000000, v_studio_id => 11);
- A stored function can be invoked in many different ways. Which invocation example is NOT valid?
- executing the stored function within an INSERT statement
- executing the stored function within a server-side function
- executing the stored function within a client-side function
- executing the stored function within the DEFAULT clause of the CREATE TABLE
- You have just created a PL/SQL user-defined function called CALC_COMM. Which statement will successfully test it?
- EXECUTE calc_comm;
- SELECT * FROM calc_comm(emp);
- EXECUTE calc_comm(SELECT total FROM ord);
- SELECT * FROM ord GROUP BY ordid HAVING calc_comm(total) > 5000;
- How do functions simplify maintainability?
- by limiting changes to logic to one location
- by eliminating the need for an executable section
- by eliminating the need for the storage of object code
- by ignoring side effects when invoked from within a SQL statement
- Which two statements are true? (Choose two.)
- A function must return a value.
- Functions and procedures must return a value.
- Functions and procedures must contain IN arguments.
- A function can be invoked from within a PL/SQL expression.
- A procedure must be invoked from within a PL/SQL expression.
- Examine this statement:
SELECT id,
theater_pck.get_budget(id)
FROM studio;
What must be true about the
GET_BUDGET function for this statement to be successful?
- It must be remote.
- It must not modify the database.
- It must also exist as a stand-alone function.
- It must only exist in the body of THEATER_PCK.
- Examine this function:
CREATE OR REPLACE FUNCTION
get_budget
RETURN number IS
v_yearly_budget NUMBER;
BEGIN
SELECT yearly_budget INTO
v_yearly_budget
FROM studio WHERE id = v_studio_id;
RETURN v_yearly_budget;
END;
What additional code is needed to
compile this function successfully?
- Remove the first RETURN statement.
- Change "RETURN number" to "OUTPUT number".
- Add "(v_studio_id IN NUMBER)" right after the IS keyword.
- Add "(v_studio_id IN NUMBER)" right before the RETURN statement of the header.
26. Which subprogram type can be invoked from
within a SQL statement?
A.
function
- procedure
- public packaged procedure
- private packaged procedure
- private packaged function
- Examine this function:
CREATE OR REPLACE FUNCTION
get_budget
(v_studio_id IN NUMBER) RETURN
number IS
v_yearly_budget NUMBER;
BEGIN
SELECT yearly_budget INTO
v_yearly_budget
FROM studio WHERE id = v_studio_id;
END;
To execute this function
successfully, what additional code must be added to the executable section?
- RETURN;
- RETURN get_budget;
- OUT v_yearly_budget;
- RETURN v_yearly_budget;
28. While creating a package, you placed the
function name in the specification and the body. Which type of construct have
you created?
A.
public
- illegal
- private
- one-time only
- Examine this code:
CREATE OR REPLACE PACKAGE prod_pack
IS
g_tax_rate NUMBER := .08;
END prod_pack;
Which statement about this code is
true?
- This package specification can exist without a body.
- This package body can exist without a specification.
- This package body cannot exist without a specification.
- This package specification cannot exist without a body.
- Examine this package specification:
CREATE OR REPLACE PACKAGE
theater_package IS
PROCEDURE find_cpt
(v_movie_id IN NUMBER,
v_cost_per_ticket IN OUT NUMBER);
PROCEDURE update_theater (v_name IN
VARCHAR2);
PROCEDURE find_seats_sold
(v_movie_id IN NUMBER DEFAULT 34,
v_theater_id IN NUMBER);
PROCEDURE add_theater;
END theater_package;
Which statement about the procedures
in this specification is true?
- They are public procedures.
- They are private procedures.
- Each procedure is missing code and, therefore, is illegal.
- Each procedure contains an argument list and, therefore, is illegal.