This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Diagnostics – 2”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. The following message is displayed in stderr.
Assertion failed: expression, file filenum, line nmn
a) true
b) false
View Answer
Explanation: The message written might be of the form
Assertion failed: expression, file filename, line nmn
This message is displayed on stderr only when the expression in the statement void assert(int expression) is zero.nmn is the line number.
2. The source filename and line number come from the preprocessor macros ________ and ______
a) _ _FILE_ _ and _ _LINE_ _
b) _ _NAME_ _ and _ _NUMBER_ _
c) _ _FILENAME_ _ and _ _NMN_ _
d) _ _FILE_ _ and _ _NUM_ _
View Answer
Explanation: The source filename and line number come from the preprocessor macros
_ _FILE_ _ and _ _LINE_ _. Filename and line number are displayed in the error message when the expression of the assert is zero. It is displayed as
Assertion failed: expression, file filename, line nmn.
3. The function abort() is defined in which of the following header file?
a) stdio.h
b) stdarg.h
c) stdlib.h
d) assert.h
View Answer
Explanation: abort() function is declared inside the header file stdlib.h .This function is used to terminate the program abnormally.
4. Correct code to turn assertions ON at various places throughout a source file is _________
a)
#undef NDEBUG #include <assert.h>
b)
#define NDEBUG #include <assert.h>
c)
#define NDEBUG #include <stdlib.h>
d)
#undef NDEBUG #include <stdlib.h>
Explanation: you can turn assertions on and off at various places throughout a source file, so to turn assertions on, you write:
#undef NDEBUG #include <assert.h>
5. Correct code to turn assertions OFF at various places throughout a source file is _________
a)
#undef NDEBUG #include <assert.h>
b)
#define NDEBUG #include <assert.h>
c)
#define NDEBUG #include <stdlib.h>
d)
#undef NDEBUG #include <stdlib.h>
Explanation: you can turn assertions on and off at various places throughout a source file, so to turn assertions off, you write:
#define NDEBUG #include <assert.h>
6. Which line from the given code is the passive form?
#undef assert #ifdef NDEBUG #define assert (test) ( (void) 0) #else #define assert (test) #endif
a) #define assert(test)
b) #define assert(test) ((void)0)
c) #ifdef NDEBUG
d) #undef assert
View Answer
Explanation: In the given code, the line #define assert (test)((void)0) is in the pasive form.
7. What will be the output of the following C code?
#include <assert.h> #include <stdio.h> #include <stdlib.h> void Assert (char *mesg) { fputs (mesg, stderr); fputs (" -- assertion failed\n" , stderr); abort () ; }
a) prints only assertion message
b) program is just aborted
c) prints assertion message and aborts
d) no action takes place
View Answer
Explanation: The given function uses assert function as defined in assert.h.The program writes assertion message on the stanard error file(stderr) and then calls abort() function to terminate the execution of the program.
8. Which macro can be used to detect and report exceptional conditions?
a) extern
b) edom
c) assert
d) lbdl_min 1e-37
View Answer
Explanation: assert macro is used to detect and report exceptional conditions. It is used to write diagnostics information on standard error file.
9. What will be the output of the following C code?
#include <assert.h> #include <stdio.h> void main() { int n=11; char str[50]="program"; assert(n >= 10); printf(" output: %d\n", n); assert(str != NULL); printf("output: %s\n", str); }
a) output: 11
b) error message
c)
output: 11 output: program
d) output: program
View Answer
Explanation: The condition defined in the macro assert is true hence the statements following it are displayed.
10. What will be the output of the following C code?
#include <assert.h> #include <stdio.h> void main() { int n=12; char str[50]=""; assert(n >= 10); printf(" output: %d\n", n); assert(str != NULL); printf("output: %s\n", str); }
a)
output: 12 output:
b) output: 12
c)
output: 12 assertion error
d) error
View Answer
Explanation: In the given code the first condition in the assert is true, hence the statement following it is displayed. The string is initialized null therefore the second assert condition is false and error message is written on the stderr.
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.
- Check Computer Science Books
- Apply for C Internship
- Watch Advanced C Programming Videos
- Check C Books
- Practice BCA MCQs