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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A. $0$
B.
C. $1$
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. A pointer to the data to be written
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_APPEND
B. O_END
C. O_TRUNC
D. O_LAST

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

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

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

15 What is the return type of lseek()?

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

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

A. lseek(fd, 0, SEEK_END)
B. lseek(fd, 0, SEEK_CUR)
C. lseek(fd, 0, SEEK_SET)
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 returns $0$
B. It closes the pipe
C. It works normally
D. It returns and sets errno to ESPIPE

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

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

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 file length is truncated to $0$
B. The file is deleted and a new one is created
C. The file is opened in append mode
D. The function returns an error

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

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

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

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

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. $1$
D. $0$

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

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

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

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

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

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

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. Returns an error immediately
B. Moves the offset forward by 10 bytes
C. Moves the offset backward by 10 bytes
D. Moves to the 10th byte from the end

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. The call fails (returns )
C. It overwrites the existing file
D. It appends to the existing file

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

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

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

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

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_WRONLY
B. O_RDONLY
C. O_RDWR
D. O_EXECUTE

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. ssize_t
C. int
D. size_t

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

A. $0600$
B. $0666$
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. ftell(fp)
B. rewind(fp)
C. fseek(fp, 0, SEEK_END)
D. fsetpos(fp, NULL)

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 sets the offset to $0$
B. It returns and sets errno to EINVAL
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. sys_err
B. error
C. errno
D. std_err

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

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

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. Unlimited
B. Depends on ssize_t max value (typically SSIZE_MAX)
C. 255 bytes
D. 4096 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. int fd, size_t offset, int whence
D. FILE *fp, long offset, int whence

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

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

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

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

49 What is the effect of O_SYNC flag in open?

A. Writes block until data is physically stored on the disk
B. Allows multiple processes to write simultaneously
C. Opens file in synchronous non-blocking mode
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. struct file *
B. char *
C. int *
D. void *