PL/SQL Packages: Specification and Body Concepts Quiz Quiz

Gain insight into the structure and functionality of PL/SQL packages, focusing on package specification and body declarations. This quiz assesses understanding of key definitions, syntax, visibility rules, and best practices in working with PL/SQL packages.

  1. Package Specification Definition

    Which part of a PL/SQL package contains the public declarations of procedures, functions, variables, and types that can be accessed by other programs?

    1. Anonymous Block
    2. Package Body
    3. Package Specification
    4. Trigger

    Explanation: The package specification houses all public declarations accessible from outside the package. The package body implements the routine logic, not public declarations. A trigger is unrelated to package declarations, and an anonymous block does not provide reusable, named programmatic structures like a package specification.

  2. Location of Procedure Bodies

    Where should the full implementation (code) of a procedure defined in the package specification be placed?

    1. Database View
    2. Package Specification
    3. Sequence
    4. Package Body

    Explanation: The package body contains the full definitions and executable code for procedures and functions listed in the specification. Only their headers appear in the specification. A database view and sequence are unrelated and cannot contain PL/SQL code. The specification contains declarations, not implementations.

  3. Content of Package Specification

    Which of the following can be declared inside a package specification?

    1. SQL*Plus commands
    2. Table storage
    3. Private procedures only
    4. Public constants

    Explanation: Public constants can be declared in a package specification, making them accessible outside the package. Private procedures are only declared in the body, not the specification. Table storage is defined in SQL DDL, not within a package, and SQL*Plus commands are not part of valid PL/SQL code.

  4. Package Body Exclusivity

    Which element can be declared only in the package body and never in the package specification?

    1. Global variables
    2. Public exception declarations
    3. Private helper functions
    4. Cursor specifications

    Explanation: Private helper functions are declared exclusively in the package body and are not visible outside the package. Global variables and cursor specifications, if needed externally, go in the specification. While exceptions can be public or private, public ones must be declared in the specification.

  5. Omitting the Package Body

    If a PL/SQL package contains only global constants and no procedure or function implementations, what can be omitted?

    1. Exception Handling
    2. Package Body
    3. Public Variables
    4. Package Specification

    Explanation: If the package only holds global constants or declarations without executable code, the package body can be omitted. A specification is required for any package; exception handling cannot be omitted from procedures where needed, and public variables refer to specification, not the body.

  6. Recompilation After Changing Specification

    When you modify the package specification, what must be done to keep the package valid?

    1. Disconnect the session
    2. Restart the database
    3. Recompile the package body
    4. Alter the package body code only

    Explanation: After modifying a package specification, the body must be recompiled to reflect changes and avoid invalid objects. Altering the body alone won't resolve dependency issues. Disconnecting or restarting the database has no effect on package validity. Only recompiling ensures consistency.

  7. Accessing Package Members

    How are public procedures declared in a package specification accessed from outside the package?

    1. package_name.procedure_name
    2. Spec.body.procedure_name
    3. procedure_name only
    4. procedure_name()

    Explanation: Public procedures are accessed with the qualified name: package_name.procedure_name. Using only the procedure name or adding parentheses is insufficient for package-scoped procedures. 'Spec.body.procedure_name' is not valid syntax in PL/SQL.

  8. Hiding Implementation Details

    Why is it considered good practice to declare only headers in the package specification and place the code in the package body?

    1. To encapsulate business logic and hide implementation details
    2. To increase package size unnecessarily
    3. To allow triggers to access private objects
    4. To declare SQL*Loader commands

    Explanation: Encapsulation ensures only the interface is exposed, while implementation is hidden in the body. Increasing package size is not a goal. Triggers cannot access private objects by this design. SQL*Loader commands are unrelated to package structure.

  9. Package Initialization Section

    Where can an initialization section that executes once when a package is first referenced be placed?

    1. At the end of the package body
    2. Within a database view
    3. Inside the package specification
    4. In a standalone procedure

    Explanation: The initialization section appears last in the package body and runs once per session when the package is first invoked. The specification cannot include executable statements. Standalone procedures and database views do not serve this purpose.

  10. Dependency Management in Packages

    If you change only the body of a PL/SQL package but keep the specification unchanged, what is the effect on dependent objects?

    1. The package specification must be dropped
    2. All dependent objects must be recompiled automatically
    3. Dependent objects do not become invalid
    4. All dependent objects must be dropped

    Explanation: Changing only the body does not affect dependent objects because their interface remains unchanged. Dropping or recompiling specifications or dependents is unnecessary. Dependencies are tied to the specification, not the body.