Perl Possible CBE Questions & Answers by Sir Linux
Perl Possible CBE Questions & Answers by Sir Linux
1
This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “perl – A General
Purpose Tool – 1”.
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.
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.
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
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
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 ;
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
advertisement
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
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
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
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.
1. An operator can’t perform any function without being combined with a command or itself.
a) True
b) False
View Answer
Answer: a
Explanation: An operator alone cannot perform any function unless it is combined with a command or itself. For
example, by using dd or yy we can delete or copy a line respectively.
2. For deleting the line form current cursor to the end of the line, we can use:
a) $
b) d$
c) dd
d) $d
View Answer
Answer: a
Explanation: dd command is a combination of the d operator with itself. vi can perform complex deletion when
this operator is combined with a command of the command mode. For example,
d$ //delete entire line fro current cursor location to end of line
3. dw will ________
a) deletes one line
b) deletes one word
c) deletes one character
d) deletes one sentence
View Answer
Answer: b
Explanation: dd command is a combination of the d operator with itself. vi can perform complex deletion when
this operator is combined with a command of the command mode. For example,
dw // deletes one word
3dw // deletes 3 words
a) throw an error
b) undefined behavior
c) changes the case of the current line from lower to uppercase
d) changes the case of current line from upper to lowercase
View Answer
Answer: c
Explanation: The ! operator when doubled is used for operating on current line. For example, we can change
the case of current line from lowercase to uppercase by using the above command.
13. The command c0 will change the text from current cursor to the end of line.
a) True
b) False
View Answer
Answer: b
Explanation: The command c0 will change the text from current cursor to the beginning of line.
14. To sort all lines from current cursor to end of line, ____ can be used.
a) sort .,$
b) sort !G
c) sort !,G
d) !Gsort
View Answer
Answer: d
Explanation: The sort command is combined with ! and G operator for sorting from current cursor to the end of
the line.
15. Which of the following is not a valid operator-command combination?
a) 5dd
b) yy5
c) cc
d) yG
View Answer
Answer: b
Explanation: The command yy5 is not a valid one as we cannot prefix the command yy with an integer value.
2. For returning back to the original file after switching we can use:
a) :e!
b) ctrl-^
c) :e#
d) ctrl-^ and :e#
View Answer
Answer: d
Explanation: We can switch between multiple files without quitting the vi editor. For this purpose, :e command
is used. For returning back to the original file, we can use one of the following commands:
[Ctrl-^] //toggles between previous and current file
:e# //same
3. When multiple filenames are used with vi, we can switch to next file using ___ command.
a) new
b) :n
c) :rew
d) :prev
View Answer
Answer: c
Explanation: When vi editor is used with multiple filenames, it loads the first file. We can switch to next file
using :n command.
4. We can move back to the previous file using ____ command.
a) new
b) :n
c) :rew
d) :prev
View Answer
Answer: c
Explanation: When vi editor is used with multiple filenames, it loads the first file. We can switch to next file
using :n command. In this manner, we can reach to the last file. At any stage, we can move back to the first file
using :rew command.
5. :r !date inserts the output of date command in our file.
a) True
b) False
View Answer
Answer: a
Explanation: We can also insert the output of any command in our file using :r command.
6. To split the screen into multiple windows, we can use ____ command.
a) :sp
b) :new
c) :n
d) :r
View Answer
Answer: a
Explanation: We can also split our screen into multiple windows using :sp command. The window can be empty
or it can contain a file, even a copy of the same file.
7. To create a new window, ____ can be used.
a) :sp
b) :new
c) :n
d) :r
View Answer
Answer: c
Explanation: We can also create an empty window which will not be associated with any file. For this
purpose, :new command is used. Now we can move between these windows using ctrl-w.
8. To remove all other windows except the current one, which of the following command is used?
a) :on
b) :new
c) :n
d) :r
View Answer
Answer: a
Explanation: To make the current window the only window on the screen and close all other windows use :on
command. We can also kill the current window using :q.
9. vi editor has ____ named buffers.
a) 2
b) 4
c) 26
d) 5
View Answer
Answer: c
Explanation: vi editor stores the deleted text in an unnamed buffer. But this suffers some limitations. i.e. we can
use only one buffer at one time. So vi uses 26 special named buffers which are named after the letters of the
alphabet. For example,
“a4yy // yanks 4 lines into buffer a
advertisement
9. Which of the following is used with :set to customize vi to ignore case in pattern searches?
a) nomatch
b) ignorecase
c) nocase
d) nomagic
View Answer
Answer: b
Explanation: To customize the vi to ignore case in pattern searches we can use the following statement:
advertisement
10. To change the default tab stop spaces in vi, ___ can be used with :set.
a) nomagic
b) tabstop=n
c) tab
d) tabchange
View Answer
Answer: b
Explanation: To change the default tab setting (8 spaces) to 4 spaces, :set tabstop=4 can be used.
11. Which command is used for mapping keys of a keyboard?
a) set
b) map
c) abbr
d) autowrite
View Answer
Answer: b
Explanation: The map command lets us assign the undefined keys or reassign the defined ones so that when
such a key is pressed, it expands to a command sequence.
12. /* and f* are same.
a) True
b) False
View Answer
Answer: b
Explanation: /* will search the entire file for an asterisk, while f* looks for it in the current line only.
13. For compiling a C program without leaving the editor, which command will be used?
a) cc %
b) :! Cc
c) :!cc %
d) :!c
View Answer
Answer: c
Explanation: vi provides us with a feature of compiling a C program without leaving the editor by using the :!cc
% command.
14. Which of the following statement is not true?
a) f and t commands are used for searching a character
b) y and yy are same
c) vi has 26 named buffers
d) :e# is used for toggling between files
View Answer
Answer: b
Explanation: The y operator yanks text but it cannot perform any function unless it is combined with any
command or itself.
1. A system call is a routine built into the kernel and performs a basic function.
a) True
b) False
View Answer
Answer: a
Explanation: All UNIX systems offer around 200 special functions known as system calls. A system call is a
routine built into the kernel and performs a very basic function that requires communication with the CPU,
memory and devices.
2. When we execute a C program, CPU runs in ____ mode.
a) user
b) kernel
c) supervisory
d) system
View Answer
Answer: a
Explanation: When we execute a C program, the CPU runs in user mode. It remains it this particular mode until
a system call is invoked.
3. In ____ mode, the kernel runs on behalf of the user.
a) user
b) kernel
c) real
d) all
View Answer
Answer: b
Explanation: Whenever a process invokes a system call, the CPU switches from user mode to kernel mode
which is a more privileged mode. The kernel mode is also called as supervisor mode. In this mode, the kernel
runs on behalf of the user and has access to any memory location and can execute any machine instruction.
4. All UNIX and LINUX systems have one thing in common which is ____
a) set of system calls
b) set of commands
c) set of instructions
d) set of text editors
View Answer
Answer: a
Explanation: As we know that, all UNIX and LINUX systems have one thing in common; they use the same set
of system calls.
5. The chmod command invokes the ____ system call.
a) chmod
b) ch
c) read
d) change
View Answer
Answer: a
Explanation: Many commands and system calls share the same names. For example, the chmod command
invokes the chmod system call.
6. For reading input, which of the following system call is used?
a) write
b) rd
c) read
d) change
View Answer
Answer: c
Explanation: The standard C library offers a set of separate functions to read a block of data. For example, to
read a block of data fread is used, for reading a line fgets is used and for reading a character fgetc is used. All
these functions invoke the system call -read, which is available for reading input.
7. Which of the following system call is used for opening or creating a file?
a) read
b) write
c) open
d) close
View Answer
Answer: c
Explanation: To read or write to a file, we first need to open it. For this purpose, open system call is used. Open
has two forms ; the first forms assumes that the file already exists and the second form creates the file if it
doesn’t.
8. There are ___ modes of opening a file.
a) 4
b) 3
c) 2
d) 1
View Answer
Answer: b
Explanation: There are three modes of opening a file, out of which only one mode is required to be specified
while opening the file. The three modes are, O_RDONLY, O_WRONLY, O_RDWR.
9. Which of the following mode is used for opening a file in both reading and writing?
a) O_RDONLY
b) O_WRONLY
c) O_RDWR
d) O_WDR
View Answer
Answer: c
Explanation: There are three modes of opening a file namely:
O_RDONLY - opens files for reading
O_WRONLY - opens file for writing
O_RDWR - opens file for reading and writing
advertisement
$ echo $$
258 // PID of the current shell
SUCCESS!