Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

499 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 121–130 of 499
Topics in C Programming
Q.121 Medium File Handling
What is the difference between 'rb' and 'r' modes when opening a file?
A 'rb' opens in binary mode, 'r' opens in text mode with newline conversion
B 'rb' allows reading, 'r' allows both reading and writing
C Both are identical in modern C compilers
D 'r' is faster than 'rb' for large files
Correct Answer:  A. 'rb' opens in binary mode, 'r' opens in text mode with newline conversion
EXPLANATION

'rb' (binary read) reads data as-is without converting line endings. 'r' (text read) converts platform-specific newlines (\r\n on Windows) to \n.

Take Test
Q.122 Medium File Handling
A program needs to update specific bytes in the middle of a file without affecting other data. Which approach is most suitable?
A Use fseek() to position pointer, then fwrite() the new data
B Read entire file, modify in memory, write back
C Use file append mode to add changes
D Create new file with corrected data
Correct Answer:  A. Use fseek() to position pointer, then fwrite() the new data
EXPLANATION

Using fseek() with r+ or w+ mode allows direct access to specific file positions. This is efficient for small modifications without rewriting the entire file.

Take Test
Q.123 Medium File Handling
Which of the following correctly reads a string from file with size limit?
A fgets(buffer, size, fp)
B fscanf(fp, "%s", buffer)
C fgetc(fp)
D fread(buffer, 1, size, fp)
Correct Answer:  A. fgets(buffer, size, fp)
EXPLANATION

fgets(buffer, size, fp) safely reads up to (size-1) characters or until newline from file fp, preventing buffer overflow. fscanf() is vulnerable to overflow.

Take Test
Q.124 Medium File Handling
What does the function ftell(fp) return in file handling?
A Current position of file pointer from the beginning
B Total size of the file in bytes
C Number of bytes read so far
D End-of-file status
Correct Answer:  A. Current position of file pointer from the beginning
EXPLANATION

ftell(fp) returns the current position of the file pointer (in bytes) from the beginning of the file. Returns -1L on error.

Take Test
Q.125 Medium File Handling
Which mode allows both reading and appending without truncating existing content?
A a+
B w+
C r+
D ab+
Correct Answer:  A. a+
EXPLANATION

'a+' mode opens file for reading and appending. New data is added at the end without removing existing content. 'w+' would truncate the file.

Take Test
Q.126 Medium File Handling
In a file opened in "a+" mode, where does the file pointer initially point?
A Beginning of file
B End of file
C Middle of file
D Undefined location
Correct Answer:  B. End of file
EXPLANATION

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.

Take Test
Q.127 Medium File Handling
What does fflush(fp) do when fp is a file pointer?
A Clears the file content
B Writes buffered data to disk without closing file
C Moves file pointer to beginning
D Reads pending data from disk into buffer
Correct Answer:  B. Writes buffered data to disk without closing file
EXPLANATION

fflush(fp) forces buffered output to be written to disk immediately without closing the file. Useful for ensuring data safety before risky operations.

Take Test
Q.128 Medium File Handling
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?
A Use larger buffer size for each fgets() call
B Use fread() with binary mode instead
C Multiple fgets() calls are needed; accumulate partial reads
D Use fscanf() which handles large lines automatically
Correct Answer:  A. Use larger buffer size for each fgets() call
EXPLANATION

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.

Take Test
Q.129 Medium File Handling
Which of the following modes will truncate an existing file?
A "r+"
B "a"
C "w"
D "a+"
Correct Answer:  C. "w"
EXPLANATION

"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.

Take Test
Q.130 Medium File Handling
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?
A fseek(fp, 10, SEEK_SET)
B fseek(fp, 320, SEEK_SET)
C fseek(fp, 11*32, SEEK_SET)
D fseek(fp, 9*32, SEEK_SET)
Correct Answer:  B. fseek(fp, 320, SEEK_SET)
EXPLANATION

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).

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