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?
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.
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
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:
$ perl -e ‘print(“perl is present”) ;’
4. It is often more convenient to save perl program files with ____ extension.
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.
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.
Explanation: Perl variables have no type and no initialization. Both the strings and numbers can be as large as our machine permits.
7. When a string is used for numeral computations, perl converts it into ___
b) floating point number
d) boolean value
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 ____
Explanation: When a variable is undefined, it is assumed to be a NULL string and NULL string is numerically zero.
9. Which of the following are concatenation operators?
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.
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?
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.
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.
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.
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?
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?
d) tr and s
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?
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.
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.