Perl Subroutines and Function Calls Quiz Quiz

Enhance your understanding of Perl subroutines and function calls with this quiz, which covers syntax, calling conventions, argument passing, return values, and best practices for defining and using functions in Perl scripts.

  1. Defining a Simple Subroutine

    Which keyword is used in Perl to define a subroutine named 'greet' that takes no arguments?

    1. def
    2. func
    3. sub
    4. function

    Explanation: The correct keyword for defining a subroutine in Perl is 'sub'. This is followed by the subroutine name and a code block. 'def', 'func', and 'function' are not valid Perl keywords; they are used in other programming languages but have no meaning for Perl subroutine definitions.

  2. Calling a Subroutine

    How do you correctly call a Perl subroutine named 'compute_sum' with two arguments: 5 and 10?

    1. compute_sum[5, 10];
    2. compute_sum(5, 10);
    3. compute_sum{5, 10};
    4. compute_sumu003C5, 10u003E;

    Explanation: Function calls in Perl use parentheses to enclose arguments, so 'compute_sum(5, 10);' is correct. The square, curly, and angle brackets, as in the other options, are either not valid for function calls or have different meanings in Perl.

  3. Passing Arguments

    In a Perl subroutine, how are arguments accessed inside the subroutine body?

    1. Using argv[]
    2. Using the @_ array
    3. Using %ARGS
    4. Using $arguments

    Explanation: Arguments passed to a Perl subroutine are available in the special array '@_'. '$arguments' and 'argv[]' are not used for this purpose in Perl, while '%ARGS' is not automatically set by the language. Only '@_' provides the functionality needed for argument access.

  4. Returning Values

    What is the primary method for returning a value from a Perl subroutine?

    1. Exiting the code block
    2. Assigning to $_
    3. Using the return statement
    4. Printing output

    Explanation: The 'return' statement is used to send a value from a subroutine back to the caller. Assigning to '$_' or just printing output does not return a value, and simply exiting the code block with no return value may lead to undefined results or default behavior.

  5. Default Scalar Return

    If a Perl subroutine does not explicitly use 'return', what does it return by default?

    1. Zero
    2. Undefined
    3. The value of the last evaluated expression
    4. Nothing at all

    Explanation: When 'return' is omitted, Perl returns the value of the last evaluated expression within the subroutine. It does not default to undefined, zero, or nothing; instead, it always uses the last expression's value, which differs from how some other languages handle this.

  6. Prototype Syntax

    How do you define a Perl subroutine 'hello' that takes no arguments and enforces this at compile time?

    1. sub hello () { ... }
    2. function hello() { ... }
    3. def hello() { ... }
    4. sub hello[] { ... }

    Explanation: Perl allows you to specify a prototype using parentheses after the subroutine name, as in 'sub hello () { ... }'. The other options either use incorrect syntax ('hello[]', 'def', 'function') or keywords not valid in Perl for subroutine definitions.

  7. Subroutine References

    Which symbol do you use in Perl to create a reference to a subroutine named 'display'?

    1. $display
    2. &display
    3. ^display
    4. u0026display

    Explanation: To create a reference to a subroutine in Perl, use the backslash before the subroutine sigil: '&display'. 'u0026display' calls the subroutine, '$display' is a scalar variable, and '^display' is not valid syntax for functions or references.

  8. Anonymous Subroutines

    How can you define an anonymous subroutine in Perl and assign it to a variable?

    1. my $sub = sub { ... };
    2. sub $sub = { ... };
    3. var $sub = func { ... };
    4. def $sub() { ... };

    Explanation: The correct way to define an anonymous subroutine and assign it to a variable is 'my $sub = sub { ... };'. The options using 'var', 'func', or 'def' are incorrect for Perl and will result in errors. 'sub $sub = { ... };' is incorrect syntax.

  9. Calling with No Arguments

    Which way is valid for calling a Perl subroutine named 'show' without any arguments?

    1. show[];
    2. showu003Cu003E;
    3. show{};
    4. show();

    Explanation: Subroutines with zero or more arguments are called with parentheses (e.g., 'show();') or even just 'show;', but 'show();' is conventionally clear. The notations 'show[];', 'show{};', and 'showu003Cu003E;' are not valid for subroutine calls in Perl.

  10. Variable Scope in Subroutines

    Which keyword ensures a Perl variable inside a subroutine is only accessible within that subroutine?

    1. local
    2. our
    3. my
    4. global

    Explanation: 'my' declares a lexical variable in Perl, scoping it to the enclosing block such as a subroutine. 'our' creates a variable with package scope, 'local' temporarily backs up and changes a global variable, and 'global' is not a Perl keyword.