C Programming
C language from basics to advanced placement prep
198 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 198 questions
Q.41 Hard Dynamic Memory
Which statement about dynamic memory is TRUE?
A Dynamic memory is faster than stack memory
B Dynamic memory persists until explicitly freed
C Dynamic memory is limited by cache size
D Dynamic memory is automatically freed at function end
Correct Answer:  B. Dynamic memory persists until explicitly freed
EXPLANATION

Dynamic memory persists until free() is called, unlike automatic variables which are freed at function end.

Take Test
Q.42 Hard Dynamic Memory
What will happen if malloc() fails and returns NULL but code doesn't check?
A Program automatically allocates from stack
B Dereferencing NULL causes undefined behavior/crash
C Memory is automatically reallocated
D Exception is raised
Correct Answer:  B. Dereferencing NULL causes undefined behavior/crash
EXPLANATION

Dereferencing a NULL pointer causes undefined behavior, typically resulting in segmentation fault or program crash.

Take Test
Q.43 Hard File Handling
In a program that processes binary files with variable-length records, what could cause fread() to return fewer items than requested without reaching EOF?
A A read error occurred during I/O operation
B The file is opened in text mode instead of binary mode
C The buffer size is larger than remaining file size
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

fread() can return fewer items due to: (A) I/O errors, (B) text mode conversions affecting byte count, or (C) insufficient data remaining. For robust code, check ferror() and feof() after fread() returns less than requested.

Take Test
Q.44 Hard File Handling
For implementing a file locking mechanism in a competitive exam environment, which approach correctly handles concurrent file access in POSIX systems?
A Using flock() or fcntl() system calls with F_WRLCK
B Opening file in 'a' mode ensures automatic locking
C Using fseek() to check if file is accessible
D Creating a separate lock file and checking its existence
Correct Answer:  A. Using flock() or fcntl() system calls with F_WRLCK
EXPLANATION

flock() and fcntl() with F_WRLCK provide kernel-level file locking in POSIX systems. Option B is false, option C doesn't provide locking, and option D is unreliable due to race conditions.

Take Test
Q.45 Hard File Handling
A program reads serialized objects using fread(). What's the primary risk when file format changes between versions?
A fread() will fail silently
B Buffer overflow or misaligned struct interpretation
C File size becomes invalid
D Automatic version detection prevents issues
Correct Answer:  B. Buffer overflow or misaligned struct interpretation
EXPLANATION

Changed file format causes struct misalignment, leading to buffer overflow or garbage data. No automatic detection in C. Need version headers or checksums.

Take Test
Q.46 Hard File Handling
When implementing a log file writer with concurrent access, what's the critical issue with standard fopen()?
A fopen() is too slow
B No built-in file locking mechanism
C fopen() doesn't support append atomically
D Memory leaks after multiple opens
Correct Answer:  B. No built-in file locking mechanism
EXPLANATION

Standard C fopen/fwrite lack file locking. Multiple processes can corrupt data. Need OS-specific locking (flock/fcntl on Unix) or use 'a' mode with small writes.

Take Test
Q.47 Hard File Handling
For competitive exam file validation: A 2GB file must be processed without loading entirely in memory. Which strategy is optimal?
A Read entire file into heap-allocated buffer
B Use mmap() for memory-mapped I/O
C Read in fixed-size chunks in loop
D Use external sorting tools
Correct Answer:  C. Read in fixed-size chunks in loop
EXPLANATION

Fixed-size chunk processing is standard for large files. mmap() helps but may not be portable. Loading entire file exhausts memory. External tools bypass C programming.

Take Test
Q.48 Hard File Handling
A program processes encrypted binary data with fread(). Why might using 'r' mode instead of 'rb' cause corruption?
A Text mode converts line endings, corrupting binary data
B Text mode stops at null bytes
C Text mode is slower
D Text mode reduces buffer size
Correct Answer:  A. Text mode converts line endings, corrupting binary data
EXPLANATION

Text mode on Windows converts CRLF to LF and vice versa, corrupting binary. Binary mode preserves exact bytes. Null byte handling is secondary concern.

Take Test
Q.49 Hard File Handling
What does rewind(fp) accomplish compared to fseek(fp, 0, SEEK_SET)?
A rewind() is faster
B rewind() also clears error indicators; fseek() doesn't
C They are functionally identical
D fseek() works only in binary mode
Correct Answer:  B. rewind() also clears error indicators; fseek() doesn't
EXPLANATION

rewind(fp) resets pointer AND clears EOF/error flags. fseek(fp, 0, SEEK_SET) only repositions. rewind is essentially: fseek(...); clearerr(...);

Take Test
Q.50 Hard File Handling
For reading a configuration file line-by-line where lines may exceed 256 characters, which function is recommended for ISRO/GATE 2024 exams?
A fgets(buffer, sizeof(buffer), fp) with dynamic memory if needed
B fscanf(fp, "%s", buffer) with fixed-size array
C fgetc() in a loop to manually read lines
D fread() to read entire file at once
Correct Answer:  A. fgets(buffer, sizeof(buffer), fp) with dynamic memory if needed
EXPLANATION

fgets() is safest for line reading with size protection. For lines exceeding buffer size, use dynamic allocation or getline(). fscanf() with %s is vulnerable to buffer overflow.

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