250+ TOP MCQs on Filtering Commands and Answers

Unix Multiple Choice Questions on “Filtering Commands”.

1. Filter’s are a category of commands that take the advantage of shell redirection feature.
a) True
b) False

Answer: a
Clarification: Filters are defined as a category of commands that takes the advantage of shell’s redirection features. A filter is capable of reading standard input and writing to standard output. The piping mechanism is an example of a filter which lets the standard output of one filter to serve as standard input to another.

2. Which command is used for preparing a file for printing?
a) cd
b) cat
c) print
d) pr

Answer: d
Clarification: The pr command is used for preparing a file by adding suitable headers, footers and formatted text. This command is used with a filename as an argument. For example,

$ pr  dept.lst
May  06  10:38  1998  dept.lst  Page 1
01:accounts:6213
02:admin:5423
03:marketing:6521
04:personnel:2365
 
.....blank lines.......

3. pr command adds ____ lines of margin at the top and bottom.
a) 5
b) 4
c) 2
d) 1

Answer: a
Clarification: pr command is used for preparing a file for printing by adding suitable headers, footers and formatted text. It adds five lines of margin at the top and five at the bottom. The header shows the date and time of last modification of the file along with filename and page number.

4. Which option is used with pr command for printing multi-columnar output?
a) -p
b) -k
c) -d
d) -pr

Answer: b
Clarification: -k option is used with pr command (where k is an integer) for printing in k columns. For example, if a file contains a series of 12 numbers then we can divide the output into 3 columns in this way:

$ dept.lst | pr -3
May  06  10:38  1998  dept.lst  Page 1
 
1    5    9
2    6    10
3    7    11
4    8    12

5. Which option is used with pr command to suppress the header and footers?
a) -p
b) -k
c) -t
d) -n

Answer: c
Clarification: We can use the -t option with pr command if we want to suppress the headers and associated which are displayed with the output of pr command. For example,

$ dept.lst | pr -t  -5        // no headers and footers; multi-columnar output
1    5    9
2    6    10
3    7    11
4    8    12

6. Which option is used with pr command to display output along with line numbers?
a) -d
b) -n
c) -o n
d) -t

Answer: b
Clarification: pr command offers many options which are used for producing the output in just the format we need. For example,
-d double spaces input
-n numbers lines, helps in debugging code
-o n offsets lines by n spaces, increases left margin of the page

$ pr  -t  -n  -d  -o  10  dept.lst
    1    01:accounts:6213
    2    02:admin:5423
    3    03:marketing:6521
    4    04:personnel:2365

7. -h option is used with pr command to add a header of our choice.
a) True
b) False

Answer: a
Clarification: If we are not using the -t option with pr command then we can use the -h option to add a header of our choice. This option is followed by the header string. For example,

$ pr  -h “Department List”  dept.lst

8. ___ operator is used with pr command to start printing from a specific page.
a) –
b) #
c) +
d) &

Answer: c
Clarification: There’s an option available with pr command which lets the user to print from a specific page number. This function is performed by prefixing a + with the page number. For example, to print from page number 5, use the following command:

9. Which command is used for displaying the beginning of a file?
a) pr
b) head
c) begin
d) lp

Answer: b
Clarification: The head command is used for displaying the top of the file. As the name suggests, it displays the beginning of the file which is specified as an argument to the command. For example,

10. By default, how many lines are displayed using the head command?
a) 5
b) 10
c) 4
d) 20

Answer: b
Clarification: The head command, as the name suggests displays the top of the file. When this command is used without any option, it displays the first ten lines of the file.

11. Which option is used with the head command to specify line count to display?
a) -a
b) -h
c) -n
d) -o

Answer: c
Clarification: We can use the -n option with the head command to specify a line count and display a specific number of lines of the file. -n option is followed by an integer value which donates the number of lines to be displayed. For example,

$ head -n  3 emp.lst            // displays first 3 lines of emp.lst

12. ___ command is used for displaying the end of the file.
a) head
b) tail
c) lp
d) pr

Answer: b
Clarification: Complementing the head command, the tail command is used to display the end of the file. It provides an additional feature of addressing lines. By default, it displays the last ten lines of the file.

13. We can also use -n option with tail command.
a) True
b) False

Answer: a
Clarification: Alike head command we can also use the -n option with the tail command to specify the line count to be displayed from the end of the file. For example,

$ tail  -n  3  emp.lst            // displays last three lines

14. Which symbol is used with the tail command to print the file from the selected line?
a) +
b) –
c) %%
d) ^

Answer: a
Clarification: We can also address lines from the beginning instead of the end. The +count option allows us to do that, where count represents the line number from where the selection should begin. For example,

$ tail +11 emp.lst        // start printing from 11th line onwards

15. Which of the following command is incorrect?
a) pr dept.lst
b) head -n 5 emp.lst
c) tail -o +5 emp.lst
d) tail -3 emp.lst

Answer: c
Clarification: The first command will prepare a file for printing by adding header, footers and formatted text. The command head -n 5 emp.lst will display the first 5 lines of emp.lst while the command tail -3 emp.lst will display the last 3 lines of emp.lst

Leave a Reply

Your email address will not be published. Required fields are marked *