MCQs on Preprocessor Directives | C Programming

Macros and #define

  1. What is the primary purpose of the #define directive in C?
    • A) To define a constant
    • B) To define a function
    • C) To include a header file
    • D) To conditionally compile code
  2. Which of the following is the correct syntax for defining a macro?
    • A) #define MAX 100
    • B) define MAX = 100
    • C) #macro MAX 100
    • D) macro #define MAX 100
  3. In C, what does the #define directive do at compile-time?
    • A) Defines a new variable
    • B) Replaces code with the defined macro
    • C) Includes a library
    • D) Initializes memory
  4. Which of the following is true about #define?
    • A) It creates a constant value that can be modified
    • B) It substitutes the value at compile time
    • C) It is only used for functions
    • D) It can be used for dynamic memory allocation
  5. What will happen if you define a macro with the same name multiple times?
    • A) Compilation will fail
    • B) The last definition is taken into account
    • C) The first definition is used
    • D) It will throw a runtime error

Conditional Compilation (#ifdef, #ifndef, #endif)

  1. What does the #ifdef preprocessor directive check for?
    • A) If the macro is not defined
    • B) If a macro is defined
    • C) If a function is defined
    • D) If a header file exists
  2. What is the purpose of #ifndef in conditional compilation?
    • A) To check if a macro is defined
    • B) To check if a macro is not defined
    • C) To ignore a file
    • D) To include a file
  3. Which of the following is a valid use of the #endif directive?
    • A) To end a conditional compilation block
    • B) To define a macro
    • C) To include a header file
    • D) To terminate a function
  4. Which directive is used to define code that should be conditionally compiled if a macro is not defined?
    • A) #if
    • B) #ifdef
    • C) #ifndef
    • D) #else
  5. If a macro is not defined, the following code block will be compiled:cCopy code#ifndef MY_MACRO // code block #endif
    • A) True
    • B) False

File Inclusion (#include)

  1. What is the purpose of the #include directive in C?
  • A) To define a macro
  • B) To include the contents of a file
  • C) To compile a file
  • D) To create a function
  1. Which of the following will include a standard library in C?
  • A) #include <stdio.h>
  • B) #include "myfile.h"
  • C) #include stdlib
  • D) #include library.h
  1. When using #include, what is the difference between #include <file.h> and #include "file.h"?
  • A) The first searches for the file in system directories; the second searches in the current directory
  • B) The first searches for the file in the current directory; the second searches in system directories
  • C) Both are the same
  • D) Both include the file at runtime
  1. Which of the following is the correct syntax to include a user-defined header file?
  • A) #include <userfile.h>
  • B) #include "userfile.h"
  • C) #include {userfile.h}
  • D) #include [userfile.h]
  1. What happens if a file is included multiple times?
  • A) It will result in a compilation error
  • B) The compiler will ignore subsequent inclusions
  • C) It will cause a runtime error
  • D) It will be included multiple times

Macro Functions

  1. What is a macro function in C?
  • A) A function defined with the #define directive
  • B) A function with a fixed value
  • C) A regular function with a return type
  • D) A function declared inside a header file
  1. Which of the following is the correct syntax for defining a macro function?
  • A) #define square(x) x*x
  • B) #define square(x) (x*x)
  • C) macro square(x) x*x
  • D) function square(x) x*x
  1. In macro functions, why is it important to use parentheses around parameters?
  • A) To ensure proper evaluation of expressions
  • B) To avoid compile-time errors
  • C) To handle multiple arguments
  • D) To increase performance
  1. What is the potential drawback of using macro functions instead of regular functions?
  • A) They cannot be debugged
  • B) They may lead to unintended side effects
  • C) They are slower than regular functions
  • D) They cannot take parameters
  1. Which of the following is true about a macro function?
  • A) It operates at runtime
  • B) It works as a preprocessor directive
  • C) It always returns a value
  • D) It can be dynamically allocated

Predefined Macros and Operators (#, ##)

  1. What does the # operator do in a macro definition?
  • A) Concatenates two tokens
  • B) Converts a macro argument into a string
  • C) Adds two values
  • D) Defines a constant
  1. What does the ## operator do in a macro definition?
  • A) Replaces all arguments with values
  • B) Concatenates two tokens into one
  • C) Converts a value into a string
  • D) Checks if a macro is defined
  1. Which predefined macro can be used to get the current line number in C?
  • A) __LINE__
  • B) __DATE__
  • C) __TIME__
  • D) __FILE__
  1. Which predefined macro returns the current date in C?
  • A) __DATE__
  • B) __TIME__
  • C) __LINE__
  • D) __FILE__
  1. Which predefined macro is used to get the current file name in C?
  • A) __FILE__
  • B) __DATE__
  • C) __LINE__
  • D) __TIME__
  1. What does the following code do?cCopy code#define CONCAT(x, y) x ## y
  • A) Concatenates two strings
  • B) Concatenates two variables or tokens
  • C) Defines a function
  • D) Adds two numbers
  1. What is the result of the following code?cCopy code#define PRINT(x) printf(#x) PRINT(Hello);
  • A) It will print “Hello”
  • B) It will print Hello
  • C) It will cause a syntax error
  • D) It will cause a compilation error
  1. Which of the following is a valid use of the # operator?
  • A) #define MAX (5)
  • B) #define SQUARE(x) x #x
  • C) #define stringify(x) #x
  • D) #define CONCAT(x, y) x ## y
  1. How would you use the ## operator to join two variables num and 1 into num1?
  • A) #define JOIN(num, 1) num ## 1
  • B) #define JOIN(num, 1) num1
  • C) #define JOIN(num, 1) num & 1
  • D) #define JOIN(num, 1) num ## 1
  1. Which predefined macro returns the current time of compilation?
  • A) __TIME__
  • B) __DATE__
  • C) __LINE__
  • D) __FILE__

Answers (Tabular Form)

QuestionAnswer
1A
2A
3B
4B
5B
6B
7B
8A
9C
10A
11B
12A
13A
14B
15B
16A
17B
18A
19B
20B
21B
22B
23A
24A
25A
26B
27A
28C
29A
30A

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top