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: 2What 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: 3What 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: 4Which 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: 5The 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.