Computer StudyMCQ Computer ScienceQuiz Computer ScienceUGC NET

C Programming Questions and Answers – Error Handling 

C Programming Questions and Answers – Error Handling

 

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

#include <stdio.h>

int main()

{

FILE *fp;

fp = fopen(“newfile”, “w”);

printf(“%d\n”, ferror(fp));

return 0;

}

a) Compilation error

b) 0

c) 1

d) Any nonzero value

View Answer

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

View Answer

Answer:b 

3.What is the output of this C code?

#include <stdio.h>

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

View Answer

Answer:d 

4.What is the output of this C code?

#include <stdio.h>

int main()

{

FILE *fp = stdout;

stderr = fp;

fprintf(stderr, “%s”, “hello”);

}

a) Compilation error

b) hello

c) Undefined behaviour

d) Depends on the standard

View Answer

Answer:b  

5.What is the output of this C code?

#include <stdio.h>

int main()

{

char buf[12];

stderr = stdin;

fscanf(stderr, “%s”, buf);

printf(“%s\n”, buf);

}

a) Compilation error

b) Undefined behaviour

c) Whatever user types

d) None of the mentioned

View Answer

Answer:c 

6.stderr is similar to?

a) stdin

b) stdout

c) Both stdout and stdin

d) None of the mentioned

View Answer

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.

View Answer

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 b and c

View Answer

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

View Answer

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

View Answer

Answer:b  

11.stderr is similar to?

a) stdin

b) stdout

c) Both stdout and stdin

d) None of the mentioned

View Answer

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

View Answer

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 b and c

View Answer

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

View Answer

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

View Answer

Answer:b

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button