C Questions and Answers – Diagnostics – 2

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

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

Answer: a
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.
advertisement
advertisement

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

Answer: c
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  
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

b)

#define NDEBUG 
#include   

c)

#define NDEBUG 
#include  
advertisement

d)

#undef NDEBUG 
#include  
View Answer
Answer: a
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  
 
 

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

#undef NDEBUG 
#include  
advertisement

b)

#define NDEBUG 
#include   

c)

#define NDEBUG 
#include  

d)

#undef NDEBUG 
#include  
View Answer
Answer: b
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   
 
 

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

Answer: b
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

Answer: c
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

Answer: c
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

Answer: c
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

Answer: c
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.

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.