Unit 3 - Practice Quiz

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

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

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

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

A. $0$
B. $1$
C. NULL
D.

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

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

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

A.
B. $1$
C. $0$
D. $2$

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

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

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

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

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

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

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

A.
B. $0$
C. $1$
D. EOF

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 offset
C. The file path to write to
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_TRUNC
B. O_APPEND
C. O_END
D. O_LAST

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

A. To find a file on the disk
B. To change the read/write file offset
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 relative to the current position
B. The offset is invalid
C. The offset is set to the beginning of the file
D. The offset is relative to the end of the file

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

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

15 What is the return type of lseek()?

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

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

A. lseek(fd, 1, SEEK_END)
B. lseek(fd, 0, SEEK_SET)
C. lseek(fd, 0, SEEK_CUR)
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 works normally
B. It returns $0$
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(FILE *fp);
C. void close(int fd);
D. int close(int fd);

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

A. To clear the file content
B. To release the file descriptor resource back to the system
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 opened in append mode
C. The file is deleted and a new one is created
D. The file length is truncated to $0$

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

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

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

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

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. $0$
B. $1$
C. $3$
D. $2$

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

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

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 crashes the program (Segfault)
C. It prompts the user for permission
D. It writes successfully

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

A. By seeking to the beginning and truncating
B. It is not possible to create holes
C. By writing \0 characters manually
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 number of bytes requested to be written
C. The offset where writing begins
D. The capacity of the buffer

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

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

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

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

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

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

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

A. $2$
B. $3$
C. $0$
D. $1$

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

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

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

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

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

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

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

A. NULL
B. $0$
C. $1$
D.

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

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

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

A. $0755$
B. $0600$
C. $0644$
D. $0666$

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

49 What is the effect of O_SYNC flag in open?

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

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

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