C Programming - Input / Output
Exercise : Input / Output - Point Out Correct Statements
- Input / Output - General Questions
- Input / Output - Find Output of Program
- Input / Output - Point Out Errors
- Input / Output - Point Out Correct Statements
- Input / Output - True / False Questions
- Input / Output - Yes / No Questions
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;
}
Answer: Option
Explanation:
This program counts the number of lines in the file myfile.c by counting the character '\n' in that file.
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;
}
Answer: Option
Explanation:
This program reads the file INPUT.TXT and store it in the string str after reversing the string using strrev function.
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;
}
Answer: Option
Explanation:
This program get the input string from the user through gets function and store it in the file f1.txt using fputs function.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers