This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Counting File Data: wc Command”.
1. Which command is used for counting words, lines and characters in a file?
Explanation: UNIX provides a universal word counting program that allows us to count the number of words, lines and characters in a file. It takes a filename as an argument as produces a 4 column output.
$ wc file01 3 9 45 file01
Above output indicates that file01 contains 3 lines, 9 words and 45 characters.
2. wc command cannot take multiple filenames as arguments.
Explanation: wc command can take multiple filenames as arguments. If multiple filenames are specified with wc command then it will produce a separate line of output for each file along with an additional line as a total count for all files specified. For example,
$ wc file01 file02 file03 12 45 170 file01 8 34 145 file02 10 38 158 file03 30 117 403 total
3. Which option is used for counting the number of lines in a file only.
Explanation: -l option when used with wc command display only the number of lines in the specified file.
$ wc -l file01 3 // number of lines in file01
4. Which option is used for counting the number of words in a file only?
Explanation: -w option when used with wc command display only the number of words in the specified file.
$ wc -w file01 15 // number of words in file01
5. Which option is used for counting the number of characters in a file only.
Explanation: -c option when used with wc command display only the number of characters in the specified file.
$ wc -c file01 45 // number of characters in file01
6. wc command can also work on a data stream.
Explanation: wc command when invoked without any special symbol ( like < or | ) or an argument it reads the input from standard input. we have to provide the input from the keyboard and mark the end of input with Ctrl-D. For example,
$ wc Sanfoundry Global Learning project Offer internships to students Ctrl-D 3 8 65 // 3 lines, 8 words and 65 characters
7. What does the following command do?
$ wc sample.txt > newfile
a) reads word count from sample.txt
b) reads word count from newfile
c) reads word count from sample.txt and write it to newfile
d) error is produced
Explanation: > symbol can be used with wc command for redirecting output. For example, the following command will read the input from sample.txt and redirect its output to newfile.
$ wc sample.txt > newfile
8. Which command is used for printing a file?
Explanation: lp command is used for printing a single copy of the file specified as an argument to lp command. For example,
$ lp abd.txt
Request id id prl-890 (1 file )
lp command notifies the request-id i.e. a combination of printer name(rpl) and job number (890).
9. Which option is used with lp command if there are more than one printers in the system?
Explanation: lp command prints the file as a default printer has been already specified by the system administrator. In case if there is more than one printer in the system we have to use the -d option with the printer name to print the file. For example, if there is another printer named laser_001 then,
$ lp -dlaser_001 abd.txt
10. Which option is used for printing multiple copies of a file using lp command?
Explanation: If we want to print multiple copies of a file, we can use the -n option followed by an integer i.e. the number of copies that we want to print. For example to print 5 copies of file abd.txt use the following command,
$ lp -n3 abd.txt
11. -t option prints the title on the first page.
Explanation: To print a specific title on the first page, we use the -t option followed by the string which we want to print as a title. For example,
$ lp -t "UNIX and Shell programming" abd.txt
12. What does the following command do?
a) cancel printing from printer ‘prl’
b) cancel printing current job
c) cancel printing job number 320 on printer name ‘prl’
d) undefined behavior
Explanation: cancel command is used to cancel the jobs submitted by the user for printing. cancel command can cancel the job only when it is in print queue i.e. waiting to be assigned to a printer. If the printer has already started processing the job, cancel command will not work.
13. Which command is used for knowing the file type?
d) type of file
Explanation: UNIX provides the file command for determining the type of file i.e. whether it is a text file, script file, archive file or any other type of file.
$ file file01 file01: Text file
14. file command identifies the file type by examining the magic number of the file.
Explanation: Magic number is the number which is embedded in the first few bytes of a file. Every file has a unique magic number which helps in identifying the file type.
15. UNIX offers a pager named ____ which has replaced the original pager of UNIX called ____
a) more, less
b) less, more
c) more, pg
d) pg, more
Explanation: The man command displays its output one page at a time. This is possible because it sends its output to a pager program. more pager is extensively used in UNIX system and it has replaced the original pager of UNIX i.e. pg. The task of a pager program is to simply display pages on the terminal. It can also be used to display the contents of a file.
$ more abd.txt // contents of the file named abd.txt will be displayed.
16. Which of the following is not an internal command for more?
Explanation: more pager offers a set of internal commands which are used for viewing pages differently. For scrolling one page at a time, use ‘f’ or the space bar. Similarly for scrolling backward press ‘b’ and for quitting the pager use ‘q’.
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.