Unix Questions and Answers – perl – A General Purpose Tool – 2

«
»

This set of Advanced Unix Questions and Answers focuses on “perl – A General Purpose Tool – 2”.

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

Answer: a
Explanation: 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,

advertisement
 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) $_
View Answer

Answer: d
Explanation: 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.
advertisement
advertisement

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

Answer: c
Explanation: 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

advertisement

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

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

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

advertisement

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

Answer: a
Explanation: 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) $_
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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.

for($i=0;$i<3;$i++){ }

a) True
b) False
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

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

Sanfoundry Global Education & Learning Series – Unix.

To practice advanced questions and answers on all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter