PL/SQL Collections Practice Questions

PL/SQL collections: associative arrays (index-by tables), nested tables, varrays, collection methods and collection exceptions. This set has 34 multiple-choice questions, each with a short explanation. Click Start quiz to answer one question at a time and get instant feedback, or use Study mode to read every question with its answer.

All 34 questions with answers

  1. Which PL/SQL method returns the index number that succeeds index n.
    1. Extends(n)
    2. Prior(n)
    3. Next(n)
    4. Last(n)
    Show answer

    Answer: C. Next(n)

    NEXT(n) returns the index that follows n, or NULL if there is none.

  2. If the subscript of a PL/SQL collection is null,or is not convertable to the right datatype then it throws the following exception
    1. VALUE_ERROR
    2. SUBSCRIPT_BEYOND_COUNT.
    3. SUBSCRIPT_NULL
    4. COLLECTION_IS_NULL
    Show answer

    Answer: A. VALUE_ERROR

    A subscript that is NULL or cannot be converted to the key type raises VALUE_ERROR.

  3. Which statement is true about ‘TABLE OF’ in PL/SQL
    1. Table of is used to define a collection type.
    2. A variable declared based on TABLE OF data type is called cursor variable.
    3. TABLE OF is dynamic cursor in which the contents of cursor can be changed dynamically at run time depending upon our requirement.
    Show answer

    Answer: A. Table of is used to define a collection type.

    TYPE t IS TABLE OF … defines a collection type: a nested table, or an associative array when INDEX BY is added.

  4. If the collection is atomically null, PL/SQL raises
    1. VALUE_ERROR
    2. SUBSCRIPT_BEYOND_COUNT.
    3. SUBSCRIPT_NULL
    4. COLLECTION_IS_NULL
    Show answer

    Answer: D. COLLECTION_IS_NULL

    Using an uninitialized (atomically null) nested table or varray raises COLLECTION_IS_NULL.

  5. A collection is an ordered group of elements of ————————–
    1. all of the same data type
    2. all of different data type
    3. some of same data type and some of different data type
    4. none
    Show answer

    Answer: A. all of the same data type

    All elements of a collection have the same data type.

  6. Which is a PL/SQL collection type
    1. Index-by tables
    2. Nested tables
    3. Varrays
    4. all the above
    Show answer

    Answer: D. all the above

    PL/SQL has three collection types: associative arrays (index-by tables), nested tables and varrays.

  7. Index by table is also known as
    1. varrays
    2. associative arrays
    3. Nested tables
    4. none
    Show answer

    Answer: B. associative arrays

    Index-by tables were renamed associative arrays in Oracle 9i.

  8. Index by table let you look up elements using ——- for subscript values
    1. arbitrary numbers
    2. strings
    3. sequential numbers
    4. both a and b
    Show answer

    Answer: D. both a and b

    Associative arrays are indexed by PLS_INTEGER or VARCHAR2 keys, and the keys can be sparse.

  9. Nested table let you look up elements using ——- for subscript values
    1. arbitrary numbers
    2. strings
    3. sequential numbers
    4. both a and b
    Show answer

    Answer: C. sequential numbers

    Nested tables use sequential integer subscripts starting at 1, though they can become sparse after DELETE.

  10. Which PL/SQL collection type will use sequential numbers as subscript types
    1. Index-by tables
    2. Nested tables
    3. Varrays
    4. both b and c
    Show answer

    Answer: D. both b and c

    Nested tables and varrays both use sequential integer subscripts starting at 1.

  11. Which PL/SQL collection type will hold fixed number of elements
    1. Index-by tables
    2. Nested tables
    3. Varrays
    4. both b and c
    Show answer

    Answer: C. Varrays

    A varray has a maximum size that is fixed in its type definition.

  12. PL/SQL collection types can be declared in
    1. procedure
    2. Function
    3. Package
    4. All the above
    Show answer

    Answer: D. All the above

    Collection types can be declared in any block, procedure, function or package.

  13. which PL/SQL collection type does not need Constructors
    1. Index-by tables
    2. Nested tables
    3. Varrays
    4. None
    Show answer

    Answer: A. Index-by tables

    Associative arrays need no constructor; assigning to an element creates it.

  14. Which PL/SQL collection needs a constructor
    1. Index-by tables
    2. Nested tables
    3. Varrays
    4. Both b and c
    Show answer

    Answer: D. Both b and c

    Nested tables and varrays must be initialized with a constructor, for example t := num_tab();

  15. Which collection method is a procedure
    1. EXTEND
    2. TRIM
    3. DELETE
    4. All the above
    Show answer

    Answer: D. All the above

    EXTEND, TRIM and DELETE change the collection and are called as procedures.

  16. Which collection method is a function
    1. EXTEND
    2. EXISTS
    3. PRIOR
    4. BOTH B AND C
    Show answer

    Answer: D. BOTH B AND C

    EXISTS and PRIOR return values, so they are functions. COUNT, FIRST, LAST, NEXT and LIMIT are functions too.

  17. Which collection method can be applied to automatically null collections
    1. Exists
    2. Extend
    3. Prior
    4. Count
    Show answer

    Answer: A. Exists

    EXISTS is the only method you can call on an atomically null collection without raising COLLECTION_IS_NULL.

  18. Which collection method returns TRUE if the nth element in a collection exists
    1. Extend(n)
    2. Exists(n)
    3. Next(n)
    4. Count
    Show answer

    Answer: B. Exists(n)

    EXISTS(n) returns TRUE if element n exists.

  19. Which Syntax is correct for Counting the elements in a PL/SQL collection
    1. IF projects.COUNT = 25 THEN
    2. IF COUNT = 25 THEN
    3. IF COUNT(projects) = 25 THEN
    4. IF COUNT(*) = 25 THEN
    Show answer

    Answer: A. IF projects.COUNT = 25 THEN

    Collection methods use dot notation, so the count is collection_name.COUNT.

  20. which PL/SQL collection method will return NULL for nested tables and associative arrays
    1. Count
    2. Exists
    3. Limit
    4. Extends
    Show answer

    Answer: C. Limit

    LIMIT returns the maximum size of a varray. For nested tables and associative arrays it returns NULL.

  21. which PL/SQL collection method returns the index number that precedes index n in a collection
    1. Extends(n)
    2. Prior(n)
    3. Next(n)
    4. First(n)
    Show answer

    Answer: B. Prior(n)

    PRIOR(n) returns the index that comes before n.

  22. Subscript range for nested tables is
    1. 1 .. 2**31
    2. 1 .. size_limit
    3. -2**31 .. 2**31
    4. none
    Show answer

    Answer: A. 1 .. 2**31

    Nested table subscripts run from 1 up to 2**31 – 1.

  23. Subscript range for varrays is
    1. 1 .. 2**31
    2. 1 .. size_limit
    3. -2**31 .. 2**31
    4. none
    Show answer

    Answer: B. 1 .. size_limit

    Varray subscripts run from 1 up to the size limit set in the type.

  24. Subscript range for associative arrays with numeric key
    1. 1 .. 2**31
    2. 1 .. size_limit
    3. -2**31 .. 2**31
    4. none
    Show answer

    Answer: C. -2**31 .. 2**31

    Associative arrays with a PLS_INTEGER key accept negative and positive keys, roughly -2**31 to 2**31.

  25. Which is allowed for a PL/SQL collections
    1. We can check whether a collection is null
    2. We can check whether two collections are the same
    3. greater than operator between two collections
    4. less than operator between two collections
    Show answer

    Answer: A. We can check whether a collection is null

    You can test a collection with IS NULL. Nested tables also allow = and <> comparison (10g and later), but no collection allows < or >.

  26. PL/SQL collections cannot appear in ———-list
    1. Distinct
    2. Group By
    3. Order By
    4. All the above
    Show answer

    Answer: D. All the above

    Collections cannot be used in a DISTINCT, GROUP BY or ORDER BY list.

  27. LIMIT returns the maximum number of elements that the collection can contain for which PL/SQL collection type
    1. Nested tables
    2. Associative array
    3. Varrays
    4. None
    Show answer

    Answer: C. Varrays

    Only varrays have a fixed maximum, so LIMIT returns a number only for varrays.

  28. Which PL/SQL collection method returns the smallest Index number in a collection
    1. Count
    2. Limit
    3. Last
    4. First
    Show answer

    Answer: D. First

    FIRST returns the smallest index in the collection.

  29. Which PL/SQL collection method returns the largest index number in a collection
    1. Count
    2. Limit
    3. Last
    4. First
    Show answer

    Answer: C. Last

    LAST returns the largest index in the collection.

  30. If the collection is empty First and Last PL/SQL collection methods return
    1. 0
    2. Null
    3. 1
    4. None
    Show answer

    Answer: B. Null

    On an empty collection, FIRST and LAST both return NULL.

  31. Which PL/SQL method is used to remove one element from the end of a collection
    1. TRIM
    2. TRIM(n)
    3. DELETE
    4. DELETE(n)
    Show answer

    Answer: A. TRIM

    TRIM with no argument removes one element from the end.

  32. Which PL/SQL method is used to remove n elements from the end of a collection
    1. TRIM
    2. TRIM(n)
    3. DELETE
    4. DELETE(n)
    Show answer

    Answer: B. TRIM(n)

    TRIM(n) removes n elements from the end.

  33. Which PL/SQL method is used to remove all elements from a collection
    1. TRIM
    2. TRIM(n)
    3. DELETE
    4. DELETE(n)
    Show answer

    Answer: C. DELETE

    DELETE with no argument removes all elements.

  34. Which PL/SQL method removes the nth element from an associative array with a numeric key
    1. TRIM
    2. TRIM(n)
    3. DELETE
    4. DELETE(n)
    Show answer

    Answer: D. DELETE(n)

    DELETE(n) removes element n. It works on associative arrays and nested tables, but not on varrays.

More Oracle practice questions

These questions come from interview and training material collected over years of Oracle EBS project work by Amol Jadhav. They are not copied from Oracle certification exams.