Unix Questions and Answers – Counting File Data: wc Command

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?
a) diff
b) count
c) man
d) wc
View Answer

Answer: d
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.

advertisement
advertisement

2. wc command cannot take multiple filenames as arguments.
a) True
b) False
View Answer

Answer: b
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.
a) -l
b) -W
c) -c
d) -w
View Answer

Answer: a
Explanation: -l option when used with wc command display only the number of lines in the specified file.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ wc  -l  file01
3        // number of lines in file01

4. Which option is used for counting the number of words in a file only?
a) -l
b) -W
c) -c
d) -w
View Answer

Answer: d
Explanation: -w option when used with wc command display only the number of words in the specified file.

advertisement
$ wc  -w  file01
15        // number of words in file01

5. Which option is used for counting the number of characters in a file only.
a) -l
b) -W
c) -c
d) -w
View Answer

Answer: c
Explanation: -c option when used with wc command display only the number of characters in the specified file.

advertisement
$ wc  -c  file01
45        // number of characters in file01

6. wc command can also work on a data stream.
a) True
b) False
View Answer

Answer: a
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
View Answer

Answer: c
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?
a) lp
b) pr
c) pg
d) more
View Answer

Answer: a
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?
a) -t
b) -d
c) -i
d) -p
View Answer

Answer: b
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?
a) -l
b) -i
c) -t
d) -n
View Answer

Answer: d
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  -n5  abd.txt

11. -t option prints the title on the first page.
a) True
b) False
View Answer

Answer: a
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?

  cancel prl-320

a) cancel printing from printer ‘prl’
b) cancel printing current job
c) cancel printing job number 320 on printer name ‘prl’
d) undefined behavior
View Answer

Answer: c
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?
a) file
b) type
c) filetype
d) type of file
View Answer

Answer: a
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.
a) True
b) False
View Answer

Answer: a
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
View Answer

Answer: c
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?
a) q
b) f
c) b
d) z
View Answer

Answer: d
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.