250+ TOP MCQs on perl – A General Purpose Tool and Answers

Advanced Unix Questions and Answers on “perl – A General Purpose Tool”.

1. We can specify filenames in command line using perl.
a) True
b) False

Answer: a
Clarification: perl provides specific functions to open a file and perform I/O operations on it. The (diamond) operator is used for reading lines from a file. For example,

 perl  -e ‘print while (<>)’  dept.lst        // file opening implied

2. Which of the following is referred to as default variable?
a) $0
b) $1
c) $!
d) $_

Answer: d
Clarification: perl assigns the line read from input to a special variable $_; often called default variable. This is an extremely important variable, which can make our code compact.

3. ___ operator is used for selecting current line number.
a) $0
b) $1
c) $.
d) $_

Answer: c
Clarification: perl stores the current line number in a special variable $. ($ followed by a dot). We can use it to represent a line address and select lines from anywhere:

perl  -ne  'print if {$.  < 4}' foo

4. ___ is known as range operator.
a) . .
b) $1
c) $.
d) $_

Answer: a
Clarification: perl . . (range operator) is a counterpart of awk’s NR variable. For example,

the following command prints 1,2,3,4,5
for each(1..5) 
{
  print “$_”;
}

5. The command @x=(1. .10) will assign first ten integer values to the array ‘a’.
a) True
b) False

Answer: a
Clarification: Lists and arrays lie at the heart of perl. Perl has a large number of functions to manipulate them. For example, in the following we’ve assigned three values to array @month:

@month =  {“jan”, “feb”, “march”} ;        //$month[1] is feb

6. The ___ prefix to an array name signifies the last index of the array.
a) $0
b) $#
c) #$
d) $_

Answer: b
Clarification: The $# prefix to an array name signifies the last index of the array. It is always one less than the size of the array. For example,

@month =  {“jan”, “feb”, “march”} ;
$last_index = $#month;        //$last_index is “march”

7. For deleting the elements from the left of the array ___ function is used.
a) pop
b) push
c) queue
d) shift

Answer: d
Clarification: perl provides a handful of functions for manipulating the contents of an array. For example, perl uses shift function to delete the left-most element of an array.

@list= (3. .5,9) ;    // this is 3,4,5,9
shift(@list)         // now it is 4,5,9

8. For deleting the elements from the right of the array ___ function is used.
a) pop
b) push
c) queue
d) shift

Answer: a
Clarification: perl provides a handful of functions for manipulating the contents of any array. For example, perl uses the pop function to delete the right-most element of an array.

@list= (3. .5,9) ;      // this is 3,4,5,9
pop(@list)             // now it is 3,4,5

9. To add elements to the left side of the array ____ function is used.
a) pop
b) push
c) queue
d) unshift

Answer: d
Clarification: Ta add elements to the left side of the array, use the unshift function. For example,

@list= (5,9) ;                // this is 5,9
unshift( @list,1. .3) ;      // now it becomes 1,2,3,5,9

10. To add elements to the right side of the array ____ function is used.
a) pop
b) push
c) queue
d) unshift

Answer: b
Clarification: Ta add elements to the right side of the array, use the push function. For example,

@list= (5,9) ;            // this is 5,9
unshift( @list,13) ;     // now it becomes 5,9,13

11. Which function can combine the functionalities of push, pop, unshift and shift?
a) splice
b) add
c) delete
d) split

Answer: a
Clarification: The splice function can perform all the four functions performed by these functions. It uses upto four arguments to add or remove elements at any location of the array. The second argument is the offset from where we’ve to perform insertion or deletion, the third argument represents the number of elements to be removed. If it is 0, elements are to be added. The fourth argument specifies the new replaced list. For example,

@list= (1,2,3,4,5,9) ;
splice(@list, 5, 0, 6. . 8);        //adds at 6th location -1,2,3,4,5,6,7,8,9

12. For looping in a list, ____ is used.
a) for
b) fordo
c) foreach
d) while

Answer: c
Clarification: perl provides an extremely useful foreach construct to loop through a list. The syntax is,

foreach $var (@arr) 
{
   statements
}

13. perl also supports use of for loop in the following manner.

a) True
b) False

Answer: a
Clarification: perl supports both foreach construct and for loop for looping purposes. For example,

for($i=0;$i<3;$i++){print(“hello”) ; }        //print “Hello” three times

14. For splitting a line or expression into fields, __ is used.
a) foreach
b) for
c) split
d) join

Answer: c
Clarification: split breaks a particular line or expression into fields. These fields are assigned either to variables or an array. For example,

($var1, $var2, $var3 . . . ) = split(/sep/,stg) ;
@arr= split(/sep/,stg) ;      
// splits string stg on sep (sep can be literal character or regular expression)

15. ___ function is used for joining lists.
a) foreach
b) for
c) split
d) join

Answer: d
Clarification: The join function acts in an opposite manner to split. It combines its arguments into a single string and uses the delimiter as the first argument. For example,

$weekstr = join( “ “, @week_arr) ;
@weekstr = join(“ “, “mon”, “tue”, “wed”);
print $weekstr ;            // output will be mon, tue, wed

16. perl is ____ of grep, tr, sed and awk.
a) subset
b) superset
c) child
d) parent

Answer: b
Clarification: perl is a superset of grep, sed, awk and the shell. It can perform all the functions performed by all these and that too more efficiently.

17. The following will display :

perl -e ‘print “UNIX” x 10 . “n” ;’

a) UNIX
b) UNIX 10 times
c) error message
d) n

Answer: b
Clarification: The perl x operator is used for repeating a string. So the above command will print UNIX 10 times.

Leave a Reply

Your email address will not be published. Required fields are marked *