Input / Output - Point Out Errors Multiple Questions and Answers.
Exercise Questions ::
Input / Output
1. Point out the error in the program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
unsigned char ;
FILE *fp;
fp=fopen("trial" , "r" );
if (!fp)
{
printf("Unable to open file" );
exit(1 );
}
fclose(fp);
return 0 ;
}
A. Error: in unsigned char statement B. Error: unknown file pointer C. No error D. None of above
View Answer
Discuss forum
Workplace
Report
Answer: Option C
Explanation:
This program tries to open the file trial.txt in read mode. If file not exists or unable to read it prints "Unable to open file " and then terminate the program.
If file exists, it simply close the file and then terminates the program.
See More Information
Tutorial Link:
Published by:Michael Daani
2. Point out the error in the program?
#include<stdio.h>
int main()
{
char ch;
int i;
scanf("%c" , &i);
scanf("%d" , &ch);
printf("%c %d" , ch, i);
return 0 ;
}
A. Error: suspicious char to in conversion in scanf() B. Error: we may not get input for second scanf() statement C. No error D. None of above
View Answer
Discuss forum
Workplace
Report
Published by:Michael Daani
3. Point out the error, if any in the program?
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("trial" , "r" );
fseek(fp, "20" , SEEK_SET);
fclose(fp);
return 0 ;
}
A. Error: unrecognised Keyword SEEK_SET B. Error: fseek() long offset value C. No Error D. None of above
View Answer
Discuss forum
Workplace
Report
Answer: Option B
Explanation:
Instead of "20" use 20L since fseek() need a long offset value.
See More Information
Tutorial Link:
Published by:Michael Daani
4. What will be the output of the program?
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("c:\tc\file.c" , "r" );
if (!fp)
printf("Unable to open file." );
fclose(fp);
return 0 ;
}
A. No error, No output. B. Program crashes at run time. C. Output: Unable to open file. D. None of above
View Answer
Discuss forum
Workplace
Report
Answer: Option C
Explanation:
The path of file name must be given as "c:\\tc\file.c"
See More Information
Tutorial Link:
Published by:Michael Daani
5. Point out the error 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 ;
}
A. Error: in unsigned char declaration B. Error: while statement C. No error D. It prints all characters in file "trial"
View Answer
Discuss forum
Workplace
Report
Answer: Option A
Explanation:
Here, EOF is -1. As 'ch' is declared as unsigned char it cannot deal with any negative value.
See More Information
Tutorial Link:
Published by:Michael Daani