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

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “perl – A General Purpose Tool – 1”.

1. Which one of the following is the most powerful filter?
a) awk
b) grep
c) sed
d) perl
View Answer

Answer: d
Explanation: A perl is the finest filter used on the UNIX system and is the finest of all (grep, sed, awk, tr). In fact, it combines the power of these. There is nothing can these filters can do and perl can’t.

2. A perl program runs in a special interpretive mode.
a) True
b) False
View Answer

Answer: a
Explanation: A perl program runs in a special interpretive mode; the entire script is first compiled internally before getting executed.

3. To test whether perl is in your PATH, use ____
a) perl -e
b) perl -i
c) perl -el
d) perl -ed
View Answer

Answer: a
Explanation: Unlike other filters, script errors are generated before the execution of perl script file. To check whether perl exist on our system or not use this simple command:

advertisement
advertisement
$ perl -e ‘print(“perl is present”) ;’

4. It is often more convenient to save perl program files with ____ extension.
a) .gp
b) .sh
c) .awk
d) .pl
View Answer

Answer: d
Explanation: perl programs are often very big, hence it is better to use .pl extension with perl program files.

5. ___ function is used for removing the last character from the line.
a) cut
b) chop
c) erase
d) split
View Answer

Answer: b
Explanation: In many conditions, we may need to chop the last character –especially when there is a newline character. For this purpose, chop function is used.

6. perl variables have no type and no initialization.
a) True
b) False
View Answer

Answer: a
Explanation: Perl variables have no type and no initialization. Both the strings and numbers can be as large as our machine permits.
advertisement

7. When a string is used for numeral computations, perl converts it into ___
a) character
b) floating point number
c) number
d) boolean value
View Answer

Answer: c
Explanation: There are some attributes which we should keep in mind while using perl. One of which is, when we use string for numerical comparison or computation, perl immediately converts it into a number.

8. If a variable is undefined, its value is ____
a) 0
b) 1
c) NULL
d) garbage
View Answer

Answer: a
Explanation: When a variable is undefined, it is assumed to be a NULL string and NULL string is numerically zero.
advertisement

9. Which of the following are concatenation operators?
a) /
b) .
c) _
d) \\
View Answer

Answer: b
Explanation: Like in shell, concatenation is performed by placing two variables side by side. This case is not followed with perl. Rather perl uses . (dot) operator for concatenating two variables. For example,

$ perl  -e  '$x="san" ; $y="foundry" ; print($x . $y);'
sanfoundry

10. To repeat a string, perl uses ___ operator.
a) /
b) .
c) x
d) \\
View Answer

Answer: c
Explanation: perl uses the x operator to repeat a string. For example, the following command will print * 10 times;

$ perl  -e  ‘print “*” x 10 ;’
**********

11. Which function is used by perl for displaying the length of a string?
a) string
b) len
c) split
d) length
View Answer

Answer: d
Explanation: The length function is used by perl to return the length of any string. For example,

$x= “Abdullah”;
print length($x);       // prints 8

12. ___ function returns the first occurrence of a character in a string.
a) string
b) index
c) split
d) length
View Answer

Answer: b
Explanation: The index function returns the first occurrence of a character in a string. For example,

$x= “Abdullah”;
print index($x,u);     // prints 3

13. For extracting a substring, ____ function is used.
a) string
b) index
c) substr
d) length
View Answer

Answer: c
Explanation: The substr function extracts a particular substring from a string based on the value of specified indices. For example,

$x= “abcdefghijklm”
$y= substr( $x, -3,2);   // extracts two characters from the third position on the right side
print “$y”;             // prints kl

14. substr function is also used to alter an existing string.
a) True
b) False
View Answer

Answer: a
Explanation: substr function works in a versatile way. It can also be used for altering an existing string i.e. we can edit an existing string. For example,

$x= “abcdijklm”
substr($x,4,0)= “efgh” ;    // stuff $x with efgh without replacing any charcaters
print “$x” ;               //$x is now abcdefghijklm

15. Which function is used by perl for reversing a string?
a) rev
b) reverse
c) split
d) substr
View Answer

Answer: b
Explanation: The reverse function, which can operate on strings as well as arrays is used to reverse the characters in a string and return the reversed string. For example,

$x= “abcd” ;
print reverse($x) ;       / prints dcba

16. Which function is used for handling substitutions in perl?
a) tr
b) s
c) str
d) tr and s
View Answer

Answer: d
Explanation: The s and tr functions handle all substitutions in perl. The s command is used in same way as it was used in sed while tr is used translating the characters in the same way as the UNIX tr command does.

17. Which escape character is used for identifying a word character?
a) \s
b) \d
c) \w
d) \n
View Answer

Answer: c
Explanation: Perl offers some escaped characters to represent whitespace, digits and word boundaries. The most commonly used ones are:

\s - a whitespace character
\d - a digit
\w - a word character

18. We can use find command for testing files with perl.
a) True
b) False
View Answer

Answer: a
Explanation: Perl can also be used with test command for various file tests. For example,

$x = “abc.txt” ;
print “File $x is readable\n” if -r $x ;

Sanfoundry Global Education & Learning Series – Unix.

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.