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 131–140 of 499
Topics in C Programming
Q.131 Medium File Handling
A program uses fprintf() to write 1000 integers to a file but doesn't call fclose(). What could happen?
A All data is automatically saved to disk
B Data in buffer may be lost if program terminates
C File becomes corrupted
D The program will hang indefinitely
Correct Answer:  B. Data in buffer may be lost if program terminates
EXPLANATION

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.

Take Test
Q.132 Medium File Handling
A file is opened in binary mode. How will fseek() behave differently compared to text mode?
A No difference in behavior
B In binary mode, fseek() always fails
C In text mode, fseek() may not work reliably due to character encoding differences
D In binary mode, negative offsets are not allowed
Correct Answer:  C. In text mode, fseek() may not work reliably due to character encoding differences
EXPLANATION

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.

Take Test
Q.133 Medium File Handling
A program reads a text file line by line using fgets(). If a line contains more characters than the buffer size, what happens?
A fgets() reads only up to buffer size - 1 characters and adds null terminator
B fgets() reads entire line regardless of buffer size
C An error occurs and program crashes
D fgets() skips that line entirely
Correct Answer:  A. fgets() reads only up to buffer size - 1 characters and adds null terminator
EXPLANATION

fgets(buffer, size, fp) reads at most size-1 characters or until newline, whichever comes first, then adds null terminator. Remaining characters stay in stream for next read.

Take Test
Q.134 Medium File Handling
What will be the behavior of fgets() when reading from an empty line in a file?
A It will return NULL
B It will read the newline character into the buffer
C It will skip the empty line and read the next non-empty line
D It will cause a segmentation fault
Correct Answer:  B. It will read the newline character into the buffer
EXPLANATION

fgets() reads up to and including the newline character. For an empty line, it reads just the newline character into the buffer.

Take Test
Q.135 Medium File Handling
Which function is used to read formatted data from a file, similar to scanf() but for files?
A fread()
B fgets()
C fscanf()
D fgetc()
Correct Answer:  C. fscanf()
EXPLANATION

fscanf() reads formatted data from a file stream, functioning as the file equivalent of scanf().

Take Test
Q.136 Medium File Handling
What is the difference between text mode and binary mode when reading files?
A Binary mode is faster than text mode
B Text mode performs line-ending translation, binary mode does not
C Binary mode can only handle numeric data
D Text mode is only for ASCII files, binary for Unicode
Correct Answer:  B. Text mode performs line-ending translation, binary mode does not
EXPLANATION

In text mode, platform-specific line endings (\r\n on Windows, \n on Unix) are translated. Binary mode reads bytes as-is without translation.

Take Test
Q.137 Medium File Handling
Which of the following fseek() calls will position the file pointer at the end of the file?
A fseek(fp, 0, SEEK_SET)
B fseek(fp, -1, SEEK_END)
C fseek(fp, 0, SEEK_END)
D fseek(fp, 1, SEEK_CUR)
Correct Answer:  C. fseek(fp, 0, SEEK_END)
EXPLANATION

fseek(fp, 0, SEEK_END) positions the file pointer at the end of file. SEEK_END is the third constant in <stdio.h>.

Take Test
Q.138 Medium File Handling
What does ftell() function return?
A The total size of the file in bytes
B The current position of the file pointer
C The number of bytes read so far
D The line number of current file pointer position
Correct Answer:  B. The current position of the file pointer
EXPLANATION

ftell() returns the current position of the file pointer as a long integer offset from the beginning of the file.

Take Test
Q.139 Medium File Handling
Consider a scenario where fopen() fails to open a file. Which of the following is the correct way to handle it?
A FILE *fp = fopen("file.txt", "r"); if(fp == 0) { /* handle error */ }
B FILE *fp = fopen("file.txt", "r"); if(fp) { /* process file */ }
C FILE *fp = fopen("file.txt", "r"); if(fp == NULL) { /* handle error */ }
D FILE *fp = fopen("file.txt", "r"); if(!fp) { perror("Error"); }
Correct Answer:  C. FILE *fp = fopen("file.txt", "r"); if(fp == NULL) { /* handle error */ }
EXPLANATION

fopen() returns NULL pointer on failure. Checking 'fp == NULL' or '!fp' is the correct approach. Option C uses explicit NULL check which is clearer.

Take Test
Q.140 Medium File Handling
What is the purpose of the clearerr() function in file handling?
A To clear the EOF indicator and error flags for a stream
B To delete the file associated with the stream
C To flush the buffer to disk
D To clear only the error flag but not EOF
Correct Answer:  A. To clear the EOF indicator and error flags for a stream
EXPLANATION

clearerr() clears both the EOF indicator and the error flag associated with a stream.

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