Unit 3: File system management - Practice Quiz

CSE325 — Operating Systems Laboratory 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which header file is primarily required to use the open system call in a C program on a UNIX-like system?

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

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

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

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

A.
B.
C. NULL
D.

4 Which header file defines the read, write, close, and lseek system calls?

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

5 What is the integer value of the file descriptor representing Standard Input (stdin)?

A.
B.
C.
D.

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

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

7 Which flag must be included in open() to create the file if it does not exist?

A. O_NEW
B. O_CREAT
C. O_APPEND
D. O_MAKE

8 What does the read() system call return upon successfully reading data?

A. The file descriptor
B. A pointer to the buffer
C.
D. The number of bytes read

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

A.
B. EOF
C.
D.

10 Consider the syntax: ssize_t write(int fd, const void *buf, size_t count);. What does buf represent?

A. A pointer to the data to be written
B. The file path to write to
C. The file offset
D. The number of bytes to write

11 Which open flag ensures that data is always written to the end of the file?

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

12 What is the primary purpose of the lseek system call?

A. To change the read/write file offset
B. To find a file on the disk
C. To lock a file
D. To search for a string in a file

13 In the call lseek(fd, offset, whence), what does SEEK_SET indicate for the whence parameter?

A. The offset is set to the beginning of the file
B. The offset is relative to the end of the file
C. The offset is invalid
D. The offset is relative to the current position

14 Which lseek mode allows setting the offset relative to the current file position?

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

15 What is the return type of lseek()?

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

16 How can you use lseek to find the total size of a file?

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

17 What happens if lseek is called on a file descriptor referring to a pipe or socket?

A. It works normally
B. It returns
C. It returns and sets errno to ESPIPE
D. It closes the pipe

18 What is the correct prototype for the close system call?

A. int close(char *filename);
B. int close(int fd);
C. int close(FILE *fp);
D. void close(int fd);

19 Why is it important to use close() on file descriptors when they are no longer needed?

A. To prevent the OS from crashing
B. To clear the file content
C. To delete the file from the disk
D. To release the file descriptor resource back to the system

20 When open() is called with O_TRUNC and the file already exists, what happens?

A. The file is opened in append mode
B. The file is deleted and a new one is created
C. The function returns an error
D. The file length is truncated to

21 The third argument of open(), typically called mode, is mandatory only when:

A. O_TRUNC is specified in flags
B. Opening a file in read-only mode
C. Opening a system file
D. O_CREAT is specified in flags

22 Which system call corresponds to the Standard C Library function fprintf?

A. open
B. write
C. read
D. lseek

23 What is the value of fd if fd = open("file.txt", O_RDONLY) is the first file opened by a process (excluding standard streams)?

A.
B.
C.
D.

24 Which data type is ssize_t (returned by read and write)?

A. Character pointer
B. Signed integer
C. Unsigned integer
D. Floating point

25 What happens if you invoke write(fd, buf, count) where fd was opened with O_RDONLY?

A. It returns and sets errno to EBADF
B. It prompts the user for permission
C. It writes successfully
D. It crashes the program (Segfault)

26 Using lseek, how would you create a "hole" in a file?

A. By seeking past the end of the file and then writing data
B. By writing \0 characters manually
C. It is not possible to create holes
D. By seeking to the beginning and truncating

27 In the argument list for write(int fd, const void *buf, size_t count), what does count signify?

A. The offset where writing begins
B. The size of the file
C. The capacity of the buffer
D. The number of bytes requested to be written

28 Which header contains the permission bit macros like S_IRUSR, S_IWUSR used in open()?

A. <stdio.h>
B. <unistd.h>
C. <stdlib.h>
D. <sys/stat.h>

29 What does lseek(fd, -10, SEEK_CUR) do?

A. Moves the offset forward by 10 bytes
B. Returns an error immediately
C. Moves to the 10th byte from the end
D. Moves the offset backward by 10 bytes

30 If open is used with O_EXCL and O_CREAT, what happens if the file already exists?

A. It opens the existing file
B. It appends to the existing file
C. The call fails (returns )
D. It overwrites the existing file

31 What is the file descriptor for Standard Output (stdout)?

A.
B.
C.
D.

32 Which system call is used to duplicate a file descriptor?

A. dup()
B. fork()
C. copy()
D. clone()

33 What happens to the file offset after a successful read() operation?

A. It remains unchanged
B. It increments by the number of bytes read
C. It moves to the end of the file
D. It resets to 0

34 Which of the following is NOT a valid mode for open()?

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

35 What is the return value of close(fd) if fd is not a valid open file descriptor?

A.
B.
C.
D. NULL

36 Which type matches the count argument in read and write?

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

37 Which macro sets the file permission to rw-r--r-- (User read/write, Group read, Others read) in octal?

A.
B.
C.
D.

38 If read(fd, buf, 100) returns 50, what does this imply?

A. An error occurred
B. The buffer size was too small
C. Only 50 bytes were available or read from the file/stream
D. The file pointer is invalid

39 What is the equivalent of lseek(fd, 0, SEEK_SET) using standard C library functions?

A. fsetpos(fp, NULL)
B. fseek(fp, 0, SEEK_END)
C. rewind(fp)
D. ftell(fp)

40 Can open() typically open a directory for reading?

A. Yes, but generally only to read directory entries (system dependent)
B. Yes, for writing data to it
C. Yes, on all systems
D. No, never

41 What happens if you attempt to lseek to a negative resulting absolute offset?

A. It crashes
B. It sets the offset to
C. The offset wraps around
D. It returns and sets errno to EINVAL

42 The global variable used to identify the specific error code after a system call fails is:

A. std_err
B. sys_err
C. error
D. errno

43 Which function is often used to print a human-readable error message describing the last error encountered?

A. printf
B. write
C. strcat
D. perror

44 If a process calls fork(), what happens to open file descriptors?

A. Only stdin/stdout are inherited
B. They are closed in the child
C. They are inherited by the child
D. They are reset to offset 0

45 What is the maximum number of bytes read can theoretically request?

A. Unlimited
B. 4096 bytes
C. 255 bytes
D. Depends on ssize_t max value (typically SSIZE_MAX)

46 Which of these pairs represents the correct types for the arguments of lseek?

A. int fd, off_t offset, int whence
B. int fd, int offset, char *whence
C. FILE *fp, long offset, int whence
D. int fd, size_t offset, int whence

47 If fd is closed, and you call close(fd) again, what happens?

A. Undefined behavior (possible crash in some implementations)
B. Returns (EBADF)
C. Memory leak
D. Nothing, it is safe

48 How does open distinguish between text and binary files in UNIX/Linux?

A. It does not distinguish; all files are byte streams
B. Using O_TEXT and O_BINARY flags
C. By the file extension
D. By checking the file header

49 What is the effect of O_SYNC flag in open?

A. Allows multiple processes to write simultaneously
B. Opens file in synchronous non-blocking mode
C. Synchronizes the file offset
D. Writes block until data is physically stored on the disk

50 Which buffer pointer type is used in the read prototype ssize_t read(int fd, void *buf, size_t count)?

A. char *
B. void *
C. int *
D. struct file *