Input Output Questions with Answers

10 practice questions on Input Output (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Medium

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?

(The file is opened in text mode on Windows.)

  1. "I am a boy\r\n\0"
  2. "I am a boy\r\0"
  3. "I am a boy\n\0"
  4. "I am a boy"

Answer: C. "I am a boy\n\0"

Explanation

Declaration: char *fgets(char *s, int n, FILE *stream);
fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.
Therefore, the string str contain "I am a boy\n\0"

Step-by-step solution

  1. fgets reads up to n-1 characters or until a newline.
  2. The newline is kept in the buffer.
  3. A terminating null is added after it.
  4. So the string holds I am a boy, newline, null.

Why the other options are wrong

  • A. In a C text stream the end of line is read as a single newline, not carriage return plus newline.
  • B. A lone carriage return is not what fgets stores for a line end.
  • D. fgets keeps the newline and adds a null, so the string is not bare.

Tip: fgets keeps the newline character.

Question 2Medium

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;
}
  1. size of "DUMMY.C" file
  2. The handle associated with "DUMMY.C" file
  3. Garbage value
  4. Error in fileno()

Answer: B. The handle associated with "DUMMY.C" file

Explanation

fp = fopen("DUMMY.C", "w");

A file
DUMMY.C is opened in write mode and returns the file pointer to fp

t = fileno(fp);

returns the handle for,the fp stream and it stored in the variable t

printf("%d\n", t);

It prints the handle number.

Step-by-step solution

  1. The program opens a file named 'DUMMY.C' in write mode.
  2. It assigns the file pointer to the variable fp.
  3. The function fileno(fp) retrieves the file descriptor associated with the file pointer.
  4. This file descriptor is stored in the variable t.
  5. Finally, the program prints the value of t, which is the handle for the file stream.

Why the other options are wrong

  • A. The size of the file is not relevant to the file descriptor.
  • C. There are no uninitialized variables or garbage values involved.
  • D. The fileno() function does not return an error; it returns a valid file descriptor.

Tip: Remember that fileno() returns a file descriptor, not the size of the file or any other metadata.

Question 3Medium

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

  1. gets()
  2. fgets()
  3. Both
  4. None of above

Answer: B. fgets()

Explanation

Because, In fgets() we can specify the size of the buffer into which the string supplied will be stored)

Step-by-step solution

  1. gets() reads input with no limit on length, so it can overflow the buffer.
  2. fgets() takes a size argument and stops after size minus 1 characters.
  3. So fgets() is the safe function, which is option B.

Why the other options are wrong

  • A. gets() has no size limit, so it is unsafe; it was removed from C11.
  • C. Both is wrong because gets() is unsafe.
  • D. None is wrong because fgets() is safe.

Tip: Use fgets() instead of gets().

Question 4Medium

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

FILE *fp;
fp = fopen("NOTES.TXT", "r+");
  1. Reading
  2. Writing
  3. Appending
  4. Read and Write

Answer: D. Read and Write

Explanation

r+ Open an existing file for update (reading and writing).

Step-by-step solution

  1. The mode string 'r+' opens an existing file for both reading and writing.
  2. The 'r' part allows reading from NOTES.TXT.
  3. The '+' part adds writing to the same open file.
  4. So both reading and writing are possible on NOTES.TXT.
  5. The answer is Read and Write.

Why the other options are wrong

  • A. Reading alone is allowed, but r+ also allows writing.
  • B. Writing alone is allowed, but r+ also allows reading.
  • C. Appending needs an 'a' mode; r+ does not append.

Tip: R+ is read and write on an existing file; w+ and a+ are the ones that create or append.

Question 5Medium

What does fp point to in the program?

#include <stdio.h>
int main()
 {
    FILE *fp;
    fp=fopen("trial", "r");
    return 0;
 }
  1. The first character in the file
  2. At random charactor
  3. The name of the file.
  4. The last character in the file.

Answer: A. The first character in the file

Explanation

The fp is a structure which contains a char pointer which points to the first character of a file.

Step-by-step solution

  1. fopen("trial", "r") opens the file for reading.
  2. fp points to the file stream, positioned at the start.
  3. It does not point to the name or the end.
  4. So it points at the first character: option A.

Why the other options are wrong

  • B. It is not random; reading starts at the beginning.
  • C. The name is only an argument to fopen.
  • D. Reading mode starts at the beginning, not the end.

Tip: Mode r opens at the start of the file; a opens at the end.

Question 6Medium

Point out the error/warning in the program?

#include <stdio.h>
int main()
{
    unsigned char ch;
    FILE *fp;
    fp=fopen("trial", "r");
    while((ch = getc(fp))!=EOF)
        printf("%c", ch);
    fclose(fp);
    return 0;
}
  1. Error: in unsigned char declaration
  2. Error: while statement
  3. No error
  4. It prints all characters in file "trial"

Answer: A. Error: in unsigned char declaration

Explanation

Here, EOF is -1. As 'ch' is declared as unsigned char it cannot deal with any negative value.

Step-by-step solution

  1. Ch is declared as unsigned char, which can hold only the values 0 to 255.
  2. Getc returns EOF, which is -1, but storing -1 in an unsigned char gives 255.
  3. The test compares the int value 255 with EOF, which is -1, so it is never equal and the loop never ends.
  4. So the fault is in the declaration of ch, which is option A: Error: in unsigned char declaration.

Why the other options are wrong

  • B. The while test is written correctly; the problem is the type of ch.
  • C. There is an error, because ch cannot hold EOF.
  • D. The loop never ends, so it does not stop after printing the file.

Tip: Use int, not char, to store getc results so EOF can be detected.

Question 7Medium

Which of the following statement is correct about the program?

#include <stdio.h>
int main()
{
    FILE *fp;
    char ch;
    int i=1;
    fp = fopen("myfile.c", "r");
    while((ch=getc(fp))!=EOF)
    {
        if(ch == '\n')
            i++;
    }
    fclose(fp);
    return 0;
}
  1. The code counts number of characters in the file
  2. The code counts number of words in the file
  3. The code counts number of blank lines in the file
  4. The code counts number of lines in the file

Answer: D. The code counts number of lines in the file

Explanation

This program counts the number of lines in the file myfile.c by counting the character '\n' in that file.

Step-by-step solution

  1. The loop reads one character at a time with getc until EOF.
  2. Each time the character is the newline character, i is increased by 1.
  3. I starts at 1 and gains 1 for each newline, so at the end it is the number of lines in the file.
  4. So the answer is The code counts number of lines in the file.

Why the other options are wrong

  • A. The code does not count characters; it only checks for the newline character.
  • B. Words are not counted, because spaces are not checked.
  • C. Blank lines are not counted separately; every line, blank or not, is counted.

Tip: To count lines in a file, count the newline character.

Question 8Medium

Which of the following statement is correct about the program?

#include <stdio.h>
int main()
{
    FILE *fp;
    char str[11], ch;
    int i=0;
    fp = fopen("INPUT.TXT", "r");
    while((ch=getc(fp))!=EOF)
    {
        if(ch == '\n' || ch == ' ')
        {
            str[i]='\0';
            strrev(str);
            printf("%s", str);
            i=0;
        }
        else
            str[i++]=ch;
    }
    fclose(fp);
    return 0;
}
  1. The code writes a text to a file
  2. The code reads a text file and displays its content
  3. The code writes a text to a file in reverse order
  4. None of above

Answer: D. None of above

Explanation

The program only reads INPUT.TXT. It collects each word, reverses it with strrev() and prints it, so the file's words appear reversed and run together (spaces are not printed). It never writes to a file and does not show the content as it is, so none of the given statements is correct.

Step-by-step solution

  1. The program opens INPUT.TXT for reading and reads one character at a time.
  2. On each space or newline it reverses the word in str and prints it.
  3. It prints the reversed words to the screen; it does not write to a file.
  4. A word longer than 10 characters overflows str, and the last word is not printed at end of file.
  5. None of the given statements describes this exactly, so the answer is None of above.

Why the other options are wrong

  • A. The code never writes to a file; it only prints with printf.
  • B. It shows each word reversed, not the file's contents as they are.
  • C. It reverses each word, not the order of the text, and prints to the screen.

Tip: Read the code for what it writes to, not only what it reads from.

Question 9Medium

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

FILE *fp;
fp = fopen("source.txt", "rb");
  1. open "source.txt" in binary mode for reading
  2. open "source.txt" in binary mode for reading and w
  3. Create a new file "source.txt" for reading and wri
  4. None of above

Answer: A. open "source.txt" in binary mode for reading

Explanation

The mode "rb" opens the file for reading in binary mode. The r means read and the b means binary, so the bytes are read without any text conversion.

Step-by-step solution

  1. Fopen with mode rb opens the file for reading in binary mode.
  2. The r means read, and b means binary, so no newline translation takes place.
  3. The mode does not write or create a file, which rules out options B and C.
  4. The answer is open "source.txt" in binary mode for reading.

Why the other options are wrong

  • B. The mode rb does not allow writing, so the and w part is wrong.
  • C. Rb does not create a new file; it needs an existing file to read.
  • D. Option A is correct, so none of the above is wrong.

Tip: R is read, w is write, a is append, and b adds binary mode.

Question 10Medium

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;
}
  1. "A)C" "B)C" "C)C"
  2. "B)C" "C)C"
  3. "A)C"
  4. Error in fclose()

Answer: D. Error in fclose()

Explanation

fclose() takes exactly one FILE pointer, so the call fclose(fp, fs, ft) gives a compile error. Each file needs its own fclose() call.

Step-by-step solution

  1. fclose() takes exactly one argument, the FILE pointer to close.
  2. The call fclose(fp, fs, ft) passes three arguments.
  3. The compiler rejects this as too many arguments, so the program does not compile.
  4. So the result is Error in fclose().

Why the other options are wrong

  • A. The program does not compile, so no file is closed by it.
  • B. No file is closed because the call is invalid.
  • C. The error is for the whole call, not for one file.

Tip: Check the number of arguments to standard functions; fclose takes one FILE pointer.

Practise all 10 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics