Unit 3 - Practice Quiz

CSE325

1 Which header file is primarily required to use the open() system call and its flags like O_RDONLY?

A. <unistd.h>
B. <fcntl.h>
C. <stdio.h>
D. <stdlib.h>

2 Which header file defines the read(), write(), and close() system calls?

A. <fcntl.h>
B. <sys/types.h>
C. <unistd.h>
D. <file.h>

3 What is the return type of the open() system call?

A. FILE *
B. int
C. void
D. size_t

4 If the open() system call fails, what value does it return?

A.
B. 1
C. -1
D. NULL

5 Which integer value typically represents the Standard Input (stdin) file descriptor?

A.
B. 1
C. 2
D. -1

6 Which flag is used with open() to open a file for writing only?

A. O_RDONLY
B. O_RDWR
C. O_WRONLY
D. O_WRITE

7 Consider the following code snippet:
int fd = open("file.txt", O_CREAT | O_WRONLY, 0644);
What is the purpose of the third argument (0644)?

A. It specifies the file size.
B. It specifies the file offset.
C. It specifies the file permissions (mode) if the file is created.
D. It specifies the buffer size.

8 Which bitwise operator is used to combine multiple flags in the open() system call?

A. & (AND)
B. | (OR)
C. ^ (XOR)
D. ~ (NOT)

9 What does the read(fd, buf, count) system call return on success?

A. The file descriptor
B.
C. The number of bytes actually read
D. The total size of the file

10 What is the return value of read() when the end of the file (EOF) is reached?

A. -1
B.
C. 1
D. EOF

11 What is the primary difference between write() and the standard library function fwrite()?

A. write is a system call, fwrite is a library function.
B. write is buffered, fwrite is unbuffered.
C. write uses FILE*, fwrite uses file descriptors.
D. There is no difference.

12 Which flag ensures that every write() operation appends data to the end of the file, regardless of the current file offset?

A. O_TRUNC
B. O_APPEND
C. O_CREAT
D. O_END

13 What does the close(fd) system call return upon successful completion?

A. -1
B. 1
C.
D. The file descriptor

14 What happens if you attempt to write() to a file descriptor that was opened with O_RDONLY?

A. The write succeeds but data is discarded.
B. The system call returns -1 and sets errno.
C. The program crashes with a segmentation fault.
D. The file permissions are automatically changed to allowed writing.

15 Which lseek directive is used to set the file offset relative to the beginning of the file?

A. SEEK_CUR
B. SEEK_END
C. SEEK_SET
D. SEEK_BEGIN

16 Consider: off_t new_pos = lseek(fd, 0, SEEK_CUR);
What does new_pos contain after this call?

A. The size of the file.
B. The beginning of the file.
C. The current file offset.
D.

17 Which code snippet correctly calculates the size of a file using lseek?

A. lseek(fd, 0, SEEK_SET)
B. lseek(fd, 0, SEEK_END)
C. lseek(fd, 1, SEEK_END)
D. lseek(fd, 0, SEEK_CUR)

18 What is the data type of the file offset returned by lseek?

A. int
B. long
C. off_t
D. size_t

19 What does the O_TRUNC flag do when used with open()?

A. It creates the file if it does not exist.
B. It truncates the file length to 0 if it exists and is successfully opened for writing.
C. It appends data to the end of the file.
D. It opens the file in binary mode.

20 If open() is called with O_CREAT but without O_EXCL, and the file already exists, what happens?

A. The call fails.
B. The existing file is opened.
C. A new file is created with a different name.
D. The existing file is deleted.

21 What happens if you use lseek() on a file descriptor associated with a pipe or socket?

A. It works normally.
B. It returns -1 and sets errno to ESPIPE.
C. It rewinds the pipe.
D. It crashes the program.

22 In the call write(fd, buf, count), what does the count argument represent?

A. The size of the buffer buf.
B. The number of bytes the user wants to write.
C. The file offset where writing begins.
D. The file descriptor index.

23 What is the value of the file descriptor returned by open() when successful?

A. A random positive integer.
B. The lowest-numbered file descriptor not currently open for the process.
C. Always 3.
D. The process ID.

24 Which type is used for the count argument (number of bytes) in read() and write()?

A. int
B. long
C. size_t
D. off_t

25 What is the result of lseek(fd, -10, SEEK_END)?

A. It sets the offset to the 10th byte of the file.
B. It sets the offset to 10 bytes before the end of the file.
C. It is invalid and returns error.
D. It sets the offset to 10 bytes past the end of the file.

26 What is a "file hole" created by lseek?

A. A corrupted section of the disk.
B. An area of a file accessed by seeking past the end of the file and then writing.
C. A file that cannot be closed.
D. A file with permission bits set to 0.

27 Which error code is typically set if open() fails because the file does not exist (and O_CREAT was not specified)?

A. EACCES
B. EEXIST
C. ENOENT
D. EINTR

28 What happens to the file offset after a successful read(fd, buf, 100)?

A. It remains unchanged.
B. It is incremented by 100 (or the actual bytes read).
C. It is set to the end of the file.
D. It is reset to 0.

29 The ssize_t return type of read() and write() is:

A. Always unsigned.
B. Signed, to accommodate -1 for errors.
C. A floating point number.
D. A structure.

30 Which of the following is NOT a valid mode for lseek()?

A. SEEK_SET
B. SEEK_CUR
C. SEEK_END
D. SEEK_START

31 If a process calls close(1), what effect does it have?

A. It closes Standard Input.
B. It closes Standard Output.
C. It terminates the process.
D. It closes Standard Error.

32 What does O_EXCL combined with O_CREAT ensure?

A. The file is created with exclusive lock.
B. The call fails if the file already exists.
C. The file is opened for exclusive reading.
D. The file permissions are exclusive to the owner.

33 Which permission value in octal represents read/write for owner, read-only for group, and read-only for others?

A. 0777
B. 0644
C. 0755
D. 0600

34 In the statement read(fd, buf, n), who is responsible for allocating memory for buf?

A. The kernel.
B. The read function automatically.
C. The programmer (caller).
D. The file system.

35 What does O_RDWR stand for?

A. Open for Reading and Writing.
B. Open for Reading or Writing.
C. Open for Redirection.
D. Open Read Write Execute.

36 Which system call is used to delete a file name from the filesystem?

A. delete()
B. remove()
C. unlink()
D. erase()

37 If you perform lseek(fd, 10, SEEK_SET), where is the file pointer positioned?

A. 10 bytes from the end.
B. 10 bytes from the current position.
C. At the 10th byte (offset 10) from the beginning.
D. At offset 0.

38 What happens if a process forks regarding open file descriptors?

A. The child process starts with no open files.
B. The child inherits copies of the parent's open file descriptors.
C. The parent's files are closed.
D. The child gets new file descriptors pointing to different file table entries.

39 Why is it important to check the return value of write()?

A. To verify the file exists.
B. To ensure the file was closed.
C. To handle cases where fewer bytes were written than requested (partial write) or an error occurred.
D. It is not important.

40 What is the maximum number of files a process can open?

A. Unlimited.
B. Limited by system resources and configuration (ulimit).
C. Always 1024.
D. Always 64.

41 When opening a file with O_CREAT, which argument must be supplied that is otherwise optional?

A. Buffer size
B. Mode (permissions)
C. File descriptor
D. Timeout

42 What does the EBADF error indicate?

A. Bad Address Format.
B. Bad File Descriptor.
C. Bus Error.
D. Buffer Full.

43 If read() is interrupted by a signal before reading any data, what does it typically return?

A.
B. -1 (with errno = EINTR)
C. The number of bytes read so far.
D. It restarts automatically always.

44 Which flag allows a file to be opened in non-blocking mode?

A. O_NOBLOCK
B. O_NONBLOCK
C. O_ASYNC
D. O_FAST

45 In the command cat file.txt > output.txt, the shell opens output.txt and uses dup2 to redirect which file descriptor?

A. stdin (0)
B. stdout (1)
C. stderr (2)
D. fd 3

46 If you open a file with O_WRONLY | O_APPEND, and two processes write to it simultaneously, what guarantees does the OS provide?

A. None, data will be overwritten.
B. Atomic append (data from writes will not be interleaved/overwritten).
C. The file will be locked.
D. The second process will fail.

47 What is the typical prototype for the open system call?

A. int open(const char *pathname, int flags, ...);
B. void open(char *name);
C. FILE *open(const char *path, char *mode);
D. int open(int fd, int flags);

48 Which of the following refers to the specific entry in the system-wide open file table?

A. File Descriptor
B. Open File Description
C. Inode
D. File Path

49 If lseek returns -1, which variable should be checked to determine the error?

A. status
B. error_code
C. errno
D. return_val

50 Can read() return fewer bytes than requested (count) without it being an error?

A. No, it always returns count or -1.
B. Yes, if EOF is near or reading from a pipe/terminal.
C. Yes, but errno will be set.
D. No, that implies data corruption.