Unix Questions and Answers – Redirection and Pipes – 1

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Redirection and Pipes – 1”.

1. Redirection is a process of switching of the standard stream of data.
a) True
b) False
View Answer

Answer: a
Explanation: Redirection is a process in which switching of the standard stream of data is performed so that it comes from a source other than its default source or so that it goes to some destination other than its default destination.

2. How many files are used for representing different standard streams?
a) 1
b) 2
c) 4
d) 3
View Answer

Answer: d
Explanation: The shell associates three files with the terminal –two for display and one for the keyboard. These special files are actually streams of characters which many commands see as input and output. Each stream is associated with a default device –which is terminal. These three files are:
• Standard Input
• Standard output
• Standard error

3. Which stream is connected to the display?
a) standard input
b) standard output
c) standard error
d) error
View Answer

Answer: b
Explanation: Standard output is the file (or stream) which is used for representing output, and is connected to the display. Each command which uses the display for its output will find this file always open and available. The file will be automatically closed after the command has completed its execution.
advertisement
advertisement

4. Which stream is used for representing error messages?
a) standard input
b) standard output
c) standard error
d) error
View Answer

Answer: c
Explanation: The standard error (or stream) is used for representing error messages that emanate from the command or shell. This stream is also connected to the display as error messages are displayed on the terminal.

5. The command wc < sample.txt will count data from the file sample.txt.
a) True
b) False
View Answer

Answer: a
Explanation: When wc is used without any arguments it will read the data from the default source which is a keyboard. In the above command, redirection symbol < is present. The following steps are performed:
1. On seeing the <, the shell opens the disk file, sample.txt for reading.
2. wc performs its execution and displays the output.

6. Which symbol is used for taking input from standard input?
a) &
b) %
c) –
d) $
View Answer

Answer: c
Explanation: When a command takes input from multiple sources –say a file and standard input, the – symbol is used to indicate the sequence of taking input. For example,

// first take input from standard input and then foo
$ cat  -  foo
 
// first take input from foo then from standard input and after that from bar
$ cat  foo  -  bar

7. Which of the following symbol(s) can be used to redirect the output to a file or another program?
a) |
b) >
c) >>
d) |, > and >>
View Answer

Answer: d
Explanation: All commands displaying output on the terminal actually write to the standard output file as a stream of characters and not directly to the terminal as such. The symbol > will replace the default destination (terminal) with any file by using the > operator, followed by the filename while the symbol >> is used to append to a file. | is used to give input to another program.
advertisement

8. The >> symbol is used to overwrite the existing file if it exists.
a) True
b) False
View Answer

Answer: b
Explanation: The shell provides the >> symbol (right chevron used twice) to append to an existing file. For example,
//Do a word count program on sample.txt and append the output to newfile as shown below.
$ wc sample.txt >> newfile
advertisement

9. Which file descriptor is used to represent standard error stream?
a) 0
b) 1
c) 2
d) 3
View Answer

Answer: c
Explanation: Each of the three standard files is represented by a number called as a file descriptor. A file is opened using its pathname, but subsequent read and write operations identify the file by this file descriptor. Whenever we enter an incorrect command or try to open a non-existent file, certain diagnostic messages are displayed onto the terminal. This is the standard error stream whose default destination is the terminal.

10. We can redirect the error message to file named newfile using __________ command.
a) cat foo errorfile
b) cat foo >errorfile
c) cat errorfile>foo
d) cat foo 2>errorfile
View Answer

Answer: d
Explanation: Redirecting standard error requires the 2> symbol. Suppose if foo doesn’t exist then an error message ‘cannot open foo’ will be generated but we can send this message to another file. But here > and >> cannot be used. We have to use 2> symbol as the file descriptor number 2 represents the standard error stream.

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.