Input / Output - Point Out Correct Statements Multiple Questions and Answers.
Exercise Questions ::
Input / Output
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 |
|
|
| Answer: Option D |
|
|
| Explanation: |
|
This program counts the number of lines in the file myfile.c by counting the character '\n' in that file. |
| See More Information |
|
|
|
Tutorial Link: |
Published by:Michael Daani
2. | Which of the statements 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 writes a text to a file in reverse order |
| C. | The code reads a text files and display its content in reverse order |
| D. | None of above |
|
|
| Answer: Option C |
|
|
| Explanation: |
|
This program reads the file INPUT.TXT and store it in the string str after reversing the string using strrev function. |
| See More Information |
|
|
|
Tutorial Link: |
Published by:Michael Daani
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 |
|
|
| Answer: Option B |
|
|
| Explanation: |
|
This program get the input string from the user through gets function and store it in the file f1.txt using fputs function. |
| See More Information |
|
|
|
Tutorial Link: |
Published by:Michael Daani