C Programming

General Questions

Programming & Coding Exercise Mode

General Questions

Practice and master this topic with our carefully crafted questions.

10 Questions
15 Minutes
0% Completed
QUEST ? !
Question 1

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?

A
"I am a boy\r\n\0"
B
"I am a boy\r\0"
C
"I am a boy\n\0"
D
"I am a boy"
Correct Answer: Option C

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\n is translated to \n, and fgets() 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.

Question 2

What is the purpose of "rb" in fopen() function used below in the code?

FILE *fp;

fp = fopen("source.txt", "rb");
A
Open "source.txt" in binary mode for reading.
B
open "source.txt" in binary mode for reading and writing
C
Create a new file "source.txt" for reading and writing
D
None of above
Correct Answer: Option A

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\n to \n on 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.

Question 3

What does fp point to in the program ?

#include<stdio.h>

int main()
{
    FILE *fp;
    fp = fopen("trial", "r");
    return 0;
}
A
The first character in the file.
B
A structure which contains a char pointer which points to the first character of a file.
C
The name of the file.
D
The last character in the file.
Correct Answer: Option B

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. fp does not point directly to the first character of the file. It points to a FILE structure that manages the file stream.
  • B. A structure which contains a char pointer which points to the first character of a file. ✅
    Correct. fp points to a FILE structure 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 by fopen() to locate and open the file. fp does not point to the filename.
  • D. The last character in the file. ❌
    Incorrect. fp does 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.

Question 4

Which of the following operations can be performed on the file "NOTES.TXT" using the below code?

FILE *fp;

fp = fopen("NOTES.TXT", "r+");
A
Reading
B
Writing
C
Appending
D
Read and Write
Correct Answer: Option D

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() returns NULL.
  • 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).

Question 5

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;
A
printf("%f %lf", a, b);
B
printf("%Lf %f", a, b);
C
printf("%Lf %Lf", a, b);
D
printf("%f %Lf", a, b);
Correct Answer: Option A

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():

  • %f prints a floating-point value (double).
  • %lf is also accepted by printf() and behaves the same as %f. (The distinction between %f and %lf is important in scanf(), not in printf().)
  • %Lf is used only for a long double.

Therefore, the correct statement is:

printf("%f %lf", a, b);
  • A. printf("%f %lf", a, b);
    Correct. a (promoted to double) is printed using %f, and b can be printed using %lf (which behaves the same as %f in printf()).
  • B. printf("%Lf %f", a, b);
    Incorrect. %Lf expects a long double, but a is a float.
  • C. printf("%Lf %Lf", a, b);
    Incorrect. Both format specifiers expect long double, but neither variable is of that type.
  • D. printf("%f %Lf", a, b);
    Incorrect. b is a double, not a long 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.

Question 6

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;
}
A
"A.C" "B.C" "C.C"
B
"B.C" "C.C"
C
"A.C"
D
Error in fclose()
Correct Answer: Option D

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 to fclose() cannot close multiple files.
  • B. "B.C" "C.C"
    Incorrect. The call itself is invalid, so no files are closed.
  • C. "A.C"
    Incorrect. Although fp is the first argument, the function call is invalid because of the extra arguments.
  • D. Error in fclose()
    Correct. fclose() accepts only one FILE * 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.

Question 7

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;
}
A
r n
B
Trh
C
err
D
None of above
Correct Answer: Option B

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:
    getc(fs)  → 'T'
    
    The character 'T' is written to target.txt. After reading 'T', the file pointer moves to 'o'. Then:
    fseek(fs, 4L, SEEK_CUR);
    
    skips the next four characters:
    o  _  e  r
    
    The next character to be read is:
    r
    
  • Iteration 2:
    getc(fs) → 'r'
    
    The character 'r' is written to the target file. Again, fseek() skips four characters:
    _  i  s  _
    
    The next character becomes:
    h
    
  • Iteration 3:
    getc(fs) → 'h'
    
    The character 'h' is written to the target file. After skipping four more characters, the file pointer reaches the end of the file.
  • Iteration 4:
    getc(fs) → EOF
    
    The loop terminates.

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.

Question 8

To scan a and b given below, which of the following scanf() statement will you use?

#include<stdio.h>

float a;
double b;
A
scanf("%f %f", &a, &b);
B
scanf("%Lf %Lf", &a, &b);
C
scanf("%f %Lf", &a, &b);
D
scanf("%f %lf", &a, &b);
Correct Answer: Option D

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 type float.
  • %lf → Reads a value of type double.
  • %Lf → Reads a value of type long 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 %f expects a pointer to float, but b is a double.
  • B. scanf("%Lf %Lf", &a, &b);
    Incorrect. %Lf is used for variables of type long double, not float or double.
  • C. scanf("%f %Lf", &a, &b);
    Incorrect. The second format specifier expects a long double, but b is a double.
  • D. scanf("%f %lf", &a, &b);
    Correct. %f reads a float, and %lf reads a double.

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.

Question 9

Out of fgets() and gets() which function is safe to use?

A
gets()
B
fgets()
Correct Answer: Option B

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 most n - 1 characters 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.

Question 10

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;
}
A
Size of "DUMMY.C" file
B
The handle associated with "DUMMY.C" file
C
Garbage value
D
Error in fileno()
Correct Answer: Option B

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 a FILE * 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, 3 on 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. If fopen() succeeds, fileno() returns a valid file descriptor.
  • D. Error in fileno()
    Incorrect. The function is valid and works correctly when passed a valid FILE *.

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().