Unix Questions and Answers – Redirection and Pipes – 2

This set of Unix Assessment Questions and Answers focuses on “Redirection and Pipes – 2”.

1. The category of commands which uses both standard input and standard output are called ____
a) directory oriented commands
b) standard input commands
c) filters
d) standard output commands
View Answer

Answer: c
Explanation: UNIX commands can be grouped into four categories:
Directory-oriented (mkdir,rmdir)
Commands like ls, pwd that write to standard output
Commands like lp that read from standard input
Filters – commands which use both standard input and standard output (like bc)

2. The contents of file001 are:

    232
    25*50

What will be the contents of result.txt?

advertisement
advertisement
bc  < file001 > result.txt

a) undefined
b) erroneous
c) 429467296, 1250
d) 1650
View Answer

Answer: c
Explanation: We can redirect bc’s standard input to come from file001 and save the output in another file named result.txt. bc obtained the expressions from the redirected standard input, processed them and sent out the results to a redirected output stream. Here bc command is used as a filter.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. Which of the following files are known as special files in UNIX?
a) sample.txt
b) /dev/null
c) /dev/tty
d) /dev/null and /dev/tty
View Answer

Answer: d
Explanation: There are two special files named /dev/null and /dev/tty which is used in UNIX for special purposes. For example, if we want to check whether the program runs successfully without seeing its output on the screen or may not want to save output in a file either. For this purpose, a special file /dev/null is used. It accepts any stream without growing in size. The second file /dev/tty is the one used for indicating one’s terminal.
advertisement

4. The size of /dev/null is always zero.
a) True
b) False
View Answer

Answer: a
Explanation: UNIX has a special file that simply accepts any stream without growing in size. This file is /dev/null. This file is used for redirecting error messages away from the terminal so that they don’t appear on the screen. This file is pseudo-device because like other device files, it’s not associated with any physical device.

5. Which symbol is used to redirect error messages to /dev/null?
a) %
b) $
c) >
d) <
View Answer

Answer: c
Explanation: As we redirect our output using > symbol to other files in simple commands, this technique can also be used to redirect error messages to /dev/null file. For example,

advertisement
$ cmp  file01  file02 >  /dev/null

6. Which file is used for indicating terminals?
a) /dev/tty
b) /dev/null
c) terminal file
d) device file
View Answer

Answer: a
Explanation: /dev/tty is another special file which is used for representing one’s terminal. Consider user1 is working on terminal /dev/pts/1 and user 2 is working on /dev/pts/2. Then, both the users can refer to their own terminals with the same filename. For example, if user 1 issues the command

$ who > /dev/tty

Then, the list of users is sent to the terminal he is currently using — dev/pts/1. Similarly, user 2 can also issue the same command to see the output on his terminal, /dev/pts/2.
/dev/tty can be accessed by multiple users without conflict.

7. Which symbol is used to connect different streams?
a) |
b) >
c) <
d) ~
View Answer

Answer: a
Explanation: The shell can connect different streams using a special operator called pipe (|) and avoid creation of disk file. We can connect two commands using this operator so that one command can take the output of other as its input. For example,

$ who | wc -l        // count number of lines in the output of who command.

8. We can use the > symbol along with pipe in a command.
a) True
b) False
View Answer

Answer: a
Explanation: There is no restriction on the number of commands we can use in a pipeline except that we should know the behavioral properties of these commands to place there. For example, consider this command:

$ ls  | wc  -l  > output.txt    
// number of lines counted by wc command from the output of ls //are stored in a file named output.txt.

9. What is the output of the following command?

$ wc  -c  *.c

a) total size of .c files
b) size of each .c file
c) undefined
d) erroneous
View Answer

Answer: a
Explanation: We can make the command ignorant of its source using the pipeline. Above command is also an example of the same. It will simply display the total size of those files having a .c extension in the current directory.

10. In a pipeline, the command on the left of | must use standard output and one on the right must use standard input.
a) True
b) False
View Answer

Answer: a
Explanation: There is a restriction which is applied while using a pipeline. In a pipeline, the command on the left of | must use standard output (stream) and one on the right must use standard input (stream). Otherwise, an error will be generated or the output will be erroneous.

11. Which command saves one copy of character stream in a file and store the other to standard output?
a) who
b) ls
c) tee
d) wc
View Answer

Answer: c
Explanation: tee command handles a character stream by duplicating its input. It saves one copy in a file and writes the other to standard output. For example,

$ who  | tee  user.txt

Above command will the save the output of who command in a file named user.txt as well as it will also display the output of who command on the terminal.

12. tee is an internal command.
a) True
b) False
View Answer

Answer: b
Explanation: tee is an external command and not a feature of the shell. tee command handles a character stream by duplicating its input. It saves one copy in a file and writes the other to standard output.

13. Which of the following is not true about tee command?
a) it is a feature of the shell
b) it is an external command
c) tee command duplicates its input
d) tee is an internal command
View Answer

Answer: a
Explanation: tee command is not a feature of the shell. tee command handles a character stream by duplicating its input. It saves one copy in a file and writes the other to standard output. It is an external command.

14. Apart from its use in redirection, /dev/tty can also be used as an argument to some commands.
a) True
b) False
View Answer

Answer: a
Explanation: /dev/tty can be used as an argument to some UNIX commands. Because the terminal is also a file we can use the device name /dev/tty as an argument to some commands like tee. For example,

$ who  |  tee  /dev/tty  | wc  -l            // same as who | wc  -l

In the above command, tee saved the output of who in /dev/tty, thus effectively showing it on the screen and wc -l counted the number of lines from another stream.

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of Unix Assessment Questions, 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.