General Questions
Practice and master this topic with our carefully crafted questions.
In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
Correct Answer: C. "I am a boy\n\0" ✅
Consider the following line stored in a file:
I am a boy\r\n
Suppose it is read using fgets():
char str[50];
fgets(str, sizeof(str), fp);
In text mode, which is the default mode when a file is opened using "r", the C runtime translates the Windows line ending \r\n into a single newline character \n before the program receives the data.
Since fgets() retains the newline character, the string stored in str becomes:
"I am a boy\n\0"
- A.
"I am a boy\r\n\0"❌
Incorrect. This would occur only if the file were read in binary mode ("rb"), where no newline translation takes place. - B.
"I am a boy\r\0"❌
Incorrect. The carriage return alone is not retained in text mode. - C.
"I am a boy\n\0"✅
Correct. In text mode, the sequence\r\nis translated to\n, andfgets()stores the newline followed by the null terminator. - D.
"I am a boy"❌
Incorrect.fgets()does not remove the newline character; it stores it if there is enough room in the buffer.
Exam Fact:
When a file is opened in text mode (using "r"), Windows converts the line ending \r\n to a single \n during input. Therefore, fgets() stores \n followed by \0. If the file is opened in binary mode ("rb"), no translation occurs, and both \r and \n are read.
What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp;
fp = fopen("source.txt", "rb");
Correct Answer: A. Open "source.txt" in binary mode for reading. ✅
Consider the following C code:
FILE *fp;
fp = fopen("source.txt", "rb");
The fopen() function is used to open a file. The mode string determines how the file is opened.
Here, the mode is:
"rb"
r→ Opens an existing file for reading.b→ Opens the file in binary mode, preventing any text-mode translations (such as\r\nto\non Windows).
If the file does not exist, fopen() returns NULL.
- A. Open
"source.txt"in binary mode for reading. ✅
Correct. The mode"rb"opens an existing file for reading in binary mode. - B. Open
"source.txt"in binary mode for reading and writing. ❌
Incorrect. The mode for reading and writing in binary is"rb+"or"r+b". - C. Create a new file
"source.txt"for reading and writing. ❌
Incorrect. Modes such as"w+"or"wb+"create a new file (or overwrite an existing one). - D. None of above. ❌
Incorrect. Option A correctly describes the purpose of"rb".
Exam Fact:
Common fopen() modes are:
"r"– Read text file."rb"– Read binary file."w"– Write (creates a new file or overwrites an existing one)."a"– Append to the end of a file."r+"– Read and write an existing file."rb+"or"r+b"– Read and write an existing binary file.
What does fp point to in the program ?
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("trial", "r");
return 0;
}
Correct Answer: B. A structure which contains a char pointer which points to the first character of a file. ✅
Consider the following C program:
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("trial", "r");
return 0;
}
The fopen() function opens the file named "trial" in read mode and returns a pointer of type FILE *.
The FILE type is a structure (defined in <stdio.h>) that stores information about the opened file, such as its current position, buffering information, file status, and error indicators. The pointer fp points to this FILE structure, not directly to the file contents.
- A. The first character in the file. ❌
Incorrect.fpdoes not point directly to the first character of the file. It points to aFILEstructure that manages the file stream. - B. A structure which contains a char pointer which points to the first character of a file. ✅
Correct.fppoints to aFILEstructure maintained by the C runtime library. This structure contains information used to access the file, including the current file position. - C. The name of the file. ❌
Incorrect. The filename is only used byfopen()to locate and open the file.fpdoes not point to the filename. - D. The last character in the file. ❌
Incorrect.fpdoes not point to any specific character in the file.
Exam Fact:
The FILE type is an implementation-defined structure used by the C standard library to manage file streams. A FILE * returned by fopen() points to this structure, which stores information such as the current file position, buffering details, error flags, and EOF status—not directly to the file data itself.
Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp;
fp = fopen("NOTES.TXT", "r+");
Correct Answer: D. Read and Write ✅
Consider the following C code:
FILE *fp;
fp = fopen("NOTES.TXT", "r+");
The fopen() function opens a file in the mode specified by the second argument.
Here, the mode is:
"r+"
r→ Opens an existing file for reading.+→ Allows both reading and writing on the same file.- The file must already exist. If it does not exist,
fopen()returnsNULL. - The file is not truncated (its existing contents are preserved).
- The file pointer is positioned at the beginning of the file.
- A. Reading ❌
Incorrect. Reading is supported, but"r+"also allows writing. - B. Writing ❌
Incorrect. Writing is supported, but"r+"also allows reading. - C. Appending ❌
Incorrect. Appending requires modes such as"a"or"a+". In"r+", writing begins at the current file position, not necessarily at the end of the file. - D. Read and Write ✅
Correct. The mode"r+"opens an existing file for both reading and writing.
Exam Fact:
Common fopen() modes are:
"r"– Read only."w"– Write only (creates a new file or overwrites an existing one)."a"– Append only."r+"– Read and write an existing file."w+"– Read and write (creates a new file or truncates an existing one)."a+"– Read and append (writes always occur at the end of the file).
To print out a and b given below, which of the following printf() statement will you use?
#include<stdio.h>
float a = 3.14;
double b = 3.14;
Correct Answer: A. printf("%f %lf", a, b); ✅
Consider the following C program:
#include<stdio.h>
float a = 3.14;
double b = 3.14;
To print the values of a and b, we use printf().
When a float is passed to printf(), it is automatically promoted to double because of the default argument promotions used with variadic functions.
In printf():
%fprints a floating-point value (double).%lfis also accepted byprintf()and behaves the same as%f. (The distinction between%fand%lfis important inscanf(), not inprintf().)%Lfis used only for along double.
Therefore, the correct statement is:
printf("%f %lf", a, b);
- A.
printf("%f %lf", a, b);✅
Correct.a(promoted todouble) is printed using%f, andbcan be printed using%lf(which behaves the same as%finprintf()). - B.
printf("%Lf %f", a, b);❌
Incorrect.%Lfexpects along double, butais afloat. - C.
printf("%Lf %Lf", a, b);❌
Incorrect. Both format specifiers expectlong double, but neither variable is of that type. - D.
printf("%f %Lf", a, b);❌
Incorrect.bis adouble, not along double.
Exam Fact:
For printf(), a float is automatically promoted to double, so %f is sufficient. The %Lf specifier is reserved for long double. The difference between %f and %lf matters in scanf(), where %f reads a float and %lf reads a double.
Which files will get closed through the fclose() in the following program?
#include<stdio.h>
int main()
{
FILE *fs, *ft, *fp;
fp = fopen("A.C", "r");
fs = fopen("B.C", "r");
ft = fopen("C.C", "r");
fclose(fp, fs, ft);
return 0;
}
Correct Answer: D. Error in fclose() ✅
Consider the following C program:
#include<stdio.h>
int main()
{
FILE *fs, *ft, *fp;
fp = fopen("A.C", "r");
fs = fopen("B.C", "r");
ft = fopen("C.C", "r");
fclose(fp, fs, ft);
return 0;
}
The function fclose() is declared as:
int fclose(FILE *stream);
It accepts only one argument, which must be a pointer to an open file (FILE *).
However, the program calls:
fclose(fp, fs, ft);
This passes three arguments to fclose(), which is invalid. The compiler reports an error because the function prototype expects only one argument.
To close all three files, each file must be closed separately:
fclose(fp);
fclose(fs);
fclose(ft);
- A.
"A.C" "B.C" "C.C"❌
Incorrect. A single call tofclose()cannot close multiple files. - B.
"B.C" "C.C"❌
Incorrect. The call itself is invalid, so no files are closed. - C.
"A.C"❌
Incorrect. Althoughfpis the first argument, the function call is invalid because of the extra arguments. - D.
Error in fclose()✅
Correct.fclose()accepts only oneFILE *argument. Passing three arguments results in a compilation error.
Exam Fact:
The prototype of fclose() is int fclose(FILE *stream);. Each open file stream must be closed with a separate call to fclose(). Passing multiple file pointers in a single call is invalid.
On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?
#include<stdio.h>
int main()
{
int i, fss;
char ch, source[20] = "source.txt", target[20] = "target.txt", t;
FILE *fs, *ft;
fs = fopen(source, "r");
ft = fopen(target, "w");
while(1)
{
ch = getc(fs);
if(ch == EOF)
break;
else
{
fseek(fs, 4L, SEEK_CUR);
fputc(ch, ft);
}
}
return 0;
}
Correct Answer: B. Trh ✅
Consider the following C program:
#include<stdio.h>
int main()
{
int i, fss;
char ch, source[20] = "source.txt", target[20] = "target.txt", t;
FILE *fs, *ft;
fs = fopen(source, "r");
ft = fopen(target, "w");
while(1)
{
ch = getc(fs);
if(ch == EOF)
break;
else
{
fseek(fs, 4L, SEEK_CUR);
fputc(ch, ft);
}
}
return 0;
}
The source file contains:
To err is human
Let's trace the execution.
- Iteration 1:
The charactergetc(fs) → 'T''T'is written totarget.txt. After reading'T', the file pointer moves to'o'. Then:
skips the next four characters:fseek(fs, 4L, SEEK_CUR);
The next character to be read is:o _ e rr - Iteration 2:
The charactergetc(fs) → 'r''r'is written to the target file. Again,fseek()skips four characters:
The next character becomes:_ i s _h - Iteration 3:
The charactergetc(fs) → 'h''h'is written to the target file. After skipping four more characters, the file pointer reaches the end of the file. - Iteration 4:
The loop terminates.getc(fs) → EOF
Therefore, the contents of target.txt are:
Trh
- A.
r n❌
Incorrect. The program begins by writing'T', not'r'. - B.
Trh✅
Correct. The program writes every fifth character starting from the first character. - C.
err❌
Incorrect. The characters are not copied consecutively. - D.
None of above❌
Incorrect. Option B is the correct output.
Exam Fact:
fseek(fp, n, SEEK_CUR) moves the file pointer n bytes forward from its current position. In this program, after each character is read using getc(), the pointer skips four more characters, effectively copying every fifth character from the source file to the destination file.
To scan a and b given below, which of the following scanf() statement will you use?
#include<stdio.h>
float a;
double b;
Correct Answer: D. scanf("%f %lf", &a, &b); ✅
Consider the following C declarations:
#include<stdio.h>
float a;
double b;
The scanf() function requires format specifiers that match the data types of the variables being read.
%f→ Reads a value of typefloat.%lf→ Reads a value of typedouble.%Lf→ Reads a value of typelong double.
Therefore, to read a float and a double, the correct statement is:
scanf("%f %lf", &a, &b);
- A.
scanf("%f %f", &a, &b);❌
Incorrect. The second%fexpects a pointer tofloat, butbis adouble. - B.
scanf("%Lf %Lf", &a, &b);❌
Incorrect.%Lfis used for variables of typelong double, notfloatordouble. - C.
scanf("%f %Lf", &a, &b);❌
Incorrect. The second format specifier expects along double, butbis adouble. - D.
scanf("%f %lf", &a, &b);✅
Correct.%freads afloat, and%lfreads adouble.
Exam Fact:
The format specifiers for printf() and scanf() differ for floating-point types. In printf(), both %f and %lf print a double, whereas in scanf(), %f reads a float, %lf reads a double, and %Lf reads a long double.
Out of fgets() and gets() which function is safe to use?
Correct Answer: B. fgets() ✅
The functions gets() and fgets() are both used to read a string from the standard input, but they differ significantly in terms of safety.
gets()does not check the size of the input buffer. If the user enters more characters than the buffer can hold, it causes a buffer overflow, leading to undefined behavior and potential security vulnerabilities.fgets()accepts the maximum number of characters to read, preventing buffer overflow. It safely reads at mostn - 1characters and appends a null character ('\0').
Example:
char str[20];
fgets(str, sizeof(str), stdin);
The above statement safely reads up to 19 characters and stores the input in str.
- A.
gets()❌
Incorrect.gets()is unsafe because it performs no bounds checking and can cause buffer overflow. It has been removed from the C11 standard. - B.
fgets()✅
Correct.fgets()limits the number of characters read, making it the safe function for reading strings.
Exam Fact:
Always prefer fgets() over gets(). The gets() function was deprecated in C99 and completely removed from the C11 standard because it cannot prevent buffer overflow attacks.
Consider the following program and what will be content of t?
#include<stdio.h>
int main()
{
FILE *fp;
int t;
fp = fopen("DUMMY.C", "w");
t = fileno(fp);
printf("%d\n", t);
return 0;
}
Correct Answer: B. The handle associated with "DUMMY.C" file ✅
Consider the following C program:
#include<stdio.h>
int main()
{
FILE *fp;
int t;
fp = fopen("DUMMY.C", "w");
t = fileno(fp);
printf("%d\n", t);
return 0;
}
The fileno() function returns the file descriptor (file handle) associated with the specified file stream.
In this program:
fopen("DUMMY.C", "w")opens (or creates) the file"DUMMY.C"for writing and returns aFILE *stream.fileno(fp)returns the integer file descriptor associated with that stream.- The exact value printed depends on the operating system and the currently open files. It is typically a small non-negative integer (for example,
3on many systems), but the actual value is implementation-dependent.
- A. Size of
"DUMMY.C"file ❌
Incorrect.fileno()does not return the file size. - B. The handle associated with
"DUMMY.C"file ✅
Correct.fileno()returns the file descriptor (handle) corresponding to the opened file stream. - C. Garbage value ❌
Incorrect. Iffopen()succeeds,fileno()returns a valid file descriptor. - D. Error in
fileno()❌
Incorrect. The function is valid and works correctly when passed a validFILE *.
Exam Fact:
fileno(FILE *stream) converts a C stream (FILE *) into its underlying integer file descriptor. It is commonly used when mixing standard C I/O functions with low-level operating system file operations such as read(), write(), and close().