Unit 3 - Practice Quiz
1
Which header file is primarily required to use the open system call in a C program on a UNIX-like system?
<fcntl.h>
<string.h>
<stdlib.h>
<stdio.h>
2
What is the return type of the open() system call?
void
int
FILE *
char *
3
If the open() system call fails, what value does it return?
NULL
4
Which header file defines the read, write, close, and lseek system calls?
<stddef.h>
<fcntl.h>
<unistd.h>
<sys/types.h>
5
What is the integer value of the file descriptor representing Standard Input (stdin)?
6
Which flag is used with open() to open a file for reading only?
O_RDWR
O_READ
O_WRONLY
O_RDONLY
7
Which flag must be included in open() to create the file if it does not exist?
O_CREAT
O_NEW
O_APPEND
O_MAKE
8
What does the read() system call return upon successfully reading data?
9
What is the return value of read() when the end of the file (EOF) is reached?
EOF
10
Consider the syntax: ssize_t write(int fd, const void *buf, size_t count);. What does buf represent?
11
Which open flag ensures that data is always written to the end of the file?
O_TRUNC
O_APPEND
O_END
O_LAST
12
What is the primary purpose of the lseek system call?
13
In the call lseek(fd, offset, whence), what does SEEK_SET indicate for the whence parameter?
14
Which lseek mode allows setting the offset relative to the current file position?
SEEK_NOW
SEEK_SET
SEEK_END
SEEK_CUR
15
What is the return type of lseek()?
long
int
size_t
off_t
16
How can you use lseek to find the total size of a file?
lseek(fd, 1, SEEK_END)
lseek(fd, 0, SEEK_SET)
lseek(fd, 0, SEEK_CUR)
lseek(fd, 0, SEEK_END)
17
What happens if lseek is called on a file descriptor referring to a pipe or socket?
errno to ESPIPE
18
What is the correct prototype for the close system call?
int close(char *filename);
int close(FILE *fp);
void close(int fd);
int close(int fd);
19
Why is it important to use close() on file descriptors when they are no longer needed?
20
When open() is called with O_TRUNC and the file already exists, what happens?
21
The third argument of open(), typically called mode, is mandatory only when:
O_TRUNC is specified in flags
O_CREAT is specified in flags
22
Which system call corresponds to the Standard C Library function fprintf?
lseek
write
open
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)?
24
Which data type is ssize_t (returned by read and write)?
25
What happens if you invoke write(fd, buf, count) where fd was opened with O_RDONLY?
errno to EBADF
26
Using lseek, how would you create a "hole" in a file?
\0 characters manually
27
In the argument list for write(int fd, const void *buf, size_t count), what does count signify?
28
Which header contains the permission bit macros like S_IRUSR, S_IWUSR used in open()?
<stdio.h>
<sys/stat.h>
<stdlib.h>
<unistd.h>
29
What does lseek(fd, -10, SEEK_CUR) do?
30
If open is used with O_EXCL and O_CREAT, what happens if the file already exists?
31
What is the file descriptor for Standard Output (stdout)?
32 Which system call is used to duplicate a file descriptor?
dup()
copy()
fork()
clone()
33
What happens to the file offset after a successful read() operation?
34
Which of the following is NOT a valid mode for open()?
O_EXECUTE
O_RDONLY
O_WRONLY
O_RDWR
35
What is the return value of close(fd) if fd is not a valid open file descriptor?
NULL
36
Which type matches the count argument in read and write?
size_t
int
long
ssize_t
37
Which macro sets the file permission to rw-r--r-- (User read/write, Group read, Others read) in octal?
38
If read(fd, buf, 100) returns 50, what does this imply?
39
What is the equivalent of lseek(fd, 0, SEEK_SET) using standard C library functions?
fseek(fp, 0, SEEK_END)
rewind(fp)
fsetpos(fp, NULL)
ftell(fp)
40
Can open() typically open a directory for reading?
41
What happens if you attempt to lseek to a negative resulting absolute offset?
errno to EINVAL
42 The global variable used to identify the specific error code after a system call fails is:
std_err
sys_err
errno
error
43 Which function is often used to print a human-readable error message describing the last error encountered?
printf
perror
write
strcat
44
If a process calls fork(), what happens to open file descriptors?
45
What is the maximum number of bytes read can theoretically request?
ssize_t max value (typically SSIZE_MAX)
46
Which of these pairs represents the correct types for the arguments of lseek?
FILE *fp, long offset, int whence
int fd, off_t offset, int whence
int fd, int offset, char *whence
int fd, size_t offset, int whence
47
If fd is closed, and you call close(fd) again, what happens?
48
How does open distinguish between text and binary files in UNIX/Linux?
O_TEXT and O_BINARY flags
49
What is the effect of O_SYNC flag in open?
50
Which buffer pointer type is used in the read prototype ssize_t read(int fd, void *buf, size_t count)?
void *
int *
char *
struct file *