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 111–120 of 499
Topics in C Programming
Q.111 Medium File Handling
A file opened with 'r+b' mode has pointer at position 100. After fwrite(buffer, 1, 50, fp), the pointer is at:
A 100
B 150
C 50
D Undefined position
Correct Answer:  B. 150
EXPLANATION

After fwrite() of 50 bytes from position 100, pointer moves to 150. File pointer advances after read/write operations.

Take Test
Q.112 Medium File Handling
In a program processing a CSV file, what is the most robust approach for reading variable-length lines?
A Use fixed-size buffer with fgets()
B Use getc() in loop and dynamically allocate memory
C Use fscanf() with %s format
D Read entire file into memory at once
Correct Answer:  B. Use getc() in loop and dynamically allocate memory
EXPLANATION

Dynamic allocation with getc() handles variable-length lines robustly. fgets() with fixed buffer may truncate. fscanf() with %s doesn't handle spaces. Option D wastes memory.

Take Test
Q.113 Medium File Handling
Which statement correctly counts non-comment lines in a text file?
A Read each line and skip if first non-whitespace char is '//'
B Use grep to filter comments before processing
C Read line with fgets(), check for '//' pattern, increment counter if not found
D Use fscanf() with format specifier to skip comment lines
Correct Answer:  C. Read line with fgets(), check for '//' pattern, increment counter if not found
EXPLANATION

Pure C solution uses fgets() to read lines and strstr() or manual checking for comment patterns. Option B uses external tool. Option D is inefficient.

Take Test
Q.114 Medium File Handling
A program reads struct records using fread(). After reading 100 records of 64 bytes each, what is the total bytes read if successful?
A 100
B 64
C 6400
D fread() doesn't return bytes, only count
Correct Answer:  C. 6400
EXPLANATION

fread(ptr, 64, 100, fp) reads 100 items of 64 bytes each = 6400 bytes total. fread() returns count of items read, not bytes.

Take Test
Q.115 Medium File Handling
What happens if you call fclose() on an already closed file pointer?
A Returns 0
B Returns EOF and may cause undefined behavior
C Silently succeeds
D Raises compile-time error
Correct Answer:  B. Returns EOF and may cause undefined behavior
EXPLANATION

Calling fclose() on closed file causes undefined behavior and returns EOF. This is a runtime issue, not compile-time.

Take Test
Q.116 Medium File Handling
A program needs to append 500 integers to an existing binary file. Which combination is correct?
A fopen(file, 'ab') with fwrite()
B fopen(file, 'a') with fprintf()
C fopen(file, 'ab') with fprintf()
D fopen(file, 'wb') with fwrite()
Correct Answer:  A. fopen(file, 'ab') with fwrite()
EXPLANATION

Binary append requires 'ab' mode with fwrite(). Option B uses text mode. Option D would overwrite. Option C mixes binary mode with text function.

Take Test
Q.117 Medium File Handling
A C program must securely close a file and handle potential errors. What is the best approach?
A if(fclose(fp) != 0) { handle_error(); }
B fclose(fp); assuming it always succeeds
C Just let the OS close file when program exits
D Use multiple fclose() calls for safety
Correct Answer:  A. if(fclose(fp) != 0) { handle_error(); }
EXPLANATION

fclose() returns 0 on success and EOF (-1) on error. Checking return value ensures buffered data is flushed and handles errors like write failures.

Take Test
Q.118 Medium File Handling
In competitive exam file handling questions, if fseek() is called with SEEK_CUR, what does it indicate?
A Seek relative to current file pointer position
B Seek from the beginning of file
C Seek from the end of file
D Seek to current directory location
Correct Answer:  A. Seek relative to current file pointer position
EXPLANATION

SEEK_CUR is a constant meaning 'current position'. fseek(fp, offset, SEEK_CUR) moves the pointer by 'offset' bytes from its current position.

Take Test
Q.119 Medium File Handling
A program reads structured data: fread(ptr, sizeof(struct), count, fp). If fread() returns a value less than 'count', what could be the reason?
A End of file reached before reading all items
B File pointer is NULL
C sizeof(struct) is too large
D The file was opened in text mode instead of binary
Correct Answer:  A. End of file reached before reading all items
EXPLANATION

fread() returns the number of items actually read. If less than 'count' is returned, it indicates EOF was reached or a read error occurred before completing the request.

Take Test
Q.120 Medium File Handling
In a file opened with 'rb+' mode, what operations can be performed?
A Reading and writing binary data without truncating
B Only reading binary data
C Only writing binary data
D Reading, writing, and truncating
Correct Answer:  A. Reading and writing binary data without truncating
EXPLANATION

'rb+' opens a binary file for both reading and writing. The file must exist. Existing content is preserved and can be overwritten at any position.

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