A file opened with 'r+b' mode has pointer at position 100. After fwrite(buffer, 1, 50, fp), the pointer is at:
Answer: B
After fwrite() of 50 bytes from position 100, pointer moves to 150. File pointer advances after read/write operations.
Q.382Medium
Which approach correctly implements a file copy function for both text and binary files?
Answer: D
Binary mode works for all files because it's just byte-for-byte copy. Text mode conversions are unnecessary for copying.
Q.383Medium
A program crashes after reading a file. What's most likely the cause if error checking is minimal?
Answer: A
Not checking fopen() return value leads to NULL pointer dereference. Buffer overflow and permissions cause different errors. Disk space affects writing.
Q.384Medium
For a file with 1000 records, which approach minimizes I/O operations?
Answer: B
Single fread() for all records = 1 I/O operation. Loop-based approaches = 1000 operations. Fewer I/O calls = better performance.
Q.385Medium
A program needs to read and write to the same file alternately. Which file mode is most appropriate?
Answer: A
'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.
Advertisement
Q.386Medium
What will be the output of fseek(fp, -10, SEEK_END) in a file of 100 bytes?
Answer: A
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.
Q.387Medium
In a data structure serialization scenario, what is returned by fwrite() on successful completion?
Answer: A
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.
Q.388Medium
A competitive exam question requires processing a 500MB CSV file line by line. Which approach is most efficient in terms of memory?
Answer: B
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.
Q.389Medium
What is the critical issue if ferror(fp) returns non-zero during file operations in a banking application?
Answer: A
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()).
Q.390Medium
What will be the output of the following code?
int *ptr = malloc(sizeof(int));
if(ptr == NULL) printf("Failed");
else printf("Success");
Answer: B
In typical scenarios, malloc() successfully allocates memory and returns non-NULL pointer, printing 'Success'. NULL check is good practice for error handling.
Q.391Medium
What is a memory leak in C?
Answer: B
A memory leak occurs when allocated memory is not freed, consuming system resources without releasing them back.
Q.392Medium
What happens when you try to access memory after calling free()?
Answer: B
Accessing freed memory results in undefined behavior and is a critical bug. The memory may be reallocated for other purposes.
Q.393Medium
What is the purpose of realloc() function?
Answer: B
realloc() resizes a previously allocated memory block. It can increase or decrease the size and preserves existing data.
Q.394Medium
Which of the following is correct for dynamic array allocation?
Answer: B
Dynamic array allocation requires pointer and malloc() with proper size calculation. VLAs are not standard C.
Q.395Medium
What is double free error?
Answer: B
Double free error occurs when free() is called on the same memory block twice, causing undefined behavior and program crash.
Q.396Medium
Analyze this code:
char *str = malloc(5);
strcpy(str, "Hello");
How many bytes should malloc allocate for safety?
Answer: B
String "Hello" has 5 characters plus 1 null terminator (\0), requiring 6 bytes total.
Q.397Medium
What is the output?
int *p = malloc(sizeof(int) * 3);
p[0] = 1; p[1] = 2; p[2] = 3;
printf("%d", *(p+2));
Answer: C
*(p+2) is equivalent to p[2], which contains 3. Pointer arithmetic works with dynamic arrays.
Q.398Medium
Which of the following correctly allocates memory for an array of 5 strings (each max 20 characters)?
Answer: D
Option B uses dynamic allocation (2D array), Option C uses static allocation. Both are valid but for different scenarios.
Q.399Medium
What is the purpose of typecasting in malloc()?
int *p = (int*)malloc(sizeof(int));
Answer: C
Typecasting malloc() is optional in C but improves readability. It's mandatory in C++.
Q.400Medium
Analyze the memory leak in this code:
void func() {
int *p = malloc(100);
if(condition) return;
free(p);
}
Answer: B
When condition is true, the function returns without calling free(), causing a memory leak.