A file is opened in binary mode. How will fseek() behave differently compared to text mode?
Answer: C
In text mode, fseek() behavior is implementation-dependent due to line-ending conversions (\r\n vs \n). Binary mode provides more predictable fseek() behavior with absolute byte positions.
Q.42Easy
What will fseek(fp, 0, SEEK_END) do?
Answer: B
SEEK_END moves pointer relative to end of file. Offset 0 means go to the exact end. SEEK_SET (beginning) and SEEK_CUR (current) are other origin options.
Q.43Medium
A program uses fprintf() to write 1000 integers to a file but doesn't call fclose(). What could happen?
Answer: B
Without fclose() or fflush(), buffered data remains in memory. If program terminates, that data never reaches disk. System may flush on exit but it's unreliable.
Q.44Easy
Which function reads formatted data from a file?
Answer: B
fscanf() reads formatted data from file (like scanf() but from file). fgets() reads strings. fread() reads binary data. getc() reads single character.
Q.45Easy
What is returned by fopen() if file opening fails?
Answer: B
fopen() returns NULL pointer on failure. Proper practice is to check: if(fp == NULL) {...error handling...}. Returning -1 is for system calls like open().
Advertisement
Q.46Medium
A binary file contains 500 structures of size 32 bytes each. To skip the first 10 structures and read the 11th one, which fseek() call is correct?
Answer: B
To skip first 10 structures and read 11th: 10 * 32 = 320 bytes from start. Option B (320 bytes) positions pointer at start of 11th structure (320 bytes offset).
Q.47Medium
Which of the following modes will truncate an existing file?
Answer: C
"w" mode opens file for writing and truncates it to zero length if it exists. "r+" doesn't truncate. "a" and "a+" are append modes that don't truncate.
Q.48Medium
A program reads a CSV file with fgets() storing data in a 256-byte buffer. Some lines exceed 255 characters. How should this be handled?
Answer: A
Best solution is allocate larger buffer. If line is 500 chars, use buffer >= 501. Option C partially works but complicates newline handling. fscanf() doesn't automatically handle long lines.
Q.49Medium
What does fflush(fp) do when fp is a file pointer?
Answer: B
fflush(fp) forces buffered output to be written to disk immediately without closing the file. Useful for ensuring data safety before risky operations.
Q.50Hard
A program writes binary data using fwrite() but reads it back with fprintf(). What will happen?
Answer: B
Mixing write (fwrite - binary) and read (fprintf - formatted text) functions on same data causes mismatch. Binary data won't have format specifiers; misinterpretation results.
Q.51Medium
In a file opened in "a+" mode, where does the file pointer initially point?
Answer: B
In append mode "a" or "a+", file pointer initially points to end of file for writing. For reading in "a+", you must explicitly seek to read from start.
Q.52Hard
A program uses fgetc() to read 100,000 characters from a file sequentially. Which alternative would be more efficient?
Answer: B
fread() with buffer reads multiple bytes per call, reducing function call overhead. fgetc() makes 100,000 separate calls. fgets() is limited to reading up to newline.
Q.53Easy
What does the third parameter of fseek() represent?
Answer: B
Third parameter is 'whence': SEEK_SET (beginning), SEEK_CUR (current position), SEEK_END (end). Second parameter specifies offset in bytes.
Q.54Hard
A large binary file of 10GB is being processed. Reading entire file into memory is impossible. Which approach is best?
Answer: A
fread(buffer, size, count, fp) efficiently reads file in manageable chunks. Process each chunk, then read next. This is standard practice for large file handling.
Q.55Easy
Which header file must be included to use file handling functions in C?
Answer: A
stdio.h (Standard Input/Output) contains all file handling functions like fopen(), fclose(), fread(), fwrite(), etc.
Q.56Easy
What does the 'w+' mode do when opening a file with fopen()?
Answer: A
'w+' mode opens a file for both reading and writing. If the file doesn't exist, it creates a new one. If it exists, it truncates the file to zero length.
Q.57Easy
Which function is used to write a single character to a file?
Answer: A
fputc() writes a single character to the specified file. fgetc() reads a character, fwrite() writes blocks, and fprintf() writes formatted output.
Q.58Easy
What is the return value of feof(fp) when the end of file is reached?
Answer: A
feof(fp) returns a non-zero value when EOF (End Of File) is reached, and returns zero when not at EOF.
Q.59Easy
In C file handling, what does rewind(fp) do?
Answer: A
rewind(fp) repositions the file pointer to the start of the file. It's equivalent to fseek(fp, 0, SEEK_SET).
Q.60Medium
Which mode allows both reading and appending without truncating existing content?
Answer: A
'a+' mode opens file for reading and appending. New data is added at the end without removing existing content. 'w+' would truncate the file.