This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What is the value of y after executing the following code?
#define square(p) p*p
void main()
{
int y=square(2+3);
printf("%d",y);
}
Option C
In this program, square(p) is defined as a macro. A macro in C works by simple text substitution, meaning the compiler directly replaces the macro with its definition without adding any extra brackets.
When the statement square(2+3) is written, it is replaced by 2+3*2+3. According to operator precedence rules, multiplication has higher priority than addition, so 3*2 is evaluated first. This makes the expression 2+6+3, which results in the value 11.
Q: 2Which character symbol is required as first non-whitespace character to initiate a preprocessor directive statement in C?
Option C
In C, a preprocessor directive is an instruction processed by the C preprocessor before the actual compilation of the program. A preprocessor directive begins with the hash (#) symbol as the first non-whitespace character on a line.
#include <stdio.h>
#define MAX 100
int main()
{
printf("%d", MAX);
return 0;
}
Here, #include and #define are preprocessor directives. The # symbol tells the preprocessor that the line contains a directive.
The phrase "first non-whitespace character" is important. Spaces or tabs may appear before #, but the first actual character must be #.
| Directive | Purpose |
|---|---|
| #include | Includes a header file. |
| #define | Defines a macro. |
| #if | Conditional compilation. |
| #ifdef | Checks whether a macro is defined. |
| #ifndef | Checks whether a macro is not defined. |
| #undef | Undefines a macro. |
Q: 3What is the output of the C code fragment shown below?
#define n 6-4
void main()
{
int i;
i=n*n;
printf(“%d”,i);
}
Option B
In C, macros are expanded textually before compilation. Here, #define n 6-4 means every occurrence of n is replaced by 6-4 without parentheses. So the expression, i = n * n; becomes i = 6-4*6-4;
Now applying operator precedence, the multiplication before subtraction and the expression becomes 6-24-4 = -22
Q: 4What is the output of the following C program?
#include<stdio.h>
#define SQR(x) (x*x)
int main()
{
int a;
int b=4;
a=SQR(b+2);
printf(“%d”,a);
return 0;
}
Option A
In this C program, the macro SQR(x) is defined as (x*x). When a = SQR(b+2) executes with b=4, the preprocessor performs textual substitution, expanding it to a=(b+2*b+2). Following operator precedence, this evaluates as (4+2*4+2) = (4+8+2) = 14, which is then printed.
Q: 5Which of the following statement(s) is/are true about pre-processor in C/C++?
I. To replace each #include directive with the contents of the designated file.
II. To replace each escape sequence with designated character.
III. To process macro definitions and expand macro calls.
Option A
The preprocessor in C/C++ is a program that processes the source code before it is passed to the compiler. It mainly handles preprocessor directives, which begin with the # symbol. Important examples include #include, #define, #if, #ifdef, and #undef.
Statement : I (True)
The #include directive is handled by the preprocessor. It causes the contents of the specified header file to be included at that location before the actual compilation of the source code.
Statement : II (False)
Escape sequences such as \n, \t, \\, and \" are not processed by the preprocessor.
Statement : III (True)
Macro processing and macro expansion are important functions of the preprocessor.
E.g.:
#define MAX 100
int a=MAX;
The preprocessor expands MAX according to its definition, so conceptually the compiler receives int a=100;
It also processes function-like macros:
#define SQUARE(a) ((a)*(a))
int p=SQUARE(5);
Q: 6Which keyword is used to define the macros in C/C++?
Option C
In C/C++, macros are defined using the preprocessor directive #define. Preprocessor directives are instructions that are processed before the actual compilation starts.
E.g.:
Here, PI is a macro for a constant value while SQUARE(a) is a macro with arguments.
Q: 7The preprocessors in C are defined with ________________ character.
Option B
In C programming, Preprocessor Directives are lines included in the code that are processed before compilation. These directives always begin with the # (Hash) symbol. Examples of commonly used preprocessor directives starting with # include, #define, #ifdef, #ifndef etc.
| PREPROCESSOR DIRECTIVE | DESCRIPTION |
|---|---|
| #include | Includes standard or user-defined header files in the program. |
| #define | Defines a macro or symbolic constant. |
| #undef | Undefines a previously defined macro. |
| #ifdef | Checks if a macro is defined, includes code if true. |
| #ifndef | Checks if a macro is not defined, includes code if true. |
| #if | Tests a compile-time condition, includes code if true. |
| #else | Specifies alternative code if #if or #ifdef condition is false. |
| #elif | Combines else and if in conditional compilation. |
| #endif | Ends the conditional directive started by #if, #ifdef, #ifndef. |
| #error | Generates a compilation error with a message. |
| #pragma | Provides special instructions to the compiler. |
| #line | Changes the reported line number and filename for errors. |
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.