250+ TOP MCQs on Redirection and Pipes and Answers

Unix Multiple Choice Questions & Answers (MCQs) on “Redirection and Pipes”.

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

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

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

Answer: b
Clarification: 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.

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

Answer: c
Clarification: 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

Answer: a
Clarification: 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) $

Answer: c
Clarification: 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 >>

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

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

Answer: b
Clarification: 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

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

Answer: c
Clarification: 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

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

Leave a Reply

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