MCQs on Subroutines and Functions | Perl

Here are 30 multiple-choice questions (MCQs) for Chapter 5: Subroutines and Functions, focusing on defining and calling subroutines, passing arguments, returning values, and using @_ and shift. These questions test your knowledge of Perl subroutines and functions, key concepts for efficient Perl programming.


Defining and Calling Subroutines

  1. How do you define a subroutine in Perl?
    • A) sub my_function {}
    • B) function my_function {}
    • C) subroutine my_function {}
    • D) define my_function {}
  2. Which of the following is the correct way to call a subroutine in Perl?
    • A) my_function();
    • B) call my_function();
    • C) execute my_function();
    • D) run my_function();
  3. What keyword is used to define a subroutine in Perl?
    • A) sub
    • B) function
    • C) method
    • D) def
  4. What is the return value of a subroutine in Perl if no explicit return is used?
    • A) Undefined
    • B) 0
    • C) A default value
    • D) The last evaluated expression
  5. Which of the following is used to call a subroutine with arguments in Perl?
    • A) my_function($arg1, $arg2);
    • B) my_function($arg1, $arg2)();
    • C) call my_function($arg1, $arg2);
    • D) sub my_function($arg1, $arg2);
  6. How can you define a subroutine with a return value in Perl?
    • A) return value;
    • B) sub { return value; }
    • C) sub { value; }
    • D) sub return value;
  7. How do you call a subroutine that is defined in a separate package in Perl?
    • A) Package::my_function();
    • B) my_function();
    • C) call Package::my_function();
    • D) use Package::my_function();
  8. Which symbol is used to define an anonymous subroutine in Perl?
    • A) &
    • B) $
    • C) []
    • D) {}
  9. How do you pass an array as an argument to a subroutine in Perl?
    • A) my_function(@array);
    • B) my_function(@array);
    • C) my_function(@array_ref);
    • D) my_function(array);
  10. What is the purpose of using the & symbol when calling a subroutine in Perl?
  • A) To pass arguments by reference
  • B) To call the subroutine without parentheses
  • C) To return the last evaluated expression
  • D) To define a global variable

Passing Arguments and Returning Values

  1. Which array holds the arguments passed to a subroutine in Perl?
  • A) @_
  • B) @ARGV
  • C) @args
  • D) @input
  1. How can you return a value from a subroutine in Perl?
  • A) return value;
  • B) output value;
  • C) exit value;
  • D) send value;
  1. Which function can be used to access arguments passed to a subroutine in Perl?
  • A) shift
  • B) pop
  • C) push
  • D) unshift
  1. In Perl, how can you return an array from a subroutine?
  • A) return @array;
  • B) return array;
  • C) return @array;
  • D) return *array;
  1. What will be the output of the following Perl subroutine?
    sub add {
    my ($x, $y) = @_;
    return $x + $y;
    }
    A) The result of $x + $y
    B) The sum of $x and $y
    C) Syntax error
    D) Undefined value
  1. How can you pass a hash to a subroutine in Perl?
  • A) my_function(%hash);
  • B) my_function(%hash);
  • C) my_function(@hash);
  • D) my_function(hash);
  1. Which function allows you to get the number of arguments passed to a subroutine in Perl?
  • A) scalar(@_)
  • B) count(@_)
  • C) length(@_)
  • D) size(@_)
  1. What is the default return value of a subroutine in Perl if no explicit return statement is provided?
  • A) Null
  • B) 0
  • C) The last evaluated expression
  • D) Undefined
  1. How can you pass a list of values to a subroutine in Perl?
  • A) my_function($val1, $val2);
  • B) my_function(@list);
  • C) my_function(@list);
  • D) my_function(list);
  1. What happens when you return a value from a subroutine without using the return keyword in Perl?
  • A) The subroutine will not return anything
  • B) It will return the last evaluated expression
  • C) The program will throw an error
  • D) It returns undefined

Use of @_ and shift in Subroutines

  1. What is the purpose of the @_ array in Perl subroutines?
  • A) It stores all the arguments passed to a subroutine
  • B) It stores the return values from the subroutine
  • C) It is used to return the values from a subroutine
  • D) It is used to store global variables
  1. Which operator in Perl is used to remove the first element of @_?
  • A) pop
  • B) shift
  • C) unshift
  • D) push
  1. What is the result of the following Perl subroutine?
    sub demo {
    my $arg = shift;
    return $arg;
    }
    A) The subroutine returns the first argument
    B) The subroutine returns the last argument
    C) The subroutine returns the number of arguments
    D) The subroutine throws an error
  1. Which of the following is used to access the second argument in a subroutine in Perl?
  • A) $_[1]
  • B) @_[1]
  • C) shift(1)
  • D) $_[0]
  1. In Perl, how can you pass the elements of an array as individual arguments to a subroutine?
  • A) Using @array
  • B) Using \@array
  • C) Using @array_ref
  • D) Using *array
  1. What does the shift function do in the context of a Perl subroutine?
  • A) It removes and returns the first argument
  • B) It adds an element to the beginning of @_
  • C) It removes the last element from @_
  • D) It shifts the arguments to the left
  1. What is the result of calling the following subroutine?
    sub greet {
    my ($name) = @_;
    print “Hello, $name!\n”;
    }
    greet(“Alice”);
    A) Hello, Alice!
    B) Hello!
    C) The program throws an error
    D) Nothing happens
    • What does the following code do?
      sub multiply {
      my ($x, $y) = @_;
      return $x * $y;
      }
      A) Multiplies $x and $y and returns the result
      B) Adds $x and $y and returns the result
      C) Divides $x and $y and returns the result
      D) The code throws an error
  1. How do you pass an entire array by reference to a subroutine in Perl?
  • A) my_function(@array);
  • B) my_function(@array);
  • C) my_function(array);
  • D) my_function(@array_ref);
  1. Which of the following will return the first argument passed to the subroutine?
    sub first_arg {
    my $arg = shift;
    return $arg;
    }
    A) The first argument passed to the subroutine
    B) The second argument passed to the subroutine
    C) An error
    D) Undefined value

Answer Key

QnoAnswer
1A) sub my_function {}
2A) my_function();
3A) sub
4D) The last evaluated expression
5A) my_function($arg1, $arg2);
6B) sub { return value; }
7A) Package::my_function();
8A) &
9A) my_function(@array);
10B) To call the subroutine without parentheses
11A) @_
12A) return value;
13A) shift
14C) return @array;
15A) The result of $x + $y
16A) my_function(%hash);
17A) scalar(@_)
18C) The last evaluated expression
19B) my_function(@list);
20B) It will return the last evaluated expression
21A) It stores all the arguments passed to a subroutine
22B) shift
23A) The subroutine returns the first argument
24A) $_[1]
25A) Using @array
26A) It removes and returns the first argument
27A) Hello, Alice!
28A) Multiplies $x and $y and returns the result
29B) my_function(@array);
30A) The first argument passed to the subroutine

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top