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 101–110 of 499
Topics in C Programming
Q.101 Medium Dynamic Memory
What is a memory leak in C?
A Accessing freed memory
B Allocated memory that is never freed
C Memory overflow
D Stack overflow
Correct Answer:  B. Allocated memory that is never freed
EXPLANATION

A memory leak occurs when allocated memory is not freed, consuming system resources without releasing them back.

Take Test
Q.102 Medium Dynamic Memory
What will be the output of the following code?
int *ptr = malloc(sizeof(int));
if(ptr == NULL) printf("Failed");
else printf("Success");
A Failed
B Success
C Compilation Error
D Runtime Error
Correct Answer:  B. Success
EXPLANATION

In typical scenarios, malloc() successfully allocates memory and returns non-NULL pointer, printing 'Success'. NULL check is good practice for error handling.

Take Test
Q.103 Medium File Handling
What is the critical issue if ferror(fp) returns non-zero during file operations in a banking application?
A An error occurred during file I/O operation that should be handled
B The file pointer reached the end of file
C The file mode is incorrect
D Buffer overflow is detected
Correct Answer:  A. An error occurred during file I/O operation that should be handled
EXPLANATION

ferror() returns non-zero if an error has occurred in file operations. In critical applications like banking, this must be checked to ensure data integrity. This is different from EOF (checked by feof()).

Take Test
Q.104 Medium File Handling
A competitive exam question requires processing a 500MB CSV file line by line. Which approach is most efficient in terms of memory?
A Read entire file into memory using fread(), then process
B Use fgets() with a fixed-size buffer for each line
C Use mmap() for memory-mapped file access
D Use getc() to read one character at a time
Correct Answer:  B. Use fgets() with a fixed-size buffer for each line
EXPLANATION

fgets() with a fixed buffer reads one line at a time, maintaining constant memory usage regardless of file size. Option A wastes memory, option D is slowest, and option C may cause issues with large files on some systems.

Take Test
Q.105 Medium File Handling
In a data structure serialization scenario, what is returned by fwrite() on successful completion?
A The number of complete items successfully written
B The total number of bytes written
C Zero on success
D The position of file pointer after writing
Correct Answer:  A. The number of complete items successfully written
EXPLANATION

fwrite(ptr, size, nmemb, fp) returns the number of complete items written, not bytes. If writing 10 items of 4 bytes each but only 35 bytes written, it returns 8, not 35.

Take Test
Q.106 Medium File Handling
What will be the output of fseek(fp, -10, SEEK_END) in a file of 100 bytes?
A File pointer moves to byte 90
B File pointer moves to byte 10
C Returns error because negative offset is invalid
D File pointer remains unchanged
Correct Answer:  A. File pointer moves to byte 90
EXPLANATION

fseek(fp, -10, SEEK_END) positions the pointer 10 bytes before the end of file. For a 100-byte file, this places it at position 90 (0-indexed). Negative offsets with SEEK_END are valid.

Take Test
Q.107 Medium File Handling
A program needs to read and write to the same file alternately. Which file mode is most appropriate?
A 'r+'
B 'w+'
C 'a+'
D 'rb+'
Correct Answer:  A. 'r+'
EXPLANATION

'r+' mode allows both reading and writing without truncating the file. 'w+' truncates the file on opening, 'a+' is for append operations. 'r+' preserves existing content while allowing modifications.

Take Test
Q.108 Medium File Handling
For a file with 1000 records, which approach minimizes I/O operations?
A Read one record at a time with fread()
B Read all records with single fread() call
C Use fgets() in loop for each record
D Both A and C are equivalent
Correct Answer:  B. Read all records with single fread() call
EXPLANATION

Single fread() for all records = 1 I/O operation. Loop-based approaches = 1000 operations. Fewer I/O calls = better performance.

Take Test
Q.109 Medium File Handling
A program crashes after reading a file. What's most likely the cause if error checking is minimal?
A File doesn't exist but fopen() wasn't checked
B Buffer overflow during fread()
C Insufficient disk space
D File permissions are read-only
Correct Answer:  A. File doesn't exist but fopen() wasn't checked
EXPLANATION

Not checking fopen() return value leads to NULL pointer dereference. Buffer overflow and permissions cause different errors. Disk space affects writing.

Take Test
Q.110 Medium File Handling
Which approach correctly implements a file copy function for both text and binary files?
A Always use 'rb'/'wb' modes regardless of file type
B Detect file type and open accordingly
C Use 'r'/'w' for text, then reopen with 'rb'/'wb' for binary
D Binary copy is sufficient for all file types
Correct Answer:  D. Binary copy is sufficient for all file types
EXPLANATION

Binary mode works for all files because it's just byte-for-byte copy. Text mode conversions are unnecessary for copying.

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