Unix Questions and Answers – System Calls Basics – 2

This set of Unix Question Bank focuses on “System Calls Basics – 2”.

1. Which of the following system call is used for closing a file?
a) open
b) lseek
c) close
d) write
View Answer

Answer: c
Explanation: A program automatically closes all open files before termination, but it’s a good practice to close them explicitly. The close system call is used for closing a file.
int close (int fd)

2. close system call returns ____
a) 0
b) -1
c) 1
d) 0 and -1
View Answer

Answer: d
Explanation: The return type of close system call is an integer. It either returns 0 if the file is closed successfully or -1 otherwise.

3. ____ system call is used for writing to a file.
a) read
b) write
c) close
d) seek
View Answer

Answer: b
Explanation: write system call is required for writing to a file which has previously been opened with the open system call. write system call returns the number of characters written.
advertisement
advertisement

4. write system call returns -1 when ___________
a) if disk fills up while write is in progress
b) when file doesn’t exist
c) if the file size exceeds the system’s limit
d) if disk fills up while write is in progress and if the file size exceeds
View Answer

Answer: d
Explanation: write system call returns the number of characters written. However, it will return -1 if if disk fills up while write is in progress or if the file size exceeds the system’s limit.

5. ____ system call is used for positioning the offset pointer.
a) read
b) write
c) open
d) lseek
View Answer

Answer: d
Explanation: The lseek system call moves the file offset pointer to a specified point. It doesn’t do any physical I/O rather it determines the position in the file where the next I/O operation will take place.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Which of the following offset is used with lseek system call to set the offset pointer to the end of the file?
a) SEEK_SET
b) SEEK_END
c) SEEK_CUR
d) SEEK_CR
View Answer

Answer: b
Explanation: The offset signifies the position of the offset pointer which can take one of these three values:

 SEEK_SET    -    offset pointer set to the beginning of file
 SEEK_END    -    offset pointer set to the end of file
 SEEK_CUR    -    offset pointer remains at current location

advertisement

7. Which of the following system call is used for truncating a file?
a) truncate
b) ftruncate
c) trunk
d) truncate and ftruncate
View Answer

Answer: d
Explanation: The truncate and ftruncate calls can truncate a file to any length. These calls are often used in combination with lseek to overwrite a certain segment of a file.
advertisement

8. truncate needs the ___ of the file as an argument but ftruncate works with _______
a) pathname, file descriptor
b) file descriptor, pathname
c) pathname, pathname
d) file descriptor, file descriptor
View Answer

Answer: a
Explanation: The truncate and ftruncate calls can truncate a file to any length. truncate needs the pathname of the file as an argument but ftruncate works with the file descriptor.

Sanfoundry Global Education & Learning Series – Unix.

To practice Unix Question Bank, 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.