Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

198 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 51–60 of 198
Topics in C Programming
Q.51 Hard File Handling
Which scenario would be most problematic when using fprintf() for data that needs exact binary preservation?
A fprintf() interprets format specifiers and may alter binary data representation
B fprintf() is always safer than fwrite()
C fprintf() cannot handle NULL characters in data
D Both A and C are correct
Correct Answer:  D. Both A and C are correct
EXPLANATION

fprintf() is text-oriented and interprets format specifiers, potentially modifying data. It also stops at NULL characters. For binary data, fwrite() is appropriate.

Take Test
Q.52 Hard File Handling
A program processes a file with multiple reading and writing operations without closing/reopening. What is a potential issue with file buffering?
A Data written to buffer may not be physically written to disk until fflush() or fclose()
B File pointer position becomes unreliable
C Read operations always return cached data from memory
D File automatically truncates after buffer fills
Correct Answer:  A. Data written to buffer may not be physically written to disk until fflush() or fclose()
EXPLANATION

File I/O uses buffering for efficiency. Data in write buffer stays in memory until fflush(fp) explicitly flushes or fclose() is called, risking data loss if program crashes.

Take Test
Q.53 Hard File Handling
When writing binary structures with fwrite(), which precaution is essential for portability across different systems?
A Account for struct padding and alignment differences
B Always use text mode instead of binary
C Ensure file size matches sizeof(struct) * count
D Use network byte order for all integers
Correct Answer:  A. Account for struct padding and alignment differences
EXPLANATION

Struct padding varies by architecture and compiler. Binary files written on one system may not read correctly on another due to different alignment. Use serialization techniques for portability.

Take Test
Q.54 Hard File Handling
How can you determine the size of a file in bytes using standard C functions?
A Use fseek(fp, 0, SEEK_END) followed by ftell(fp)
B Use stat() function on the filename
C Use fstat() on the file descriptor
D All of the above methods work
Correct Answer:  D. All of the above methods work
EXPLANATION

Multiple methods exist: fseek/ftell repositions and returns position, stat() gets file info including size, and fstat() does similar with file descriptor. All are valid.

Take Test
Q.55 Hard File Handling
A large binary file of 10GB is being processed. Reading entire file into memory is impossible. Which approach is best?
A Use fread() to read file in chunks, process each chunk
B Use multiple fopen() calls on same file
C Split file manually into smaller files
D Use fscanf() which has built-in chunking
Correct Answer:  A. Use fread() to read file in chunks, process each chunk
EXPLANATION

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.

Take Test
Q.56 Hard File Handling
A program uses fgetc() to read 100,000 characters from a file sequentially. Which alternative would be more efficient?
A Use fgets() with larger buffer
B Use fread() with appropriate buffer size
C Use fscanf() with %c format
D Continue with fgetc(); efficiency doesn't matter
Correct Answer:  B. Use fread() with appropriate buffer size
EXPLANATION

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.

Take Test
Q.57 Hard File Handling
A program writes binary data using fwrite() but reads it back with fprintf(). What will happen?
A Data will be read correctly
B Binary data will be misinterpreted; text formatting won't apply
C Program will crash
D Only null bytes will be read
Correct Answer:  B. Binary data will be misinterpreted; text formatting won't apply
EXPLANATION

Mixing write (fwrite - binary) and read (fprintf - formatted text) functions on same data causes mismatch. Binary data won't have format specifiers; misinterpretation results.

Take Test
Q.58 Hard File Handling
What is the purpose of using fflush() in file handling, and when is it critical to use it?
A To move file pointer to the beginning of the file
B To write buffered data to the actual file on disk
C To close the file gracefully
D To clear error flags from the stream
Correct Answer:  B. To write buffered data to the actual file on disk
EXPLANATION

fflush() writes any buffered data to the underlying file. It's critical when you need to ensure data is written to disk before continuing, especially before reading the same file.

Take Test
Q.59 Hard File Handling
A program writes data using fprintf() and later attempts to read it back. However, the read operation fails intermittently. What could be the most likely cause?
A fprintf() is not suitable for output operations
B The buffer was not flushed before reading, causing incomplete data
C The file was opened in binary mode instead of text mode
D The file pointer was not reset after writing
Correct Answer:  B. The buffer was not flushed before reading, causing incomplete data
EXPLANATION

fprintf() buffers output. Without flushing (fflush() or fclose()), subsequent read operations may see incomplete data. This is a common source of bugs.

Take Test
Q.60 Hard File Handling
Consider using fseek() on a file opened in text mode with SEEK_END and a non-zero offset. What is the standard behavior?
A It is guaranteed to work correctly on all platforms
B It is undefined behavior and may not work correctly on all platforms
C It always returns an error
D It works only on Windows systems
Correct Answer:  B. It is undefined behavior and may not work correctly on all platforms
EXPLANATION

In text mode, fseek() with non-zero offset relative to SEEK_END is undefined behavior. Use fseek(fp, 0, SEEK_END) for reliable behavior.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips