250+ TOP MCQs on Diagnostics and Answers

C Multiple Choice Questions & Answers on “Diagnostics – 2”.

1. The following message is displayed in stderr.
Assertion failed: expression, file filenum, line nmn
a) true
b) false
Answer: a
Clarification: 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_ _
Answer: a
Clarification: 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
Answer: c
Clarification: 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 

b)

#define NDEBUG 
#include 

c)

#define NDEBUG 
#include 

d)

#undef NDEBUG 
#include 

View Answer

Answer: a
Clarification: you can turn assertions on and off at various places throughout a source file, so to turn assertions on, you write:

#undef NDEBUG 
#include 

 
 

5. Correct code to turn assertions OFF at various places throughout a source file is _________
a)

#undef NDEBUG 
#include 

b)

#define NDEBUG 
#include 

c)

#define NDEBUG 
#include 

d)

#undef NDEBUG 
#include 

View Answer

Answer: b
Clarification: you can turn assertions on and off at various places throughout a source file, so to turn assertions off, you write:

#define NDEBUG 
#include 

 
 

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
Answer: b
Clarification: 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  
#include 
 #include  
void -Assert (char *mesg) 
{
   fputs (mesg, stderr); 
   fputs (" -- assertion failedn" , stderr); 
   abort () ; 
}

a) prints only assertion message
b) program is just aborted
c) prints assertion message and aborts
d) no action takes place
Answer: c
Clarification: 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
Answer: c
Clarification: 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  
#include   
void main() 
{ 
   int n=11; 
   char str[50]="program";
   assert(n >= 10); 
   printf(" output: %dn", n); 
   assert(str != NULL); 
   printf("output: %sn", str); 
}

a) output: 11
b) error message
c)

output: 11
output: program

d) output: program
Answer: c
Clarification: 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  
#include   
void main() 
{ 
   int n=12; 
   char str[50]="";
   assert(n >= 10); 
   printf(" output: %dn", n); 
   assert(str != NULL); 
   printf("output: %sn", str); 
}

a)

   output: 12
   output: 

b) output: 12
c)

  output: 12
  assertion error

d) error
Answer: c
Clarification: 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.

Leave a Reply

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