Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Which header file is primarily required to use the open system call in a C program on a UNIX-like system?
Not quite. Try another option.
Answer: <fcntl.h>
The <fcntl.h> header file defines the file control options and flags (like O_RDONLY, O_CREAT) used by the open system call.
Question 2 of 50
What is the return type of the open() system call?
Not quite. Try another option.
Answer: int
open() returns a file descriptor, which is a small, non-negative integer used to reference the open file in subsequent system calls.
Question 3 of 50
If the open() system call fails, what value does it return?
Not quite. Try another option.
Answer:
On error, open() returns and sets the global variable errno to indicate the error.
Question 4 of 50
Which header file defines the read, write, close, and lseek system calls?
Not quite. Try another option.
Answer: <unistd.h>
The <unistd.h> header file provides access to the POSIX operating system API, including read, write, close, and lseek.
Question 5 of 50
What is the integer value of the file descriptor representing Standard Input (stdin)?
Not quite. Try another option.
Answer:
By convention, file descriptor is standard input, is standard output, and is standard error.
Question 6 of 50
Which flag is used with open() to open a file for reading only?
Not quite. Try another option.
Answer: O_RDONLY
O_RDONLY is the flag defined in <fcntl.h> to open a file in read-only mode.
Question 7 of 50
Which flag must be included in open() to create the file if it does not exist?
Not quite. Try another option.
Answer: O_CREAT
O_CREAT ensures that the file is created if it does not already exist. It usually requires a third argument to specify permissions.
Question 8 of 50
What does the read() system call return upon successfully reading data?
Not quite. Try another option.
Answer: The number of bytes read
On success, read() returns the number of bytes actually read. This may be less than the requested count.
Question 9 of 50
What is the return value of read() when the end of the file (EOF) is reached?
Not quite. Try another option.
Answer:
Only when read() returns does it indicate that the end of the file has been reached.
Question 10 of 50
Consider the syntax: ssize_t write(int fd, const void *buf, size_t count);. What does buf represent?
Not quite. Try another option.
Answer: A pointer to the data to be written
buf points to the memory location containing the data that the program intends to write to the file.
Question 11 of 50
Which open flag ensures that data is always written to the end of the file?
Not quite. Try another option.
Answer: O_APPEND
O_APPEND forces the file offset to the end of the file before each write, ensuring data is appended.
Question 12 of 50
What is the primary purpose of the lseek system call?
Not quite. Try another option.
Answer: To change the read/write file offset
lseek explicitly repositions the file offset of the open file description associated with the file descriptor.
Question 13 of 50
In the call lseek(fd, offset, whence), what does SEEK_SET indicate for the whence parameter?
Not quite. Try another option.
Answer: The offset is set to the beginning of the file
SEEK_SET sets the file offset to the value of the offset argument (absolute position from the start).
Question 14 of 50
Which lseek mode allows setting the offset relative to the current file position?
Not quite. Try another option.
Answer: SEEK_CUR
SEEK_CUR sets the offset to its current location plus the bytes specified in the offset argument.
Question 15 of 50
What is the return type of lseek()?
Not quite. Try another option.
Answer: off_t
lseek returns a value of type off_t, which represents the resulting offset location in bytes from the beginning of the file.
Question 16 of 50
How can you use lseek to find the total size of a file?
Not quite. Try another option.
Answer: lseek(fd, 0, SEEK_END)
Moving the offset 0 bytes from the end of the file returns the offset of the end, which equates to the total file size.
Question 17 of 50
What happens if lseek is called on a file descriptor referring to a pipe or socket?
Not quite. Try another option.
Answer: It returns and sets errno to ESPIPE
Pipes, FIFOs, and sockets are not seekable. lseek fails with the error ESPIPE.
Question 18 of 50
What is the correct prototype for the close system call?
Not quite. Try another option.
Answer: int close(int fd);
close takes a file descriptor integer and returns an integer (0 on success, -1 on error).
Question 19 of 50
Why is it important to use close() on file descriptors when they are no longer needed?
Not quite. Try another option.
Answer: To release the file descriptor resource back to the system
Process file descriptors are a limited resource. Failing to close them leads to resource leaks (file descriptor exhaustion).
Question 20 of 50
When open() is called with O_TRUNC and the file already exists, what happens?
Not quite. Try another option.
Answer: The file length is truncated to
O_TRUNC causes the file to be truncated to length 0 if it exists and allows writing, effectively clearing its contents.
Question 21 of 50
The third argument of open(), typically called mode, is mandatory only when:
Not quite. Try another option.
Answer: O_CREAT is specified in flags
The mode argument specifies the permissions to use if a new file is created. It is required when O_CREAT is used.
Question 22 of 50
Which system call corresponds to the Standard C Library function fprintf?
Not quite. Try another option.
Answer: write
fprintf is a library wrapper that eventually calls the write system call to output data to a file descriptor.
Question 23 of 50
What is the value of fd if fd = open("file.txt", O_RDONLY) is the first file opened by a process (excluding standard streams)?
Not quite. Try another option.
Answer:
open always returns the lowest numbered unused file descriptor. Since 0, 1, and 2 are standard streams, the next available is 3.
Question 24 of 50
Which data type is ssize_t (returned by read and write)?
Not quite. Try another option.
Answer: Signed integer
ssize_t is a signed integer type capable of storing the byte count or for errors.
Question 25 of 50
What happens if you invoke write(fd, buf, count) where fd was opened with O_RDONLY?
Not quite. Try another option.
Answer: It returns and sets errno to EBADF
EBADF stands for Bad File Descriptor. Writing to a read-only descriptor is an invalid operation.
Question 26 of 50
Using lseek, how would you create a "hole" in a file?
Not quite. Try another option.
Answer: By seeking past the end of the file and then writing data
If lseek moves past EOF and a write occurs, the gap between the old EOF and new write becomes a hole, reading as zeros.
Question 27 of 50
In the argument list for write(int fd, const void *buf, size_t count), what does count signify?
Not quite. Try another option.
Answer: The number of bytes requested to be written
count specifies how many bytes from the buffer should be written to the file descriptor.
Question 28 of 50
Which header contains the permission bit macros like S_IRUSR, S_IWUSR used in open()?
Not quite. Try another option.
Answer: <sys/stat.h>
Macros defining file modes/permissions (e.g., User Read, User Write) are defined in <sys/stat.h>.
Question 29 of 50
What does lseek(fd, -10, SEEK_CUR) do?
Not quite. Try another option.
Answer: Moves the offset backward by 10 bytes
Negative offsets with SEEK_CUR move the file pointer backwards towards the beginning of the file.
Question 30 of 50
If open is used with O_EXCL and O_CREAT, what happens if the file already exists?
Not quite. Try another option.
Answer: The call fails (returns )
O_EXCL combined with O_CREAT ensures the call creates the file. If it exists, the call fails (atomic check-and-create).
Question 31 of 50
What is the file descriptor for Standard Output (stdout)?
Not quite. Try another option.
Answer:
FD is reserved for standard output.
Question 32 of 50
Which system call is used to duplicate a file descriptor?
Not quite. Try another option.
Answer: dup()
dup() (and dup2()) creates a copy of a file descriptor, pointing to the same file table entry.
Question 33 of 50
What happens to the file offset after a successful read() operation?
Not quite. Try another option.
Answer: It increments by the number of bytes read
The system automatically advances the file offset by the number of bytes processed so the next read continues where the last left off.
Question 34 of 50
Which of the following is NOT a valid mode for open()?
Not quite. Try another option.
Answer: O_EXECUTE
O_EXECUTE is not a standard POSIX flag for open(). Execution permissions are handled via faccessat or file mode bits, not open flags (though O_PATH or specific non-standard flags might exist, O_RDWR etc are the standard access modes).
Question 35 of 50
What is the return value of close(fd) if fd is not a valid open file descriptor?
Not quite. Try another option.
Answer:
close returns and sets errno to EBADF if the file descriptor is invalid.
Question 36 of 50
Which type matches the count argument in read and write?
Not quite. Try another option.
Answer: size_t
count represents a magnitude (size) and cannot be negative, so it uses size_t (unsigned).
Question 37 of 50
Which macro sets the file permission to rw-r--r-- (User read/write, Group read, Others read) in octal?
Not quite. Try another option.
Answer:
User (6 = 4+2 read/write), Group (4 = read), Others (4 = read). .
Question 38 of 50
If read(fd, buf, 100) returns 50, what does this imply?
Not quite. Try another option.
Answer: Only 50 bytes were available or read from the file/stream
read may return fewer bytes than requested if EOF is near or if reading from a pipe/terminal with limited data.
Question 39 of 50
What is the equivalent of lseek(fd, 0, SEEK_SET) using standard C library functions?
Not quite. Try another option.
Answer: rewind(fp)
rewind(fp) sets the file position to the beginning, equivalent to seeking to offset 0 from SEEK_SET.
Question 40 of 50
Can open() typically open a directory for reading?
Not quite. Try another option.
Answer: Yes, but generally only to read directory entries (system dependent)
Directories can be opened with open (often requires O_DIRECTORY), but read usually fails; getdents or readdir is used instead. Writing is forbidden.
Question 41 of 50
What happens if you attempt to lseek to a negative resulting absolute offset?
Not quite. Try another option.
Answer: It returns and sets errno to EINVAL
A file offset cannot be negative. lseek validates the resulting position.
Question 42 of 50
The global variable used to identify the specific error code after a system call fails is:
Not quite. Try another option.
Answer: errno
errno is the integer variable set by system calls and some library functions in the event of an error.
Question 43 of 50
Which function is often used to print a human-readable error message describing the last error encountered?
Not quite. Try another option.
Answer: perror
perror(s) prints the string s, a colon, and the string representation of the current errno value.
Question 44 of 50
If a process calls fork(), what happens to open file descriptors?
Not quite. Try another option.
Answer: They are inherited by the child
The child process inherits copies of the parent's file descriptors, sharing the same file table entries (and thus offsets).
Question 45 of 50
What is the maximum number of bytes read can theoretically request?
Not quite. Try another option.
Answer: Depends on ssize_t max value (typically SSIZE_MAX)
While size_t is unsigned, the return type is signed ssize_t, limiting the practical max chunk to SSIZE_MAX.
Question 46 of 50
Which of these pairs represents the correct types for the arguments of lseek?
Not quite. Try another option.
Answer: int fd, off_t offset, int whence
The offset argument is of type off_t (to support large files) and whence is an int.
Question 47 of 50
If fd is closed, and you call close(fd) again, what happens?
Not quite. Try another option.
Answer: Returns (EBADF)
A double close on the same file descriptor integer (if not reassigned) returns an error because the descriptor is no longer valid.
Question 48 of 50
How does open distinguish between text and binary files in UNIX/Linux?
Not quite. Try another option.
Answer: It does not distinguish; all files are byte streams
Unlike Windows, UNIX-like systems treat all files as sequences of bytes. No translation occurs.
Question 49 of 50
What is the effect of O_SYNC flag in open?
Not quite. Try another option.
Answer: Writes block until data is physically stored on the disk
O_SYNC ensures file integrity by flushing data to the hardware before the write call returns.
Question 50 of 50
Which buffer pointer type is used in the read prototype ssize_t read(int fd, void *buf, size_t count)?
Not quite. Try another option.
Answer: void *
void * is a generic pointer type, allowing read to fill any type of data structure passed to it.
That's unit 3
Answer a few questions and your score shows up here.
Nearly through. Did unit 3 of CSE325 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 3 of CSE325 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.