Pagini

duminică, 10 februarie 2013

PLSQL Mid Term Exam Semester 1 - Part II

1. There are 12 distinct JOB_IDs in the EMPLOYEES table. You need to
write some PL.SQL code to fetch and display all the employees with aspecific JOB_ID. The chosen JOB_ID can be different each time the code isexecuted. 
What is the best way to do this? (1) Points 
Write 12 separate PL/SQL blocks, each declaring a cursor with adifferent JOB_ID in the WHERE clause. 
Write a single PL/SQL block which declares 12 cursors, one for eachdistinct value of JOB_ID. 
Write a single PL/SQL block which declares one cursor using aparameter for the JOB_ID. (*) 
Write a single PL/SQL block which uses a cursor to fetch all theemployee rows, with an IF statement to decide which of the fetched rowsto display. 

2. What is one of the advantages of using parameters with a cursor? (1) Points 
You can use a cursor FOR loop. 
You can declare the cursor FOR UPDATE. 
You do not need to DECLARE the cursor at all. 
You can use a single cursor to fetch a different set of rows eachtime the cursor is opened. (*) 
It will execute much faster than a cursor without parameters. 

3. What will be the value of v_result after the following code isexecuted? 
DECLARE 
v_grade CHAR(1) := NULL;
v_result VARCHAR2(10);
BEGIN 

CASE v_grade
WHEN 'A' THEN v_result := 'Very Good';
WHEN 'F' THEN v_result := 'Poor';
ELSE v_result := 'In Between';

END;
END; 
(1) Points 

Poor 
In Between (*) 
Null 
Very Good 

4. What will be the value of variable c after the following code isexecuted? 
DECLARE 
a BOOLEAN := TRUE;
b BOOLEAN := FALSE;
c NUMBER;

BEGIN 
c := CASE 
WHEN a AND b THEN 10 
WHEN NOT a THEN 20 
WHEN a OR b THEN 30 
ELSE 40 

END;
END; 
(1) Points 

30 (*) 
20 
40 
10 

5. You want to assign a value to v_result which depends on the valueof v_grade: if v_grade = 'A' set v_result to 'Very Good' and so on.
DECLARE 
v_grade CHAR(1);
v_result VARCHAR2(10); 

BEGIN 
v_result := 
CASE v_grade 
v_result := 
CASE v_grade 

The next line should be 
(1) Points 
WHEN v_grade = 'A' THEN 'Very Good' 
WHEN 'A' THEN 'Very Good'; 
WHEN 'A' THEN v_result := 'Very Good'; 
WHEN 'A' THEN 'Very Good' (*) 

6. Which of the following is NOT a characteristic of a CASE statement? (1) Points 
It ends with END CASE; 
It can be a complete PL/SQL block 
It returns a value (*) 
It evaluates a condition and performs an action 

7. What will be the value of v_sal_desc after the following code isexecuted? 
DECLARE 
v_salary NUMBER(6,2) := NULL;
v_sal_desc VARCHAR2(10); 

BEGIN 

CASE 

WHEN v_salary < 10000 THEN v_sal_desc := 'Low Paid';

WHEN v_salary >= 10000 THEN v_sal_desc := 'High Paid';

END CASE;
END; 
(1) Points

High Paid 
Low Paid 
Null 
The code will fail and return an exception (*) 

8. Examine the following code:
DECLARE 
CURSOR c IS SELECT * FROM employees FOR UPDATE;
c_rec c%ROWTYPE;
BEGIN 
OPEN c;
FOR i IN 1..20 LOOP 
FETCH c INTO c_rec;
IF i = 6 THEN 
UPDATE employees SET first_name = 'Joe'WHERE CURRENT OF c;
END IF;
END LOOP;
CLOSE c;
END;
Which employee row or rows will be updated when this block is executed?
(1) Points 

The first 6 fetched rows will be updated. 
No rows will be updated because you locked the rows when the cursor was opened. 
The 6th fetched row will be updated. (*) 
The block will not compile because the cursor should have beendeclared .... FOR UPDATE WAIT 5; 
None of the above. 

9. Consider the following cursor: 
CURSOR c IS 
SELECT e.last_name, e.salary, d.department_name
FROM employees e JOIN departments d
USING(department_id)
WHERE e.last_name='Smith' 
FOR UPDATE; 
When the cursor is opened and rows are fetched, what is locked?
(1) Points 

The whole EMPLOYEES table is locked. 

In the EMPLOYEES table, only the 'Smith' rows are locked. Nothing inthe DEPARTMENTS table is locked. 

Each 'Smith' row is locked and Smith's matching rows in DEPARTMENTSare locked. No other rows are locked in either table. (*) 

The whole EMPLOYEES and DEPARTMENTS tables are locked. 
Nothing is locked because the cursor was not declared with NOWAIT. 

10. User MARY has locked a row of the EMPLOYEES table. Now, user SAEEDtries to open the following cursor:
CURSOR c IS 
SELECT * FROM employeesFOR UPDATE WAIT 5;
What will happen when SAEED's session tries to fetch the row that MARY 
has locked? (1) Points 
SAEED's session successfully fetches the first 5 rows and then waitsindefinitely to fetch the 6th row. 
SAEED's session waits for 5 seconds, and then raises an exception ifMARY has not unlocked the row. (*) 
SAEED's session waits for 5 seconds, then SAEED is disconnected fromthe database. 
SAEED's session waits for 5 seconds, then MARY's session is rolledback. 
SAEED's session waits for 5 minutes, and then raises an exception ifMARY has not unlocked the row. 

11. Examine the following code fragment:
DECLARE 
CURSOR emp_curs ISSELECT first_name, last_name FROM employees;
v_emp_rec emp_curs%ROWTYPE;
BEGIN 

... 

FETCH emp_curs INTO v_emp_rec;

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

... 

To display the fetched last name, what should you code at Point A?
(1) Points 

v_emp_rec.last_name (*) 
v_emp_rec(last_name) 
v_emp_rec 
last_name 
None of the above 

12. The following cursor has been declared:
CURSOR emp_curs IS
SELECT first_name, last_name, job_id, salary
FROM employees; 

Which of the following correctly declares a composite record with thesame structure as the cursor? 
(1) Points 

emp_rec emp_rec%ROWTYPE; 
emp_rec emp_curs%TYPE; 
emp_rec emp_curs%ROWTYPE; (*) 
emp_rec cursor%ROWTYPE; 

13. The employees table contains 11 columns. The following blockdeclares a cursor and a record based on the cursor: 
DECLARE 
CURSOR emp_curs IS
SELECT * FROM employees;
v_emp_rec emp_curs%ROWTYPE; 

A twelfth column is now added to the employees table. Which of thefollowing statements is true?
(1) Points 

The declaration of emp_rec must be changed to add an extra field. 
The block will still work correctly without any changes to the PL/SQLcode. (*) 
The block will fail and an INVALID_CURSOR exception will be raised. 
An extra scalar variable must be declared to correspond to thetwelfth table column. 

14. Which of the following cursor attributes evaluates to TRUE if thecursor is open? (1) Points 
%ISOPEN (*) 
%NOTFOUND 
%FOUND 
%ROWCOUNT 

15. The DEPARTMENTS table contains four columns. Examine the followingcode: 
DECLARE 
CURSOR dept_curs IS 
SELECT * FROM departments;
v_dept_rec dept_curs%ROWTYPE;

BEGIN 
OPEN dept_curs;
FETCH dept_curs INTO v_dept_rec; 

... 

Which one of the following statements is true?
(1) Points 

v_dept_rec contains the first four rows of the departments table. 
The FETCH will fail because the structure of v_dept_rec does not match the structure of the cursor. 
v_dept_rec contains the first row of the departments table. (*) 
The block will fail because the declaration of v_dept_rec is invalid. 

16. Assume that you have declared a cursor called C_EMP. Which of thefollowing statements about C_EMP is correct? (Choose two.) (1) Points (Choose all correct answers) 
You can use c_emp%NOTFOUND to exit a loop. (*) 
You can fetch rows when c_emp%ISOPEN evaluates to FALSE. 
You can use c_emp%ROWCOUNT to return the number of rows returned by the cursor so far. (*) 
You can use c_emp%FOUND after the cursor is closed. 

17. Which of the following is a good reason to declare and usemultiple cursors in a single PL/SQL block? 
(1) Points 
Multiple cursors improve performance. They are faster than using asingle cursor. 
Multiple cursors use less memory than a single cursor. 
Multiple cursors allow us to fetch rows from two or more relatedtables without using a JOIN. (*) 
Multiple cursors are the only way to use cursors with parameters. 
Multiple cursors can be opened many times, while a single cursor canbe opened only once. 

18. What is wrong with the following code?
DECLARE 
CURSOR emp_curs(p_dept_id NUMBER) ISSELECT * FROM employees WHERE department_id = p_dept_id;
BEGIN 
FOR dept_rec IN (SELECT * FROM departments) LOOPDBMS_OUTPUT.PUT_LINE(dept_rec.department_name);
FOR emp_rec IN emp_curs(dept_rec.department_id) LOOPDBMS_OUTPUT.PUT_LINE(emp_rec.last_name);
END LOOP;
END LOOP;
END;
(1) Points
The DEPARTMENTS cursor must be declared with a parameter. 

You cannot use a cursor with a subquery in nested loops. 

You cannot use two different kinds of loop in a single PL/SQL block. 

EMP_CURS should not be DECLAREd explicitly; it should be coded as asubquery in a cursor FOR loop. 

Nothing is wrong. The block will execute successfully and display alldepartments and the employees in those departments. (*) 


19. You want to display each row from the DEPARTMENTS table, andimmediately underneath it, a list of all EMPLOYEES in that department.
Which of the following is a good way to do this? (1) Points
Use a single cursor, declared as SELECT * FROM employees GROUP BYdepartment_id; 

Use two cursors, one for each of the two tables. Declare the EMPLOYEES cursor with a parameter for the DEPARTMENT_ID. (*) tables. Declare the EMPLOYEES cursor with a parameter for the DEPARTMENT_ID. (*) 

Write a SELECT statement which JOINs the two tables, and use CONNECTBY PRIOR and LEVEL to display the rows in the correct order. 

Use a single cursor with a cursor FOR loop. 

Change the physical model so that all employee and department data isin a single table. 

20. Which kind of loop is this?
v_count := 1;
LOOP 
v_count := v_count + 1;
EXIT WHEN i > 20;
END LOOP; 
(1) Points 

FOR loop 
IF-THEN loop 
Basic loop (*) 
WHILE loop 
CASE loop 

21. A PL/SQL block contains the following code:
v_counter := 1; 
LOOP 
EXIT WHEN v_counter=5;EXIT WHEN v_counter=5;

END LOOP;

v_counter := v_counter + 1;

What is the value of V_COUNTER after the loop is finished?
(1) Points 
This is an infinite loop; the loop will never finish. (*) 

22. Examine the following block:
DECLARE 
v_counter PLS_INTEGER := 1;
BEGIN 
LOOP 
DBMS_OUTPUT.PUT_LINE(v_counter);
v_counter := v_counter + 1;

EXIT WHEN v_counter = 5;
END LOOP;
END; 

What is the last value of V_COUNTER that is displayed?
(1) Points 
4 (*) 
This is an infinite loop; the loop will never finish. 

23. The EXIT statement can be located anywhere inside a basic loop.
True or False? (1) Points 
True (*) 
False 

24. A PL/SQL block contains the following code:
v_counter := 1;
LOOP 
EXIT WHEN v_counter = 5;
v_counter := v_counter + 1;
END LOOP; 

What is the value of V_COUNTER after the loop is finished?
(1) Points 
5 (*) 
This is an infinite loop; the loop will never finish. 

25. In the following code fragment, you want to exit from the outer 
loop at Line A if v_number = 6. Which statement would you write on LineA? 
<<big_loop>>
WHILE condition_1 LOOP 
<<small_loop>>

FOR i IN 1..10 LOOP 
DBMS_OUTPUT.PUT_LINE(i);
--Line A 

END LOOP;
END LOOP; 
(1) Points 

IF v_number = 6 THEN EXIT; 
EXIT outer_loop WHEN v_number = 6; 
EXIT big_loop WHEN v_number = 6; (*) 
EXIT small_loop WHEN v_number = 6; 

26. Examine the following code:
DECLARE 
v_outer_count NUMBER := 1;
v_inner_count NUMBER := 1;

BEGIN 
LOOP 

LOOP 
v_inner_count := v_inner_count + 1;
EXIT WHEN v_inner_count > 5; --Line A 

END LOOP; 
v_outer_count := v_outer_count + 1;
EXIT WHEN v_outer_count > 3;

END LOOP;
END; 

What happens at Line A when the value of V_INNER_COUNT equals 6? (1) Points
Both loops are exited and the block's execution is terminated. 
The inner loop is exited but the outer loop continues execution. (*) 
The outer loop is exited but the inner loop continues execution. 
An error condition is returned. 

27. What will happen when the following code is executed?
DECLARE CURSOR emp_curs ISSELECT salary FROM employees;
v_salary employees.salary%TYPE;

BEGIN 
OPEN emp_curs;
FETCH emp_curs INTO v_salary;
CLOSE emp_curs;
FETCH emp_curs INTO v_salary;

END; 
(1) Points 

The block will fail and an INVALID_CURSOR exception will be raised.(*) 
The first employee row will be fetched twice. 
The first two employee rows will be fetched. 
The block will fail and a TOO_MANY_ROWS exception will be raised. 

28. What will happen when the following code is executed?
DECLARE 
CURSOR emp_curs ISSELECT salary FROM employees;
v_salary employees.salary%TYPE;
BEGIN 
FETCH emp_curs INTO v_salary;
DBMS_OUTPUT.PUT_LINE(v_salary);
CLOSE emp_curs;

END; 
(1) Points
The first employee's salary will be fetched and displayed. 
All employees' salaries will be fetched and displayed. 
The execution will fail and an error message will be displayed. (*) 
The lowest salary value will be fetched and displayed. 

29. The employees table contains 20 rows. What will happen when thefollowing code is executed?
DECLARE 
CURSOR emp_curs IS
SELECT job_id FROM employees;
v_job_id employees.job_id%TYPE;

BEGIN 
OPEN emp_curs;
LOOP 

FETCH emp_curs INTO v_job_id;
DBMS_OUTPUT.PUT_LINE(v_job_id);
EXIT WHEN emp_curs%NOTFOUND;


END LOOP;
CLOSE emp_curs;
END; 
(1) Points 

20 job_ids will be displayed. 
The block will fail and an error message will be displayed. 
21 rows of output will be displayed; the first job_id will bedisplayed twice. displayed twice. 
21 rows of output will be displayed; the last job_id will bedisplayed twice. (*) 

30. Which of these constructs can be used to fetch multiple rows froma cursor's active set? (1) Points 
A CASE statement 
An IF .... ELSE statement 
A basic loop which includes FETCH and EXIT WHEN statements (*) 
A basic loop which includes OPEN, FETCH and CLOSE statements 

31. Which of these is NOT a valid cursor declaration? (1) Points 

CURSOR emp_curs IS 
SELECT salary
FROM employees
ORDER BY salary DESC; 

CURSOR emp_curs IS
SELECT salary
FROM employees
WHERE last_name LIKE 'S%'; 

CURSOR emp_dept_curs IS
SELECT e.salary, d.department_name 
FROM employees e, departments d
WHERE e.department_id = d.department_id; 

CURSOR emp_curs IS
SELECT salary INTO v_salary
FROM employees;(*) 

32. Which of these statements about implicit cursors is NOT true? (1) Points 
They are declared automatically by Oracle for single-row SELECT statements. 
They are declared automatically by Oracle for all DML statements. 
They are declared by the PL/SQL programmer. (*) 
They are opened and closed automatically by Oracle. 

33. An implicit cursor can be used for a multiple-row SELECT statement. True or False? (1) Points 
True 
False (*) 

34. An explicit cursor must always be declared, opened and closed bythe PL/SQL programmer. True or False? (1) Points 
True 
False (*) 

35. Examine the following code:
DECLARE 
a BOOLEAN := TRUE;
b BOOLEAN := FALSE;
c BOOLEAN := TRUE;
d BOOLEAN := FALSE;
game char(4) := 'lost';

BEGIN 
IF ((a AND b) AND (c OR d))
THEN game := 'won';
END IF;

What is the value of GAME at the end of this block? 
(1) Points 

NULL 
won' 
lost' (*) 
False 

36. What is the correct name for CASE, LOOP, WHILE, and IF-THEN-ELSE 
structures ? (1) Points 
Control structures (*) 
Array structures 
Memory structures 
Cursor structures 

37. You need to execute a set of statements 10 times, increasing acounter by 1 each time. Which of the following PL/SQL constructs can dothis? (Choose three) (1) Points (Choose all correct answers) 
IF ... THEN ... ELSE 
A WHILE loop (*) 
CASE ... WHEN ... THEN 
A FOR loop (*) 
A basic loop (*) 

38. What is the correct form of a simple IF statement? (1) Points 
IF condition THEN statement; 

IF condition THEN statement;
END IF; (*) 

IF condition;
THEN statement;
END IF; 

IF condition 
THEN statement 
ENDIF; 

39. Which statement best describes when a FOR loop should be used? (1) Points 
When the number of iterations is known (*) 
When testing the value in a Boolean variable 
When the controlling condition must be evaluated at the start of eachiteration 

40. When using a counter to control a FOR loop, which of the followingis true ? (1) Points 

You must have exactly one counter but it is implicitly declared. (*) 
You must have exactly one counter and you must explicitly declare it. 
You can have multiple counters, but you need at least one. 
You don't need a counter; you can test for anything (for example,
whether a BOOLEAN is TRUE or FALSE). 

41. Which of the following blocks produces the same output as thisblock? 
BEGIN 
FOR i in 1 .. 3 LOOP 
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END; 
(1) Points 

DECLARE 
i PLS_INTEGER := 0;
BEGIN 
WHILE i<3 LOOP 
DBMS_OUTPUT.PUT_LINE(i);
i := i + 1;
END LOOP;
END; 

DECLARE 
i PLS_INTEGER := 0;
BEGIN 
WHILE i<3 LOOP 
i := i + 1;
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;(*) 

DECLARE 
i PLS_INTEGER := 0;
BEGIN 
WHILE i<3 LOOP 
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
i := i+ 1;
END; 

42. In a WHILE loop, the statements inside the loop must execute atleast once. True or False? (1) Points 
True 
False (*) 

43. What will happen when the following code is executed?
BEGIN 
FOR i in 1 ..3 LOOP 
DBMS_OUTPUT.PUT_LINE (i);

i := i + 1;
END LOOP;
END; 
(1) Points 

It will display 1, 2, 3. 
It will display 2, 3, 4. 
It will result in an error because you cannot modify the counter in aFOR loop. (*) 
It will result in an error because the counter was not explicitlydeclared. 

44. Which statement best describes when a WHILE loop shouild be used? (1) Points 
When the number of iterations is known 
When repeating a sequence of statements until the controllingcondition is no longer true (*) 
When assigning a value to a Boolean variable 
When testing whether a variable is null 

45. There are no employees in department_id 75.
Which of the following is NOT a valid cursor FOR loop with a subquery? (1) Points 

FOR emp_rec IN
(SELECT last_name, salary FROM employees) LOOP ... 

FOR emp_rec IN
(SELECT * FROM employees) LOOP ... 

FOR emp_rec IN
(SELECT last_name, salary FROM employees ORDER BY last_name) LOOP ... 

FOR emp_rec IN
(SELECT * FROM employees WHERE department_id = 75) LOOP ... 

None of the above. They are all valid. (*) 

46. Examine the following code. To display the salary of an employee,
what must be coded at Point A? 
DECLARE 
CURSOR emp_curs IS SELECT * FROM employees;
BEGIN 
FOR emp_rec IN emp_curs LOOPDBMS_OUTPUT.PUT_LINE( --what goes here ? );
END LOOP;
END; 
(1) Points 

salary 
emp_curs.salary 
emp_rec.salary (*) 
employees.salary 
emp_rec.salary IN emp_curs 

47. The following code fragment shows a cursor FOR loop:
FOR emp_record IN emp_cursor LOOP ...... 
Which of the following do NOT need to be coded explicitly? (Choosethree.) (1) Points (Choose all correct answers) 

OPEN emp_cursor; (*) 
DECLARE CURSOR emp_cursor IS ... 
emp_record emp_cursor%ROWTYPE; (*) 
FETCH emp_cursor INTO emp_record; (*) 
END LOOP; 

Niciun comentariu:

Trimiteți un comentariu