Unix Questions and Answers – Displaying and Creating Files: cat Command

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Displaying and Creating Files: cat Command”.

1. Which command is used for displaying contents of a file?
a) cp
b) rm
c) cat
d) mkdir
View Answer

Answer: c
Explanation: cat command is used to display the contents of a file. For displaying the contents, simply invoke the cat command with the filename (whose contents are to be displayed) as an argument. For example, if abd.txt file contains “HELLO WORLD” then,

$ cat abd.txt
HELLO WORLD

advertisement
advertisement

2. Apart from displaying file contents, cat command is also used for _____ files.
a) displaying
b) deleting
c) copying
d) creating
View Answer

Answer: d
Explanation: cat command is one of the most well-known commands of the UNIX system. It is generally used to display the contents of a file on the terminal but can also be used for creating files. Suppose to create a file abd.txt, type the following command

$ cat  >  abd.txt

Now the prompt vanishes. cat command now waits for the input to be entered by the user. After the input is entered, the user hits the ctrl-D button which marks the end of the file and thus the file and the contents entered by the user are saved.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. Multiple arguments as filenames can be specified in cat command.
a) True
b) False
View Answer

Answer: a
Explanation: cat, like several other UNIX commands can accept more than one filename as arguments. For example,

advertisement
$ cat   file1   file2

Above command shows the contents of the second file immediately after the contents of the first file without any header information i.e. it concatenates the contents of two files on the terminal.

advertisement

4. Which symbol is used with cat command for creating files?
a) >
b) <
c) *
d) /
View Answer

Answer: a
Explanation: > symbol acts as a redirection symbol and records the input entered by the user in the specified file. For example,

$ cat  > file_01
UNIX is a powerful operating system        // input entered by the user
[CTRL-D]

5. If we create a file using cat command with the same filename which already exists in the current directory then,
a) existing file is deleted
b) new file will be created separately
c) existing file will be overwritten
d) an error will be produced
View Answer

Answer: c
Explanation: As cat command is also used to create files, so if we want to create a file with the same filename which already exists in the directory then the existing file will be overwritten.

6. Which symbol is used to append an existing file?
a) >
b) <
c) >>
d) $
View Answer

Answer: c
Explanation: cat command overwrites the existing file if we try to create another file with a similar filename as an existing file. To avoid this we can use the >> operator. For example, if file01 already exists and we want to append it, then use the following command:

$ cat  >> file01

7. Which option is used with cat command for displaying non-printable characters?
a) -v
b) -n
c) -x
d) -a
View Answer

Answer: a
Explanation: If the file that we want to display contains nonprintable ASCII characters, then to display those characters we use -v option with the cat command. -n option is used for displaying the contents of the file along with line numbers.

8. Which option is used with the cat command for displaying file with line numbers?
a) -n
b) -v
c) -a
d) -x
View Answer

Answer: a
Explanation: cat command supports -n option which is used for displaying file contents along with line number while -v is used for displaying nonprintable ASCII characters in the file.

9. Which of the following cannot be performed by cat command?
a) displaying files
b) creating files
c) appending files
d) deleting files
View Answer

Answer: d
Explanation: cat command cannot delete files. It can only be used for viewing file contents, creating a file or appending to an existing file.

10. What does cat file01 file01 file01 display?
a) error
b) blank terminal
c) contents of file01 three times successively
d) contents of file01 single time
View Answer

Answer: c
Explanation: When multiple filenames are specified as arguments with cat command, it concatenates the contents of all the files specified as arguments and displays the output on the terminal.

11. Which files will be displayed by the following command:

     cat  *file*

a) all files in the directory
b) all files with filename containing ‘file’
c) no files will be displayed
d) a single file
View Answer

Answer: b
Explanation: Since * is a meta-character, here it is being used for pattern matching. So above command will search all the files in the directory having ‘file’ as a part of their filename. After that it will concatenate the output of all the searched files and display them on the terminal.

12. Which command is used to create empty files?
a) cp
b) cat
c) touch
d) create
View Answer

Answer: c
Explanation: Touch command changes timestamps. It is also an easy way to create empty files.
The only argument required to be specified with touch command is the filename of the file to be created.

$ touch file01

13. Which option is used with touch command which forces the command not to create file, if it does not exists.
a) -h
b) -c
c) -t
d) -f
View Answer

Answer: b
Explanation: In case if there is a strict requirement that touch command should not create a new file, we can use the -c option with it. Else if the file already exists then it will do nothing.

$ touch -c newfile001

14. Which one of the following commands is incorrect?
a) cat file01
b) cat > file01
c) cat >> file1
d) cat -a file01
View Answer

Answer: d
Explanation: There is no such option as -a available with cat command.

 cat  file01		// display contents of file01
 cat > file01		// create a new file named 'file01'
 cat >> file1		// append to the file named 'file01'
 cat  -a file01		// invalid command

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.