0

Programming in PL/SQL (Basic Concepts)

Description: Imporove your PL/SQL Skills
Number of Questions: 25
Created by:
Tags: PL/SQL SQL.Oracle Database Programming Computers Java/Oracle /C/C++
Attempted 0/25 Correct 0 Score 0

How many sections or parts are there in a PL/SQL program?

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: C
Explanation:

There are three parts in a PL/SQL program. They are - declaration section (where the variables are declared), begin section (where executable code is written) and exception section (where the exceptions are handled). The declaration and exception sections are optional but the begin part is compulsory. A PL/SQL program ends with an 'end' statement.

A group of related data items stored in fields, each with its own name and datatype, is called

  1. varray

  2. nested table

  3. record

  4. index-by tables


Correct Option: C
Explanation:

A PL/SQL record is a group of related data items. For example, the data about a student like roll no., name, date of birth are logically related to each other but have different types. A record containing a field for each item lets the user treat the data as a logical unit. Thus, making it easier to organise and represent. The syntax for creating a record is

type type_name is record (fielddeclaration1,fielddeclaration2...); 

Which of the following is NOT a valid loop in PL/SQL?

  1. For loop.. end loop

  2. loop..end loop

  3. While loop.. end loop

  4. Do while loop..end loop


Correct Option: D
Explanation:

There is no 'Do while' loop in PL/SQL. To write conditional loops, we can either use the 'simple' or the 'while' loop.

When a 'Select' command is written inside a PL/SQL program, the ____ keyword must be included in it.

  1. where

  2. in

  3. group by

  4. into


Correct Option: D
Explanation:

All PL/SQL 'select' statements must contain the 'into' keyword. The 'into' keyword is used to copy the values returned by a select statement into variables. For example, the statement - 'select bookno, bookname, price into bkno, bkname,bkprice from books where title='Oracle''- will copy the values of bookno, bookname and price columns in the variables bkno, bkname and bkprice, respectively. The 'into' clause is not used when the 'select' statement is used at the SQL prompt.

Which of the following is NOT a valid cursor attribute?

  1. %ISOPEN

  2. %EXISTS

  3. %FOUND

  4. %ROWCOUNT


Correct Option: B
Explanation:

The %exists is not a valid cursor attribute. If used with a cursor name, it will result in compilation error.

Which of the following is a schema object that groups logically related PL/SQL types, items and sub-programs?

  1. Trigger

  2. Procedure

  3. Function

  4. Package


Correct Option: D
Explanation:

A package is a collection of related PL/SQL objects like procedures, functions and triggers created as a single unit. It is used to store related objects in a single file. A package has two parts - Package specification and package body. The specification part contains the declaration of procedure, functions and triggers whereas the body part contains the actual implementation or code of all the subprogram declared in specification part.

For which of the following SQL statements can a trigger not be written?

  1. Alter

  2. Update

  3. Delete

  4. Insert


Correct Option: A
Explanation:

A trigger is a PL/SQL block structure, which is fired when DML statements like Insert, Delete, Update are executed on a database table. 'Alter' is not a DML statement but a DDL statement that is used to alter or change the structure of a table like adding, dropping and resizing a column. We cannot write a trigger for a DDL command.

In PL/SQL, which of the following is the correct syntax of the 'if-else' statement?

  1. If condition then statements elseif condition then statements else statements end if;

  2. if condition statements elseif condition statements else statements end if;

  3. if condition then statements elif condition then statements else statements end if;

  4. if condition statements elsif condition statements else statements endif;


Correct Option: A
Explanation:

'If-else' is a condition statement that is used to write conditional statement in a PL/SQL program. For example, the following code will print a message depending on whether the value of A is greater, B is greater or both of them have the same values - if a>b then      dbms_output.put_line('A is greater'); elsif b>a then      dbms_output.put_line('B is greater'); else      dbms_output.put_line('Both are equal'); end if;

Which of the following is NOT a correct way of passing parameters to a function or a procedure in PL/SQL?

  1. IN

  2. OUT

  3. INOUT

  4. OUTIN


Correct Option: D
Explanation:

There is no OUTIN parameter in PL/SQL. Therefore, the program will return an error.

Which named exception is thrown when an attempt is made to open a cursor that is already open?

  1. INVALID_CURSOR

  2. OPEN_CURSOR

  3. CURSOR_ALREADY_OPEN

  4. CURSOR_IS_OPEN


Correct Option: C
Explanation:

The CURSOR_ALREADY_OPEN exception is thrown when we are trying to open a cursor that is already open. A cursor cannot be opened twice without closing it first.

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

  1. Procedure can be called from SQL prompt, while function cannot be called from SQL prompt.

  2. Function can return a value, while procedure cannot return any value.

  3. Functions can have arguments, while procedure cannot have arguments.

  4. Function can call procedures while procedure cannot call functions.


Correct Option: B
Explanation:

The difference between a function and procedure is that a function can return a value to its calling function while a procedure cannot return a value. Function returns a value using the 'return' keyword. A function cannot return more than one value.

Which of the following statements is FALSE about packages?

  1. Packages are made up of two components: specification and body

  2. The package body contains the actual code.

  3. The package specification contains the actual code.

  4. Packages encapsulate related procedures, functions, etc. into one self-contained unit.


Correct Option: C
Explanation:

The specification part does not contain the actual executable code of the functions and procedures but just their declaration. The actual code is written in the body part.

Which of the following statements is FALSE regarding triggers?

  1. A trigger can be written for handling updates of a particular column.

  2. A trigger can be run either before or after the execution of an SQL statement.

  3. A trigger cannot be written on a view.

  4. A trigger cannot contains a condition.


Correct Option: C
Explanation:

A trigger can be written on a view if it is declared using the 'instead of' keyword. The difference between a normal trigger and an instead of trigger is that when a normal trigger is fired, both the triggers and the firing DML statement like Delete, Insert or Update is also executed. On the other hand, in case of an instead of trigger the DML statements only invoke the trigger, they are not executed. A normal trigger is written against a table whereas an instead of trigger is written on a view.

Which of the following is the correct method of declaring a constant in PL/SQL?

  1. Data constant number (3)=10

  2. Data constant number (3) := 10;

  3. Data constant number (3);

  4. Constant Data number (3) := 10;


Correct Option: B
Explanation:

A constant is a value used in a PL/SQL block that remains unchanged throughout the program. It is declared using the assignment operator ':='. We must assign a value to a constant at the time of declaration. If we do not assign a value to a constant while declaring it and try to assign a value in the execution section, we will get an error. 

Which of the following keywords is NOT present in a 'function declaration'?

  1. Declare

  2. Begin

  3. Return

  4. Exception


Correct Option: A
Explanation:

The declaration section of a PL/SQL program is used to contain all variable declarations. While using a function, the declare keyword is replaced by 'is' keyword. All the variables are declared inside the 'is' keyword. For example, CREATE OR REPLACE FUNCTION myfunc  RETURN VARCHAR(20); IS sname VARCHAR(20); BEGIN SELECT studname INTO sname FROM students WHERE rollno = 1; RETURN sname; END;

Which of the following is NOT a difference between SQL and PL/SQL ?

  1. SQL is declarative, while PL/SQL is procedural.

  2. SQL is used to write queries, whereas PL/SQL is used to code programs.

  3. SQL can be embedded within PL/SQL but a PL/SQL statement cannot be embedded in SQL.

  4. PL/SQL executes one statement at a time, whereas SQL is used to write a code block.


Correct Option: D
Explanation:

PL/SQL is used to write individual statement in the form of a unit or program. When the program is executed, all the statements written in a complete logic are executed. On the other hand, SQL statements are executed one at a time when the user presses enter key to run the statement.

In a PL/SQL 'case' statement, if all the given conditions are false, the statements written in the ____ block are executed.

  1. default

  2. else

  3. case

  4. otherwise


Correct Option: B
Explanation:

The 'case' statement is a conditional statement like the 'if-else' statement. It contains both the conditions and the statements that are processed if the condition is true. A 'case' statement can have as many 'cases' or statements as required. If all specified conditions in the 'case' statement are false, the control automatically transfers to the 'else' part. Although optional, the 'else' part will be processed if none of the conditions above is true.

Which of the following is the correct method of handling exceptions in the 'Exception' section?

  1. If exception then exception handling statements

  2. If raised_exception then exception handling statements

  3. When exception then exception handling statements

  4. When raised_exception then exception handling statements


Correct Option: C
Explanation:

Exception handling prevents the program from abnormal termination. If a program encounters an exception/run time error, the control moves to the exception part where it is handled using the 'when exception then' keyword. For example: To handle a user defined exception, the statement will be  when no_data_found then dbms_output.put_line('No Records Found');

In a PL/SQL program, to declare a variable with the same data type as a column of a table (anchored declaration), the correct syntax will be

  1. Varname tablename.colname%rowtype

  2. Varname tablename.colname%istype

  3. Varname tablename.colname%totype

  4. Varname tablename.colname%type


Correct Option: D
Explanation:

PL/SQL provides a very useful feature called variable anchors. It refers to the use of keyword %TYPE to declare a variable with the same data type as the column data type in a table. For example, if we want to declare a variable named 'salary' with the same data type as of 'basic_salary' column of a table named 'employee' we can write the following statement: salary employee.basic_salary%type;

Which of the following is NOT an advantage of subprograms (procedures and functions)?

  1. Modularity

  2. Reusability

  3. Abstraction

  4. Inheritance


Correct Option: D
Explanation:

There is no inheritance in subprogram and therefore it cannot be counted as one of its advantages.

With reference to OLD and NEW pseudorecords, which of the following statements is FALSE ?

  1. For an INSERT trigger, OLD contains no value and NEW contains new values.

  2. For an ALTER trigger, OLD contains the old values, and NEW contains the new values.

  3. For an UPDATE trigger, OLD contains the old values, and NEW contains the new values.

  4. For a DELETE trigger, OLD contains old values, and NEW contains no value.


Correct Option: B
Explanation:

ALTER is a DDL statement and triggers are written on execution of a DML statement. Therefore, we cannot create a trigger that fires when an Alter statement is issued.

Which of the following statements is FALSE regarding 'Parameters in Cursor' (Parameterized Cursor)?

  1. The mode of a parameter can be both IN and OUT.

  2. A cursor parameter can be assigned a default value.

  3. The scope of the cursor parameter is local to the cursor.

  4. We can only specify the datatype of the parameter, not its length.


Correct Option: A
Explanation:

Parameterized cursors are static cursors that can accept passed-in parameter values when they are opened. The mode of parameter passed to a cursor can only be IN. The syntax for opening a parameterized cursor is open cursor_name(value)

Example: open mycursor(1000); where 1000 is the parameter value

Which of the following statements is FALSE about 'Implicit Cursors'?

  1. Oracle performs the open, fetches, and close for you automatically.

  2. It is less efficient than an explicit cursor.

  3. It is less vulnerable to data errors.

  4. It gives you less programmatic control.


Correct Option: C
Explanation:

If an implicit SELECT statement returns more than one row, it raises the TOO_MANY_ROWS exception. In this case, execution in the current block terminates and control is passed to the exception section. An implicit cursor is more prone to errors.

Which of the following statements is FALSE regarding variable names?

  1. Variable names must start with a letter.

  2. Variable names cannot contain a '$' sign.

  3. Variable names cannot contain spaces.

  4. Variable name should be a maximum of 30 characters in length.


Correct Option: B
Explanation:

The name of a variable can contain $(dollar), _ (underscore) and # (pound).

Which line in the above code has an error?

 declare
            no number;
            sq number;
begin
            no:=&number; --Line 1
            if no>10000 then -- Line 2
                        dbms_output.put_line('Too large value'); -- Line 3
            else -- Line 4
                        sq=no*no; -- Line 5
                        dbms_output.put_line(\\\\\\'Square of \\\\\\' || no || \\\\\\' is \\\\\\' || sq); -- Line 6
            end if; -- Line 7
end;
/

  1. Line 1

  2. Line 2

  3. Line 5

  4. Line 6


Correct Option: C
Explanation:

The assignment operator in PL/SQL is := whereas the above code is using the '=' operator which is an equality operator. Therefore, this line will display an error.

- Hide questions