C Programming Questions and Answers – Error Handling – 1

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Error Handling – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. What is the output of the following C code if there is no error in stream fp?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         FILE *fp;
  5.         fp = fopen("newfile", "w");
  6.         printf("%d\n", ferror(fp));
  7.         return 0;
  8.     }

a) Compilation error
b) 0
c) 1
d) Any nonzero value
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

2. Within main, return expr statement is equivalent to ________
a) abort(expr)
b) exit(expr)
c) ferror(expr)
d) none of the mentioned
View Answer

Answer: b
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

3. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         FILE *fp;
  5.         char c;
  6.         int n = 0;
  7.         fp = fopen("newfile1.txt", "r");
  8.         while (!feof(fp))
  9.         {
  10.             c = getc(fp);
  11.             putc(c, stdout);
  12.         }
  13.     }

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
View Answer

Answer: d
Explanation: None.
advertisement

4. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         FILE *fp = stdout;
  5.         stderr = fp;
  6.         fprintf(stderr, "%s", "hello");
  7.     }

a) Compilation error
b) hello
c) Undefined behaviour
d) Depends on the standard
View Answer

Answer: b
Explanation: None.

5. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char buf[12];
  5.         stderr = stdin;
  6.         fscanf(stderr, "%s", buf);
  7.         printf("%s\n", buf);
  8.     }

a) Compilation error
b) Undefined behaviour
c) Whatever user types
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

6. stderr is similar to?
a) stdin
b) stdout
c) both stdout and stdin
d) none of the mentioned
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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();
View Answer

Answer: b
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.