C# Questions & Answers – Fundamentals of Preprocessors

«
»

This section of our 1000+ C# MCQs focuses on fundamentals of preprocessors in C# Programming Language.

1. Choose the symbol which begins a preprocessor directive in C#.NET?
a) #
b) **
c) *
d) &
View Answer

Answer: a
Explanation:

advertisement
 #define, #elif, #else etc.

2. What is meant by preprocessor directive in C#.NET?
a) a form of command which are interpreted by the compiler
b) a form of macros like in c and c++ not exactly same to them, separately designed for C#.NET
c) always begins with a ‘#’ character occupies separate line of source of code
d) all of the mentioned
View Answer

Answer: d
Explanation: Preprocessor directives are commands that are interpreted by the compiler and affect the output or behavior of the build process. The C# compiler does not have a separate preprocessor, like C and C++ we cannot use these directives to create macros. Preprocessing directives are top lines in our program that start with ‘#’. The ‘#’ is followed by an identifier that is the directive name.

3. What is meant by preprocessor directive #define?
a) defines a character sequence
b) helps in determining existence and non existence of a symbol
c) can be used to create function like macros as in C/C++
d) all of the mentioned
View Answer

Answer: a
Explanation: The #define directive defines a character sequence called a symbol. The existence or nonexistence of a symbol can be determined by #if or #elif and is used to control compilation. #define which supports creation of function like macros in c/c++ does not support the same in C#.

4. Select the defined preprocessor in C#.NET?
a) #define
b) #elif
c) #else
d) All of the mentioned
View Answer

Answer: d
Explanation: None.

5. What does preprocessor directive #if and #endif explains?
a) Enables compilation of sequence of code on condition basis
b) Express results into true or false on evaluation of condition
c) If expression following #if is true then code that is between #if and #endif is compiled otherwise skipped
d) All of the mentioned
View Answer

Answer: d
Explanation: The #if and #endif directives enable conditional compilation of a sequence of code based upon whether an expression involving one or more symbols evaluates to true. A symbol is true if it has been defined. It is false otherwise. If the expression following #if is true, the code that is between it and #endif is compiled. Otherwise, the intervening code is skipped. The #endif directive marks the end of an #if block.

6. What will be the output of the following C# code snippet?

  1.  #define pi 
  2.  using System;
  3.  using System.Collections.Generic;
  4.  using System.Linq;
  5.  using System.Text;
  6.  using System.Threading.Tasks;
  7.  
  8.  namespace ConsoleApplication13
  9. {
  10.     class Program
  11.     {   
  12.         static void Main(string[] args)
  13.         {
  14.             #if (!pi) 
  15.             Console.WriteLine("i");
  16.             #else 
  17.             Console.WriteLine("pi not define");
  18.             #endif
  19.             Console.WriteLine("ok");
  20.             Console.ReadLine();
  21.        }
  22.    }
  23. }

a)

i
pi not define
advertisement

b)

pi not define
ok

c)

i
ok

d) ok
View Answer

Answer: b
Explanation: The defined symbol ‘pi’ when compared as per ‘if’ condition, hence the outcome is false which results in skip of statement and hence executes statement after #else and finally the end statement after #endif.
Output: pi not define
ok

7. What will be the output of the following C# code snippet?

  1. #define DEBUG 
  2. #define MYTEST
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApplication13
  10. {
  11.     class Program
  12.     {   
  13.         static void Main(string[] args)
  14.         {
  15.             #if (DEBUG && !MYTEST)
  16.             Console.WriteLine("DEBUG is defined");
  17.             #elif (!DEBUG && MYTEST)
  18.             Console.WriteLine("MYTEST is defined");
  19.             #elif (DEBUG && MYTEST)
  20.             Console.WriteLine("DEBUG and MYTEST are defined");
  21.             #else
  22.             Console.WriteLine("DEBUG and MYTEST are not defined");
  23.             #endif
  24.             Console.ReadLine();
  25.         }
  26.     }
  27. }

a)

DEBUG is defined
MYTEST is defined

b)

MYTEST is defined
DEBUG and MYTEST are defined

c)

DEBUG and MYTEST are not defined
MYTEST is defined
advertisement

d) DEBUG and MYTEST are defined
View Answer

Answer: d
Explanation: None.

8. What will be the output of the following C# code snippet?

  1. #define DEBUG 
  2. #undef DEBUG
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApplication13
  10. {
  11.     class Program
  12.     {   
  13.         static void Main(string[] args)
  14.         {
  15.             #if (DEBUG)
  16.             Console.WriteLine("DEBUG is defined");
  17.             #elif (!DEBUG && MYTEST)
  18.             Console.WriteLine("MYTEST is defined");
  19.             #elif (DEBUG && MYTEST)
  20.             Console.WriteLine("DEBUG and MYTEST are defined");
  21.             #else
  22.             Console.WriteLine("DEBUG and MYTEST are not defined");
  23.             #endif
  24.             Console.ReadLine();
  25.        }
  26.    }
  27. }

a)

DEBUG is defined
DEBUG and MYTEST are not defined

b) DEBUG and MYTEST are not defined
c)

MYTEST is defined
DEBUG and MYTEST are not defined

d) DEBUG is defined
View Answer

Answer: b
Explanation: #undef lets to undefine a symbol such that by using the symbol as the expression in a #if directive, the expression will evaluate to false i.e the symbol will be undefined in nature.
Output: DEBUG and MYTEST are not defined

9. Which preprocessor directive among the following forces the compiler to stop the compilation?
a) #warning
b) #endregion
c) #undef
d) #error
View Answer

Answer: d
Explanation: The #error directive forces the compiler to stop compilation. It is used for debugging. The general form of the #error directive is #error error-message. When the #error directive is encountered, the error message is displayed.

10. Which among the following is not a preprocessor directive?
a) #ifdef
b) #pragma
c) #Or
d) #undef
View Answer

Answer: c
Explanation: None.

Sanfoundry Global Education & Learning Series – C# Programming Language.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn