This section of our 1000+ C# MCQs focuses on preprocessors in C# Programming Language.
1. Choose the symbol which begins a preprocessor directive in C#.NET?
a) #
b) **
c) *
d) &
View Answer
Explanation:
#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
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
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
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
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?
#define pi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
#if (!pi)
Console.WriteLine("i");
#else
Console.WriteLine("pi not define");
#endif
Console.WriteLine("ok");
Console.ReadLine();
}
}
}
a)
i pi not define
b)
pi not define ok
c)
i ok
d) ok
View Answer
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?
#define DEBUG
#define MYTEST
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
#if (DEBUG && !MYTEST)
Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && MYTEST)
Console.WriteLine("MYTEST is defined");
#elif (DEBUG && MYTEST)
Console.WriteLine("DEBUG and MYTEST are defined");
#else
Console.WriteLine("DEBUG and MYTEST are not defined");
#endif
Console.ReadLine();
}
}
}
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
d) DEBUG and MYTEST are defined
View Answer
Explanation: None.
8. What will be the output of the following C# code snippet?
#define DEBUG
#undef DEBUG
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
#if (DEBUG)
Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && MYTEST)
Console.WriteLine("MYTEST is defined");
#elif (DEBUG && MYTEST)
Console.WriteLine("DEBUG and MYTEST are defined");
#else
Console.WriteLine("DEBUG and MYTEST are not defined");
#endif
Console.ReadLine();
}
}
}
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
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
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
Explanation: None.
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 C# Books
- Practice MCA MCQs
- Check MCA Books
- Apply for Computer Science Internship
- Apply for C# Internship