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

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

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

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

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

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

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

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

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

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

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

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_MAKE
D. O_APPEND

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

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

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

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

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

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

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 find a file on the disk
C. To lock a file
D. To change the read/write file offset

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 set to the beginning of the file
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_SET
B. SEEK_CUR
C. SEEK_NOW
D. SEEK_END

15 What is the return type of lseek()?

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

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 and sets errno to ESPIPE
C. It closes the pipe
D. It returns $0$

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

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

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 delete the file from the disk
C. To clear the file content
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 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 file in read-only mode
B. O_CREAT is specified in flags
C. O_TRUNC is specified in flags
D. Opening a system file

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

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

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

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 writing \0 characters manually
B. It is not possible to create holes
C. By seeking past the end of the file and then writing data
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 capacity of the buffer
C. The number of bytes requested to be written
D. The size of the file

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

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

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

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

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

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

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

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

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

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

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

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

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

A. O_EXECUTE
B. O_RDONLY
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. $0$
B.
C. $1$
D. NULL

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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. 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. By checking the file header
C. It does not distinguish; all files are byte streams
D. Using O_TEXT and O_BINARY flags

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. Writes block until data is physically stored on the disk
D. Synchronizes the file offset

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

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