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 141–150 of 499
Topics in C Programming
Q.141 Medium File Handling
Which mode should be used if you want to read and write to a file without truncating existing content?
A "w+"
B "r+"
C "a+"
D "rb+"
Correct Answer:  B. "r+"
EXPLANATION

The 'r+' mode opens a file for both reading and writing without truncating it. The file must exist.

Take Test
Q.142 Medium File Handling
In the context of ferror() function, what does it return?
A Non-zero if an error has occurred, zero otherwise
B The specific error code from the system
C The line number where error occurred
D Always returns 0 for binary files
Correct Answer:  A. Non-zero if an error has occurred, zero otherwise
EXPLANATION

ferror() returns a non-zero value if an error flag is set for the stream, otherwise returns 0.

Take Test
Q.143 Medium File Handling
Which function is used to read a string from a file, including whitespace characters, up to a maximum length?
A fscanf()
B fgets()
C getc()
D fread()
Correct Answer:  B. fgets()
EXPLANATION

fgets() reads a string from file including whitespace until newline or EOF is encountered or max length is reached.

Take Test
Q.144 Medium File Handling
What happens when fseek() is used with SEEK_END as origin?
A File pointer moves to end of file
B File pointer stays at current position
C File pointer moves to beginning
D File is closed
Correct Answer:  A. File pointer moves to end of file
EXPLANATION

SEEK_END moves the file pointer to the end of the file. Offset can be 0 or negative to move before the end. SEEK_SET starts from beginning, SEEK_CUR from current position.

Take Test
Q.145 Medium File Handling
Consider the code: FILE *fp = fopen("data.txt", "r"); if(fp == NULL) { printf("Error"); } What does this check?
A Whether file opened successfully
B Whether file is empty
C Whether file pointer is valid
D Whether file has read permission
Correct Answer:  A. Whether file opened successfully
EXPLANATION

This code checks if fopen() returned NULL, which indicates the file failed to open. This is the standard error checking method for file operations.

Take Test
Q.146 Medium File Handling
What is the purpose of rewind() function in file handling?
A Moves file pointer to the beginning
B Closes the file
C Reverses file content
D Undoes last write operation
Correct Answer:  A. Moves file pointer to the beginning
EXPLANATION

rewind() moves the file pointer to the beginning of the file and clears the EOF indicator. It's equivalent to fseek(file, 0, SEEK_SET).

Take Test
Q.147 Medium File Handling
What does fwrite() function return?
A Number of items successfully written
B Number of bytes written
C 1 on success, 0 on failure
D Always returns 0
Correct Answer:  A. Number of items successfully written
EXPLANATION

fwrite() returns the number of items successfully written to the file, which may be less than the count requested if an error occurs.

Take Test
Q.148 Medium File Handling
Which mode allows both reading and writing without truncating?
A "r+"
B "w+"
C "a+"
D "rb+"
Correct Answer:  A. "r+"
EXPLANATION

"r+" mode opens a file for both reading and writing without truncating it. The file must exist. "w+" creates a new file or truncates existing one.

Take Test
Q.149 Medium File Handling
What is the difference between fgetc() and getc()?
A fgetc() is a function while getc() is a macro
B getc() is faster than fgetc()
C fgetc() can only read one character
D getc() requires file pointer argument
Correct Answer:  A. fgetc() is a function while getc() is a macro
EXPLANATION

fgetc() is a function that reads a character from a file, while getc() is a macro. Both serve the same purpose but getc() may be faster due to macro expansion.

Take Test
Q.150 Medium File Handling
What does feof() function check?
A Whether end of file is reached
B Whether file is empty
C Whether file exists
D Whether file is open
Correct Answer:  A. Whether end of file is reached
EXPLANATION

feof() returns true (non-zero) if the end-of-file indicator is set for the given file stream, otherwise it returns 0.

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