0% found this document useful (0 votes)
8 views30 pages

Perl Possible CBE Questions & Answers by Sir Linux

This document contains a series of Unix Multiple Choice Questions and Answers focused on Perl as a general-purpose tool. It covers various aspects of Perl including its functions, operators, and how it interacts with files and arrays. Additionally, it includes questions about operator-command combinations in Unix, emphasizing the importance of combining operators with commands for functionality.

Uploaded by

Joshua Emmanuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views30 pages

Perl Possible CBE Questions & Answers by Sir Linux

This document contains a series of Unix Multiple Choice Questions and Answers focused on Perl as a general-purpose tool. It covers various aspects of Perl including its functions, operators, and how it interacts with files and arrays. Additionally, it includes questions about operator-command combinations in Unix, emphasizing the importance of combining operators with commands for functionality.

Uploaded by

Joshua Emmanuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 30

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:
$ 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.
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

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
advertisement

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 ;

SAY NO TO EXAMINATION MALPRACTICE!!!!!!


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,
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.
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

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,
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
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

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.

SAY NO TO EXAMINATION MALPRACTICE!!!!!!

Unix Questions and Answers – Operator-Command Combinations


This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Operator-Command
Combinations”.

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

4. For yanking text, ___ is used.


a) d
b) y
c) c
d) G
View Answer
Answer: b
Explanation: The y operator is used for yanking (copying) text. It is used in the same way as d operator is used
i.e. it is also combined with itself or with another command. For example,
5yy // yanks five lines

5. For pasting text, ____ is used.


a) p or P
b) d or D
c) C
d) x or X
View Answer
Answer: a
Explanation: After yanking text with yy, we can paste it using either p or P. p paste the text below the current
line while P paste the text above the current line.
6. c operator is used for changing the text.
a) True
b) False
View Answer
advertisement

7. To change entire lines, ____ is used.


a) cc
b) CC
c) dd
d) c$
View Answer
Answer: a
Explanation: For changing text, c operator is used. This operator is also combined with a command to perform
the replacement. To change entire lines, use the cc command.
8. Which operator is used in vi to filter text?
a) %%
b) &&
c) $
d) !
View Answer
Answer: d
Explanation: vi provides a marvellous feature of allowing UNIX filter to act on the text that’s displayed on the
terminal. For filtering text screen,
1. move to the beginning of the text to be acted upon and press !
2. move to the other end of the text using navigation command like G
3. Enter the command to act on the text.
9. To delete from current cursor upto the first occurrence of }, which of the following will be used?
a) dd$}
b) d /
c) d / }
d) }
View Answer
Answer: c
Explanation: We can perform many functions using operator-command combinations. For example, d / } will
deletes from the current cursor upto first occurrence of } .
10. The command df. will _____
a) deletes entire line
b) deletes entire paragraph
c) deletes from current cursor up to first occurrence of
d) deletes zero lines
View Answer
Answer: c
Explanation: The command df. is another example of operator-command combination. It deletes from cursor to
first occurrence of a dot.
11. To yank from current cursor up to first occurrence of string str in reverse direction, we can use:
a) y?str
b) y!str
c) yy str
d) yy ! str
View Answer
Answer: a
Explanation: The command y?str is another example of operator-command combination. The operator ? is
used with y operator for yanking a string in reverse direction.
12. The following command will_________
!! tr ‘[a-z]’ ‘[A-Z]’

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.

SAY NO TO EXAMINATION MALPRACTICE!!!!!!

Unix Questions and Answers – Handling Multiple Files and Buffers


This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Handling Multiple Files
and Buffers”.

1. Which of the following command is used for switching files?


a) :e
b) e!
c) !e
d) !r
View Answer
Answer: a
Explanation: We can switch between multiple files without quitting the vi editor. For example, while working with
one file, we can switch to another by using the following command,
:e file02 // switches to file02

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

10. To restore a number of recent deletions, numbered buffers are used.


a) True
b) False
View Answer
Answer: a
Explanation: We often need to restore a number of recent deletions. Vi editor makes it possible to restore up to
recent nine complete line deletions using its numbered buffers. For example,
“1p // restores most recent deletion
11. For restoring the contents of numbered buffers efficiently, ___ command is used.
a) .
b) $
c) :nb
d) :q
View Answer
Answer: a
Explanation: vi offers the dot command to restore the contents of any buffer efficiently. Every time this
command is pressed, it steps through the buffer set to restore the contents of the next buffer.
12. The technique of restoring line deletions from numbered buffers is applicable only to entire lines.
a) True
b) False
View Answer
Answer: a
Explanation: The technique of restoring line deletions from numbered buffers is applicable only to entire lines.
For example, we can’t restore five words deleted with 5dw unless it was the last editing action performed.
13. vi used ___ to enter control characters.
a) ctrl-w
b) ctrl-v
c) ctrl-d
d) esc
View Answer
Answer: b
Explanation: In vi, some characters are directly enterable, but a control character has to be preceded by
another control character for it to be interpreted properly. Vi used ctrl-v to precede any control character.
14. To append contents of buffer to file01, we can use __________
a) : w >> file01
b) :r > file01
c) :w > file01
d) :r >> file01
View Answer
Answer: a
Explanation: The command :w >> file01 will append the contents of buffer to the file specified.
15. Which of the following is an invalid command?
a) :r file01
b) :e file01
c) :e#
d) :w # file01
View Answer
Answer: d
Explanation: : r file01 will read the contents of file01 below the current line. :e file01 will stop editing the current
file and will edit file01. Similarly, :e# returns to editing most recently edited file.

SAY NO TO EXAMINATION MALPRACTICE!!!!!!

Unix Questions and Answers – Customizing Vi Editor


This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Customizing Vi Editor”.

1. For searching a character, ___ and ___ are used.


a) f, t
b) n, w
c) e, d
d) k, n
View Answer
Answer: a
Explanation: For searching a character, we can take the cursor near or to a specific character in the current
line. This is done with f and t commands.
2. For moving the cursor forward to the first occurrence of a character, __ is used.
a) d
b) t
c) f
d) n
View Answer
Answer: c
Explanation: For moving the cursor forward to the first occurrence of a character, f command is used followed
by the character. For example,
fch // moves the cursor forward to the first occurrence of ch

3. t command positions the cursor a single character before the occurrence.


a) True
b) False
View Answer
Answer: a
Explanation: t command works in a similar manner as f except the fact that it positions the cursor a single
character before the occurrence.
4. __ and __ commands works in a reverse manner as f and t.
a) F, T
b) tf, ft
c) z, x
d) ff, tt
View Answer
Answer: a
Explanation: F and T perform the respective functions as f and t but in reverse direction.
5. For repeating the character search , and ; are used.
a) True
b) False
View Answer
Answer: a
Explanation: We can repeat the character search using the ; and , respectively. ; repeats the search in the
same direction along which the previous search was made with f or t. Similarly (,) repeats the search in the
opposite direction.
6. For customizing vi, which of the following command is used?
a) set
b) map
c) abbr
d) set, map, abbr
View Answer
Answer: d
Explanation: vi can be customized in the way we want. For this purpose, map, set and abbr commands are
used.
7. vi environment is determined by variable settings. These variables are controlled by ____
command.
a) :set
b) :abbr
c) :map
d) autowrite
View Answer
Answer: a
Explanation: vi environment is determined by variable settings. These variables are controlled by :set
command.
8. For automatic indentation, ____ is used with : set command.
a) autowrite
b) autoindent
c) automode
d) showmode
View Answer
Answer: b
Explanation: To provide automatic indentation to or code, we can use the following statement:
:set autoindent

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

:set ignorecase // case-insensitive search

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.

SAY NO TO EXAMINATION MALPRACTICE!!!!!!

Unix Questions and Answers – System Calls Basics – 1


This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “System Calls Basics –
1”.

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

10. open system call returns the file descriptor as ___


a) int
b) float
c) char
d) double
View Answer
Answer: c
Explanation: open returns the file descriptor as an int. This is the lowest number available for allocation and is
used as an argument by the other four calls (read, write, close, lseek).

SAY NO TO EXAMINATION MALPRACTICE!!!!!!

Unix Questions and Answers – System Calls Basics – 2


This set of Unix Question Bank focuses on “System Calls Basics – 2”.

1. Which of the following system call is used for closing a file?


a) open
b) lseek
c) close
d) write
View Answer
Answer: c
Explanation: A program automatically closes all open files before termination, but it’s a good practice to close
them explicitly. The close system call is used for closing a file.
int close (int fd)
2. close system call returns ____
a) 0
b) -1
c) 1
d) 0 and -1
View Answer
Answer: d
Explanation: The return type of close system call is an integer. It either returns 0 if the file is closed successfully
or -1 otherwise.
3. ____ system call is used for writing to a file.
a) read
b) write
c) close
d) seek
View Answer
Answer: b
Explanation: write system call is required for writing to a file which has previously been opened with the open
system call. write system call returns the number of characters written.
4. write system call returns -1 when ___________
a) if disk fills up while write is in progress
b) when file doesn’t exist
c) if the file size exceeds the system’s limit
d) if disk fills up while write is in progress and if the file size exceeds
View Answer
Answer: d
Explanation: write system call returns the number of characters written. However, it will return -1 if if disk fills up
while write is in progress or if the file size exceeds the system’s limit.
5. ____ system call is used for positioning the offset pointer.
a) read
b) write
c) open
d) lseek
View Answer
Answer: d
Explanation: The lseek system call moves the file offset pointer to a specified point. It doesn’t do any physical
I/O rather it determines the position in the file where the next I/O operation will take place.
6. Which of the following offset is used with lseek system call to set the offset pointer to the end of
the file?
a) SEEK_SET
b) SEEK_END
c) SEEK_CUR
d) SEEK_CR
View Answer
Answer: b
Explanation: The offset signifies the position of the offset pointer which can take one of these three values:
SEEK_SET - offset pointer set to the beginning of file
SEEK_END - offset pointer set to the end of file
SEEK_CUR - offset pointer remains at current location
7. Which of the following system call is used for truncating a file?
a) truncate
b) ftruncate
c) trunk
d) truncate and ftruncate
View Answer
Answer: d
Explanation: The truncate and ftruncate calls can truncate a file to any length. These calls are often used in
combination with lseek to overwrite a certain segment of a file.
8. truncate needs the ___ of the file as an argument but ftruncate works with _______
a) pathname, file descriptor
b) file descriptor, pathname
c) pathname, pathname
d) file descriptor, file descriptor
View Answer
Answer: a
Explanation: The truncate and ftruncate calls can truncate a file to any length. truncate needs the pathname of
the file as an argument but ftruncate works with the file descriptor.

SAY NO TO EXAMINATION MALPRACTICE!!!!!!

Unix Questions and Answers – Process Basics


This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Process Basics”.

1. A process is an instance of _______ program.


a) waiting
b) executing
c) terminated
d) halted
View Answer
Answer: b
Explanation: A process is simply an instance of a running program. A process passes through many states
throughout its life cycle i.e. when it is born until it is executed. After the process has completed it is said to be
terminated.
2. A process is said to be ____ when it starts its execution.
a) born
b) die
c) waiting
d) terminated
View Answer
Answer: a
Explanation: A process is said to be born when it starts its execution. It is the initial state of a process. The
process is assigned to CPU for its execution further.
3. When the process has completed its execution it is called ______
a) born
b) terminated
c) waiting
d) exit
View Answer
Answer: d
Explanation: A process is said to be died or terminated when it has completed its execution either normally or
abnormally. As long as the process is running it is in an active state but as soon as the process has completed
its execution, the process is said to die.
4. Programs and process are synonymous.
a) True
b) False
View Answer
Answer: b
Explanation: Program should not be confused with the process. Both differ from each other but very slightly.
The process is only an instance of a running program. Until a program hasn’t started its execution it is referred
to a program only but as soon it is in execution state it is called as a process.
5. Which data structure is used to store information about a process?
a) process control block (pcb)
b) array
c) queue
d) program control block
View Answer
Answer: a
Explanation: A process control block is a data structure which is used for storing information about a process. It
is also known as task control block and is maintained by the kernel for maintenance of a process. Each process
has its own pcb.
6. Some attributes of every process are maintained by the kernel in memory in a separate structure
called the ______
a) pcb
b) task control block
c) process table
d) task table
View Answer
Answer: c
Explanation: As every process has some attributes. Some of these attributes are maintained by the kernel in
memory in a separate structure called the process table. A process table is simply an array of many pcb’s.
Process table contains two major attributes of a process i.e. processed and parent process ID.
7. Process table and process control block store same attributes of a process.
a) True
b) False
View Answer
Answer: b
Explanation: Both pcb and process table store attributes and information about processes. But the major
difference between both is, pcb contains all the information about the process and is used in context switching
while process table contains very few attributes of a process like registers, pid, parent pid.
8. Each process is identified by a unique integer called ______
a) PID
b) PPID
c) TID
d) PTID
View Answer
Answer: a
Explanation: Each process is uniquely identified by a unique integer called as the Process ID (PID) which is
allotted by the kernel when the process is born. This PID is used for controlling the process of killing it.
9. Every process has a parent process.
a) True
b) False
View Answer
Answer: a
Explanation: Just like a file has a parent, every process also has the same. The parent is also a process and
the process born from it is called child process. For example, when we run the cat command a process
representing the cat command is started by the shell process. The process started by the shell is called child
process and the shell (which could be sh, ksh or any other) is the parent process.
10. The parent id of a child is called ______
a) PID
b) PPID
c) TID
d) PTID
View Answer
Answer: b
Explanation: The parent ID of a child process is called PPID (parent process ID) and is available as a process
attribute. It is common that several processes have the same parent. When several processes have the same
parent, it often makes sense to kill the parent process rather than killing each child separately.
11. Which process is immediately set up by the kernel when we log on to a UNIX system?
a) shell
b) parent
c) shell
d) bash
View Answer
Answer: a
Explanation: As we log on to a UNIX system, a process is immediately set up by the kernel. This process
represents a UNIX command which may sh (Bourne shell), ksh (Korn shell), csh (C shell) or bash (Bash). This
process remains alive until we log out when it is killed by the kernel.
12. To know the PID of your current shell, which command will be used?
a) echo $$
b) echo $
c) $SHELL
d) $PATH
View Answer
Answer: a
Explanation: The shell’s pathname is stored in SHELL, but it’s PID is stored in a special variable, $$. To know
the PID of our current shell, type
advertisement

$ echo $$
258 // PID of the current shell

13. The PID of our login shell doesn’t change.


a) True
b) False
View Answer
Answer: b
Explanation: The PID of our login shell can’t obviously change as long as we are logged in. But when we log
out and log in again, our login shell will be assigned a different PID. This knowledge of PID is necessary to
control the activities at our terminal.
14. What is the PID of the first process that is set up when the system is booted?
a) 1
b) 0
c) any
d) 2
View Answer
Answer: b
Explanation: Every process has a parent process, we can’t have any orphaned process for a longer time. The
ancestry of every process is ultimately traced to the first process (PID 0) that is set up when the system is
booted. It’s like the root directory of the system.
15. Which of the following command doesn’t create a process?
a) pwd
b) fork
c) cd
d) pwd and cd
View Answer
Answer: d
Explanation: When we run a command, a process representing the command is started by the shell process
but all commands don’t set up processes. Built-in commands of the shell like pwd, cd etc do not create
processes.

SAY NO TO EXAMINATION MALPRACTICE!!!!!!

SUCCESS!

You might also like