C Programming

Point Out Correct Statements

Programming & Coding Exercise Mode

Point Out Correct Statements

Practice and master this topic with our carefully crafted questions.

3 Questions
5 Minutes
0% Completed
QUEST ? !
Question 1

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;
}
A
The code counts number of characters in the file
B
The code counts number of words in the file
C
The code counts number of blank lines in the file
D
The code counts number of lines in the file
Correct Answer: Option D

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

Consider the following C 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;
}

The program initializes the variable i to 1 because a file containing text has at least one line (provided it is not empty).

It then reads the file one character at a time using getc().

  • If the character read is '\n' (newline), the statement:
    i++;
    
    increments the line count.
  • Each newline character marks the end of one line and the beginning of the next.

For example, if the file contains:

Line 1
Line 2
Line 3

There are two newline characters, so:

Initial value of i = 1

After first '\n'  → i = 2
After second '\n' → i = 3

Thus, i represents the total number of lines in the file.

  • A. The code counts number of characters in the file ❌
    Incorrect. It only checks for newline characters and does not count every character.
  • B. The code counts number of words in the file ❌
    Incorrect. Words are usually counted by detecting spaces, tabs, or newlines.
  • C. The code counts number of blank lines in the file ❌
    Incorrect. The program increments the count for every newline, regardless of whether the line is blank.
  • D. The code counts number of lines in the file ✅
    Correct. Each newline character increases the line count by one.

Exam Fact:
A common method to count the number of lines in a text file is to initialize a counter to 1 and increment it every time a newline character ('\n') is encountered while reading the file. (This assumes the file is not empty.)

Question 2

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;
}
A
The code writes a text to a file
B
The code reads a text file and displays its content in reverse order
C
The code writes a text to a file in reverse order
D
None of above
Correct Answer: Option B

Correct Answer: B. The code reads a text file and displays its content in reverse order (word by word)

Consider the following C 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;
}

The program opens the file INPUT.TXT in read mode and reads it one character at a time using getc().

Characters are stored in the array str until a space (' ') or a newline ('\n') is encountered. At that point:

str[i] = '\0';
strrev(str);
printf("%s", str);

The string is terminated, reversed using strrev(), and then printed. This process repeats for every word in the file.

For example, if the file contains:

Hello World

The output will be:

olleHdlroW

Each word is reversed individually as it is read from the file. The program does not write anything back to the file.

  • A. The code writes a text to a file ❌
    Incorrect. The file is opened in "r" (read) mode, so nothing is written.
  • B. The code reads a text file and displays its content in reverse order ✅
    Correct. It reads the file and prints each word in reverse order.
  • C. The code writes a text to a file in reverse order ❌
    Incorrect. The program only displays the reversed words; it does not modify the file.
  • D. None of above ❌
    Incorrect. Option B is the expected answer.

Exam Fact:
The function strrev() (available in Turbo C and some non-standard C compilers) reverses a string in place. Since the file is opened in read mode ("r"), the program only reads and displays reversed words—it does not change the contents of the file.

Question 3

Point out the correct statements about the program?

#include<stdio.h>

int main()
{
    FILE *fptr;
    char str[80];

    fptr = fopen("f1.dat", "w");

    if(fptr == NULL)
        printf("Cannot open file");
    else
    {
        while(strlen(gets(str)) > 0)
        {
            fputs(str, fptr);
            fputs("\n", fptr);
        }
        fclose(fptr);
    }

    return 0;
}
A
The code copies the content of one file to another
B
The code writes strings that are read from the keyboard into a file.
C
The code reads a file
D
None of above
Correct Answer: Option B

Correct Answer: B. The code writes strings that are read from the keyboard into a file.

Consider the following C program:

#include<stdio.h>

int main()
{
    FILE *fptr;
    char str[80];

    fptr = fopen("f1.dat", "w");

    if(fptr == NULL)
        printf("Cannot open file");
    else
    {
        while(strlen(gets(str)) > 0)
        {
            fputs(str, fptr);
            fputs("\n", fptr);
        }
        fclose(fptr);
    }

    return 0;
}

The program opens the file f1.dat in write mode ("w"). It repeatedly reads a line of text from the keyboard using gets().

The loop:

while(strlen(gets(str)) > 0)

continues as long as the user enters a non-empty string.

Each string entered is written to the file using:

fputs(str, fptr);
fputs("\n", fptr);

Thus, every line typed from the keyboard is stored in f1.dat.

  • A. The code copies the content of one file to another ❌
    Incorrect. The program does not open or read any source file for copying.
  • B. The code writes strings that are read from the keyboard into a file. ✅
    Correct. It reads input using gets() and writes it to the file using fputs().
  • C. The code reads a file ❌
    Incorrect. The file is opened in write mode, and no data is read from it.
  • D. None of above ❌
    Incorrect. Option B correctly describes the program.

Exam Fact:
The function gets() reads a line of text from the keyboard (it is deprecated and unsafe in modern C). The function fputs() writes a string to a file. Opening a file with mode "w" creates the file if it does not exist or overwrites its existing contents.