Here is a listing of C multiple choice questions on “Error Handling” along with answers, explanations and/or solutions:
1. What is the output of the following C code if there is no error in stream fp?
-
#include -
int main()
-
{ -
FILE *fp;
-
fp = fopen("newfile", "w");
-
printf("%dn", ferror(fp));
-
return 0;
-
}
a) Compilation error
b) 0
c) 1
d) Any nonzero value
Answer: b
Clarification: None.
2. Within main, return expr statement is equivalent to ________
a) abort(expr)
b) exit(expr)
c) ferror(expr)
d) none of the mentioned
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include -
int main()
-
{ -
FILE *fp;
-
char c;
-
int n = 0;
-
fp = fopen("newfile1.txt", "r");
-
while (!feof(fp))
-
{ -
c = getc(fp);
-
putc(c, stdout);
-
} -
}
a) Compilation error
b) Prints to the screen content of newfile1.txt completely
c) Prints to the screen some contents of newfile1.txt
d) None of the mentioned
Answer: d
Clarification: None.
4. What will be the output of the following C code?
-
#include -
int main()
-
{ -
FILE *fp = stdout;
-
stderr = fp;
-
fprintf(stderr, "%s", "hello");
-
}
a) Compilation error
b) hello
c) Undefined behaviour
d) Depends on the standard
Answer: b
Clarification: None.
5. What will be the output of the following C code?
-
#include -
int main()
-
{ -
char buf[12];
-
stderr = stdin;
-
fscanf(stderr, "%s", buf);
-
printf("%sn", buf);
-
}
a) Compilation error
b) Undefined behaviour
c) Whatever user types
d) None of the mentioned
Answer: c
Clarification: None.
6. stderr is similar to?
a) stdin
b) stdout
c) both stdout and stdin
d) none of the mentioned
Answer: a
Clarification: None.
7. What happens when we use the following C statement?
fprintf(stderr, "error: could not open filen");
a) The diagnostic output is directly displayed in the output
b) The diagnostic output is pipelined to the output file
c) The line which caused error is compiled again
d) The program is immediately aborted
Answer: a
Clarification: None.
8. Which of the following function can be used to terminate the main function from another function safely?
a) return(expr);
b) exit(expr);
c) abort();
d) both exit(expr); and abort();
Answer: b
Clarification: None.
