Pagini

marți, 12 februarie 2013

PLSQL Semester 1 Mid Term Exam - Part II


Section 4
     
1.  When using a counter to control a FOR loop, which of the following is 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).
 
2.  Examine the following code:
DECLARE
v_bool BOOLEAN := FALSE;
v_counter NUMBER(4) := 0;
BEGIN
... Line A
...
END;
Which of the following is NOT valid at line A?
(1) Points
     
    WHILE NOT v_boolean LOOP
    WHILE v_boolean AND v_counter < 6 LOOP
    WHILE v_counter > 8 LOOP
    WHILE v_counter IN 1..5 LOOP (*)

3.  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 a FOR loop. (*)
    It will result in an error because the counter was not explicitly declared.
 
4.  In a WHILE loop, the statements inside the loop must execute at least once. True or False?  (1) Points
     
    True
    False (*)

5.  Which of the following blocks produces the same output as this block?
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;

6.  What is the correct form of a compound IF statement?  (1) Points
     
    IF condition
THEN statement1
ELSE statement 2;

    IF condition
THEN statement1
ELSE statement 2;
END IF;
 
    IF condition;
THEN statement1;
ELSE statement2;
END IF;
 
    IF condition THEN statement1;
ELSE statement2;
END IF; (*)

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

8.  Examine the following code:
DECLARE
    a VARCHAR2(6) := NULL;
    b VARCHAR2(6) := NULL;
BEGIN
    IF a = b THEN
       DBMS_OUTPUT.PUT_LINE('EQUAL');
    ELSIF a != b THEN
       DBMS_OUTPUT.PUT_LINE('UNEQUAL');
    ELSE
       DBMS_OUTPUT.PUT_LINE('OTHER');
    END IF;
END;
Which word will be displayed?
 (1) Points
     
    UNEQUAL
    EQUAL
    Nothing will be displayed
    OTHER (*)

9.  What type of control structures are repetition statements that enable you to execute statements in a PLSQL block repeatedly?  (1) Points
     
    IF statements
    Loops (*)
    CASE expressions
    CASE statements
 
10.  What is the correct name for CASE, LOOP, WHILE, and IF-THEN-ELSE structures ?  (1) Points
     
    Control structures (*)
    Array structures
    Memory structures
    Cursor structures
     
11.  Examine the following code:
DECLARE
    v_salary NUMBER(6);
    v_constant NUMBER(6) := 15000;
    v_result VARCHAR(6) := 'MIDDLE';
BEGIN
    IF v_salary != v_constant THEN
        v_result := 'HIGH';
    ELSE
        v_result := 'LOW';
    END IF;
END;
What is the final value of v_result?
(1) Points
     
    HIGH
    LOW (*)
    MIDDLE
    Null
 
12.  You need to execute a set of statements 10 times, increasing a counter by 1 each time. Which of the following PL/SQL constructs can do this? (Choose three)  (1) Points (Choose all correct answers)
     
    IF ... THEN ... ELSE
    A WHILE loop (*)
    CASE ... WHEN ... THEN
     A FOR loop (*)
     A basic loop (*)
 
 13.  Which one of these tasks is best done using a LOOP statement?  (1) Points
     
    Assigning a letter grade to a numerical score
    Calculating and displaying the sum of all integers from 1 to 100 (*)
    Testing if a condition is true, false or null
    Fetching and displaying an employee's last name from the database
 
14.  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 (*)
    6
    1
    This is an infinite loop; the loop will never finish.
 
15.  Which one of these is NOT a kind of loop?  (1) Points
     
    ASCENDING loop (*)
    FOR loop
    Basic loop
    WHILE loop

16.  What kind of statement is best suited for displaying the multiplication table for "sixes": 6x1=6, 6x2=12 ... 6x12=72?  (1) Points
     
    CASE expression
    IF statement
    CASE statement
    LOOP statement (*)
   
17.  The EXIT statement can be located anywhere inside a basic loop. True or False?  (1) Points
     
    True (*)
    False

18.  What will be the value of v_sal_desc after the following code is executed?
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
 
19.  What value will v_answer contain after the following code is executed?
DECLARE
    v_age NUMBER:= 18;
    v_answer VARCHAR2(10);
BEGIN
    v_answer :=
       CASE
          WHEN v_age < 25 THEN 'Young'
          WHEN v_age = 18 THEN 'Exactly 18'
          ELSE 'Older'
       END CASE;
END;
(1) Points
     
    Exactly 18
    Young (*)
    Null
    Older
 
20.  What will be the value of variable c after the following code is executed?
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 CASE;
END;
(1) Points
     
    30 (*)
    20
    40
    10

21.  What will be the value of variable c after the following code is executed?
DECLARE
    a BOOLEAN := TRUE;
    b BOOLEAN := NULL;
    c NUMBER;
BEGIN
    IF a AND b THEN c := 2;
       ELSIF a OR b THEN c := 0;
       ELSE c := 1;
    END IF;
END;
(1) Points
     
    1
    Null
    0 (*)
    2

22.  What will be the value of v_result after the following code is executed?
DECLARE
    v_grade CHAR(1) := NULL;
    v_result VARCHAR2(10);
BEGIN
    CASE v_grade
       WHEN ('A' OR 'B') THEN v_result := 'Very Good';
       WHEN ('E' OR 'F') THEN v_result := 'Poor';
       ELSE v_result := 'In Between';
    END CASE;
END;
(1) Points
     
    Poor
    In Between (*)
    Null
    Very Good
 
 23.  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.
 
24.  Examine the following code:
BEGIN
   FOR i IN 1..5 LOOP
     FOR j IN 1..8 LOOP
       EXIT WHEN j = 7;
       DBMS_OUTPUT.PUT_LINE(i || j);
     END LOOP;
   END LOOP;
END;
How many lines of output will be displayed when this code is executed?
 (1) Points
     
    35
    6
    30 (*)
    40

25.  What clause will leave the outer loop at Point A?
DECLARE
  i INTEGER := 0;
BEGIN
   << i_loop >>
  WHILE i <= 10 LOOP
    i := i+1;
     << j_loop >>
    FOR j IN 1..5 LOOP
      _______ WHEN i = j*2; -- Point A
      DBMS_OUTPUT.PUT_LINE(i || j);
    END LOOP;
  END LOOP;
END;
(1) Points
     
    EXIT <<outerloop>>
    EXIT outerloop
    EXIT i_loop (*)
    EXIT j_loop
 
26.  What kinds of loops can be nested?  (1) Points
     
    BASIC loops
    WHILE loops
    FOR loops
    All of the above (*)
     
27.  User TOM has locked a row in the WORKERS table. Now, user DICK wants to open the following cursor:
CURSOR c IS
  SELECT * FROM workers FOR UPDATE NOWAIT;
What will happen when DICK opens the cursor and tries to fetch rows?
 (1) Points
     
    TOM's session is rolled back. DICK's session successfully fetches rows from the cursor.
    DICK's session waits indefinitely.
    Both sessions wait for a few seconds; then the system breaks all locks and both sessions raise an exception.
    DICK's session immediately raises an exception. (*)
    The c%NOWAIT attribute is set to TRUE.
 
28.  You want to declare a cursor which locks each row fetched by the cursor. Examine the following code:
DECLARE
  CURSOR emp_curs IS
    SELECT * FROM employees
    FOR -- Point A
Which of the following can NOT be coded at Point A?
 (1) Points
     
    UPDATE;
    UPDATE OF salary;
    UPDATE OF employees; (*)
    UPDATE NOWAIT;
 
29.  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 in the DEPARTMENTS table is locked.
    Each 'Smith' row is locked and Smith's matching rows in DEPARTMENTS are 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.
 
30.  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 a specific JOB_ID. The chosen JOB_ID can be different each time the code is executed.
What is the best way to do this?  (1) Points
     
    Write 12 separate PL/SQL blocks, each declaring a cursor with a different JOB_ID in the WHERE clause.
 
    Write a single PL/SQL block which declares 12 cursors, one for each distinct value of JOB_ID.
 
    Write a single PL/SQL block which declares one cursor using a parameter for the JOB_ID. (*)
 
    Write a single PL/SQL block which uses a cursor to fetch all the employee rows, with an IF statement to decide which of the fetched rows to display.
 
31.  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 each time the cursor is opened. (*)
    It will execute much faster than a cursor without parameters.

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.  Examine the following code:
DECLARE
    CURSOR emp_curs IS
       SELECT last_name, salary
       FROM employees
       ORDER BY salary;
    v_last_name employees.last_name%TYPE;
    v_salary employees.salary%TYPE;
BEGIN
...
Which of the following statements successfully opens the cursor and fetches the first row of the active set?
 (1) Points
     
    OPEN emp_curs;
FETCH emp_curs INTO v_last_name, v_salary; (*)

     OPEN emp_curs;
FETCH emp_curs INTO v_salary, v_last_name;

     OPEN emp_curs;
FETCH FIRST emp_curs INTO v_last_name, v_salary;

     OPEN emp_curs;
FETCH emp_curs;

34.  Place the following statements in the correct sequence:

OPEN my_curs;
CLOSE my_curs;
CURSOR my_curs IS SELECT my_column FROM my_table;
FETCH my_curs INTO my_variable;
(1) Points
     
    C,D,A,B
    C,A,D,B (*)
    A,C,D,B
    C,A,B,D
 
35.  An explicit cursor must always be declared, opened and closed by the PL/SQL programmer. True or False?  (1) Points
     
    True
    False (*)
 
36.  The employees table contains 20 rows. What will happen when the following 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 be displayed twice.
    21 rows of output will be displayed; the last job_id will be displayed twice. (*)
     Incorrect. Refer to Section 5 Lesson 1.
   
37.  For which type of SQL statement must you use an explicit cursor?  (1) Points
     
    DML statements that process more than one row.
    Queries that return more than one row. (*)
     Data Definition Language (DDL) statements.
     Queries that return a single row.
 
38.  An implicit cursor can be used for a multiple-row SELECT statement. True or False?  (1) Points
     
    True
    False (*)
 
39.  What will happen when the following code is executed?
DECLARE CURSOR emp_curs IS
    SELECT 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.
 
40.  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; (*)

  41.  What is wrong with the following code?
DECLARE
    CURSOR dept_curs IS SELECT * FROM departments;
BEGIN
    FOR dept_rec IN dept_curs LOOP
       DBMS_OUTPUT.PUT_LINE(dept_curs%ROWCOUNT || dept_rec.department_name):
    END LOOP;
    DBMS_OUTPUT.PUT_LINE(dept_rec.department_id);
END;
(1) Points
     
    The cursor DEPT_CURS has not been opened.
    The implicitly declared record DEPT_REC cannot be referenced outside the cursor FOR loop. (*)
    You cannot use %ROWCOUNT with a cursor FOR loop.
    The cursor DEPT_CURS has not been closed.
    Nothing is wrong, this code will execute successfully.
     Incorrect. Refer to Section 5 Lesson 3.
 
42.  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 LOOP
       DBMS_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

43.  What is wrong with the following code?
BEGIN
    FOR emp_rec IN
       (SELECT * FROM employees WHERE ROWNUM < 10
          FOR UPDATE NOWAIT) LOOP
        DBMS_OUTPUT.PUT_LINE(emp_rec%ROWCOUNT || emp_rec.last_name):
    END LOOP;
END;
(1) Points
     
    You cannot use FOR UPDATE NOWAIT with a cursor FOR loop using a subquery.
    You cannot reference %ROWCOUNT with a cursor FOR loop using a subquery. (*)
    The field EMP_REC.LAST_NAME does not exist.
    You cannot use ROWNUM with a cursor FOR loop.
    The cursor has not been opened.

44.  The DEPARTMENTS table contains four columns. Examine the following code:
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.

45.  Which of the following cursor attributes is set to the total number of rows returned so far?  (1) Points
     
    %ISOPEN
    %NOTFOUND
    %FOUND
    %ROWCOUNT (*)

46.  Examine the following code fragment:
DECLARE
    CURSOR emp_curs IS
       SELECT 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

47.  The employees table contains 11 columns. The following block declares 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 the following 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/SQL code. (*)
    The block will fail and an INVALID_CURSOR exception will be raised.
    An extra scalar variable must be declared to correspond to the twelfth table column.
 
48.  You want to display all locations, and the departments in each location. Examine the following code:
DECLARE
  CURSOR loc_curs IS SELECT * FROM locations;
  CURSOR dept_curs(p_loc_id NUMBER) IS
    SELECT * FROM departments WHERE location_id = p_loc_id;
BEGIN
  FOR loc_rec IN loc_curs LOOP
    DBMS_OUTPUT.PUT_LINE(loc_rec.city);
    FOR dept_rec IN dept_curs(-- Point A --) LOOP
      DBMS_OUTPUT.PUT_LINE(dept_rec.department_name);
    END LOOP;
  END LOOP;
END;
What should you code at Point A?
(1) Points
     
    p_loc_id
    location_id
    null
    LOOP ... END LOOP;
    loc_rec.location_id (*)
 
49.  Which of the following is a good reason to declare and use multiple cursors in a single PL/SQL block?  (1) Points
     
    Multiple cursors improve performance. They are faster than using a single cursor.
    Multiple cursors use less memory than a single cursor.
    Multiple cursors allow us to fetch rows from two or more related tables 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 can be opened only once.
 
50.  When using multiple nested cursors, what kinds of loops can you use?  (1) Points
     
    Cursor FOR loops only.
    Basic loops only.
    WHILE loops only.
    None of the above.
    All of the above. (*)

Niciun comentariu:

Trimiteți un comentariu