C Programming Multiple Choice Questions and Answers on Error Handling in File for Freshers

1. What is the output of this C code if there is no error in stream fp?
  1.     #include 
  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
Answer: b
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
3. What is the output of this C code?
  1.     #include 
  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
Answer: d
4. What is the output of this C code?
  1.     #include 
  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
Answer: b
5. What is the output of this C code?
  1.     #include 
  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
Answer: c
6. stderr is similar to?

a) stdin
b) stdout
c) Both stdout and stdin
d) None of the mentioned
Answer: a
7. What happens when we use
    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
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
9. Which of the following causes an error?

a) Trying to read a file that doesn’t exist
b) Inability to write data in a file
c) Failure to allocate memory with the help of malloc
d) All of the mentioned
Answer: d
10. What is the purpose of the function?
    int ferror(FILE *fp)

a) They check for input errors
b) They check for output errors
c) They check for all types of errors
d) They check for error in accessing the file
Answer: b
11. stderr is similar to?
a) stdin
b) stdout
c) Both stdout and stdin
d) None of the mentioned
Answer: b

Explanation: stderr is not exactly same as stdout, but similar in the sense that both puts the output or error to the monitor
12. What happens when we use?
    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
13. 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
14. Which of the following causes an error?

a) Trying to read a file that doesn’t exist
b) Inability to write data in a file.
c) Failure to allocate memory with the help of malloc
d) All of the menioned
Answer: d
15. What is the purpose of the function?
    int ferror(FILE *fp)

a) They check for input errors
b) They check for output errors
c) They check for all types of errors
d) They check for error in accessing the file
Answer: b

Related

Multiple Choice Questions 5594677432227796161

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item