
The preprocessor, bitwise operators and file handling are topics that many C learners skip - and exactly the ones that show up in tricky interview and GATE-style questions. This third C set covers macro pitfalls, include guards, token pasting, bit tricks and file I/O with fopen, fgetc, fseek and ftell. Assume stdio.h is included and int is 32 bits.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Q1. What is printed?
#define SQ(x) x * x
printf("%d", SQ(2 + 3));
a) 25
b) 11
c) 10
d) 13
Q2. When are #define and #include directives processed?
a) At run time
b) By the preprocessor, before actual compilation
c) By the linker
d) By the loader
Q3. What is the purpose of include guards (#ifndef / #define / #endif) in a header file?
a) To speed up the program at run time
b) To prevent the same header from being included more than once in a translation unit
c) To hide the header from other files
d) To make all functions in the header static
Q4. In a macro, what does the ## operator do?
a) Converts an argument into a string literal
b) Pastes two tokens together into one token
c) Comments out the rest of the line
d) Computes a hash
Q5. Which predefined macro expands to the current source line number?
a) __FILE__
b) __LINE__
c) __DATE__
d) __func__
Q6. What is printed?
printf("%d", 5 & 3);
a) 1
b) 7
c) 6
d) 8
Q7. What is printed?
printf("%d", 5 ^ 3);
a) 1
b) 7
c) 6
d) 2
Q8. What is the value of 1 << 4 ?
a) 4
b) 5
c) 8
d) 16
Q9. Which expression is true only when the positive integer n is a power of two?
a) n & 1
b) (n & (n - 1)) == 0 && n != 0
c) n % 2 == 0
d) n | (n - 1)
Q10. For a signed int on a two's complement system, what is the value of ~0 ?
a) 0
b) 1
c) -1
d) INT_MAX
Q11. What is printed?
int n = 12;
printf("%d", n & (n - 1));
a) 11
b) 8
c) 4
d) 12
Q12. What does fopen("log.txt", "a") do?
a) Opens the file for reading only
b) Opens for writing at the end of the file, creating it if it does not exist
c) Truncates the file and writes from the start
d) Fails if the file does not exist
Q13. What does fopen return if the file cannot be opened?
a) 0 as an int
b) EOF
c) NULL
d) -1
Q14. Why should the result of fgetc be stored in an int rather than a char?
a) int is faster
b) So that EOF (a negative int) can be distinguished from every valid character value
c) fgetc returns a pointer
d) char cannot hold letters
Q15. After fseek(fp, 0, SEEK_END); what does ftell(fp) return for a file opened in binary mode?
a) 0
b) The size of the file in bytes
c) The number of lines in the file
d) -1 always
Recommended MCQs
C Programming MCQs - Pointers and Dynamic Memory (Set 1)
C Programming MCQs - Arrays, Strings, Structures and Storage Classes (Set 2)
Comments
Post a Comment