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
How do you define a subroutine in Perl?
A) sub my_function {}
B) function my_function {}
C) subroutine my_function {}
D) define my_function {}
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();
What keyword is used to define a subroutine in Perl?
A) sub
B) function
C) method
D) def
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
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);
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;
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();
Which symbol is used to define an anonymous subroutine in Perl?
A) &
B) $
C) []
D) {}
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);
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
Which array holds the arguments passed to a subroutine in Perl?
A) @_
B) @ARGV
C) @args
D) @input
How can you return a value from a subroutine in Perl?
A) return value;
B) output value;
C) exit value;
D) send value;
Which function can be used to access arguments passed to a subroutine in Perl?
A) shift
B) pop
C) push
D) unshift
In Perl, how can you return an array from a subroutine?
A) return @array;
B) return array;
C) return @array;
D) return *array;
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
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);
Which function allows you to get the number of arguments passed to a subroutine in Perl?
A) scalar(@_)
B) count(@_)
C) length(@_)
D) size(@_)
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
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);
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
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
Which operator in Perl is used to remove the first element of @_?
A) pop
B) shift
C) unshift
D) push
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
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]
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
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
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
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);
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
Qno
Answer
1
A) sub my_function {}
2
A) my_function();
3
A) sub
4
D) The last evaluated expression
5
A) my_function($arg1, $arg2);
6
B) sub { return value; }
7
A) Package::my_function();
8
A) &
9
A) my_function(@array);
10
B) To call the subroutine without parentheses
11
A) @_
12
A) return value;
13
A) shift
14
C) return @array;
15
A) The result of $x + $y
16
A) my_function(%hash);
17
A) scalar(@_)
18
C) The last evaluated expression
19
B) my_function(@list);
20
B) It will return the last evaluated expression
21
A) It stores all the arguments passed to a subroutine