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. <stdio.h>
B. <string.h>
C. <stdlib.h>
D. <fcntl.h>

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

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

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

A.
B.
C.
D. NULL

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

A. <unistd.h>
B. <stddef.h>
C. <fcntl.h>
D. <sys/types.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_RDWR
B. O_WRONLY
C. O_READ
D. O_RDONLY

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

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

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

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

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

A.
B.
C.
D. EOF

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

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

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

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

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

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

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

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

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

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

15 What is the return type of lseek()?

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

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

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

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

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

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

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

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

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

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

A. The function returns an error
B. The file is deleted and a new one is created
C. The file is opened in append mode
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 system file
C. Opening a file in read-only mode
D. O_CREAT is specified in flags

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

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

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. Floating point
C. Unsigned integer
D. Signed integer

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

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

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

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

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

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

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

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

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

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

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. clone()
C. copy()
D. fork()

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

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

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

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

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

A.
B. NULL
C.
D.

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

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

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. Only 50 bytes were available or read from the file/stream
C. The file pointer is invalid
D. The buffer size was too small

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

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

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

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

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

A. It sets the offset to
B. It crashes
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. errno
B. sys_err
C. std_err
D. error

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

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

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

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

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

A. 255 bytes
B. Unlimited
C. 4096 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, int offset, char *whence
B. FILE *fp, long offset, int whence
C. int fd, size_t offset, int whence
D. int fd, off_t offset, int whence

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

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

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

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

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. struct file *
B. int *
C. char *
D. void *