This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Token Concatenation”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. Which of the following operators is used to concatenate two strings without space?
a) #
b) < >
c) **
d) ##
View Answer
Explanation: The operator ## is used for token concatenation (to concatenate two strings without space).
2. What will be the output of the following C code?
#include <stdio.h> #define p( n ) printf( "t" #n " = %d", t##n ) int t3=10; int main() { p(3); }
a) t=10
b) t3=10
c) t10=3
d) t=3
View Answer
Explanation: The code shown above uses the ## operator to concatenate ‘t’ and 3. t3 has been declared as 10 in the code, Hence the output of the code shown above is: t3=10
3. What will be the output of the following C code?
#include <stdio.h> #define p( n,m ) printf( "%d", m##n ) int main() { p(3,4); }
a) Error
b) Junk value
c) 34
d) 43
View Answer
Explanation: In the code shown above, we have concatenated the two tokens in the order: mn. The value of m is equal to 4 and that of n is equal to 3. Hence the output of this code is 43.
4. What will be the output of the following C code?
#include <stdio.h> #define p( n,m ) printf( "%d", m##n ) #define q(a,b) printf("%d",a##b) main() { p(3,4); q(5,6); }
a) 4356
b) 3456
c) 4365
d) 3465
View Answer
Explanation: In the code shown above we have concatenated m and n in one statement and a and b in another. Since we have not used a new line character, the output of this code will be equal to 4356.
5. The following C code results in an error.
#include <stdio.h> #define world( n ) printf( "t^^" #n" = %c", t##n ) int t3=1; int main() { world(3); }
a) True
b) False
View Answer
Explanation: The code shown above does not result in an error. The output of this code is t^^3=junk value.
6. What will be the output of the following C code?
#include <stdio.h> #define display( n ) printf( "a" #n " = %d", a##n ) int main() { display(3); }
a) a3
b) 31
c) a 3
d) error
View Answer
Explanation: The code shown above results in an error because we have not explicitly declared a3. Had we declared a3 in this code, it would not have thrown an error.
7. What will be the output of the following C code?
#include <stdio.h> #define hello( n ) a##n int a3; int main() { int x; x=hello(3); if(x!=0) printf("hi"); else printf("good"); }
a) error
b) a3
c) good
d) hi
View Answer
Explanation: The code shown will print ‘hi’ if x is not equal to zero and ‘good’ if x is equal to zero. In the above code, we have declared x (equal to zero). Hence ‘good’ is printed as output.
8. What will be the output of the following C code?
#include <stdio.h> #define hello( n ) printf( "a" #n "= %d", a##n ) int a3=3; int main() { #ifdef a3 hello(3); #else printf("sorry"); #endif }
a) a3=3
b) error
c) a=3
d) sorry
View Answer
Explanation: The code shown above prints a3=3 if as is defined. Since a3 is not defines, ‘sorry’ is printed as output.
9. What will be the output of the following C code?
#include <stdio.h> #define p( n ) printf( "t*" #n " = %s", t##n ) char tsan[]="tsan"; int main() { int x; x=p(san); }
a) error
b) tsan=tsan
c) t*san=t*san
d) t*san=tsan
View Answer
Explanation: The code shown above uses the ## operator to concatenate the tokens t* and san. We have decalred tsan[]=tsan. Hence the output of the code shown will be: t*san=tsan.
10. What will be the output of the following C code?
#include <stdio.h> #define p( n ) printf( "t%%\n" #n " = %d", t##n ) int t3=10; int main() { int x; x=p(3); }
a)
t% 3=10
b) t3=10
c) t%3=10
d)
t %3=10View Answer
Explanation: In this code, operator ## is used to concatenate t and 3. However, a new line character is a part of this statement. The variable t3 has been declared as 10. Hence the output of this code will be:
t% 3=10
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.
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Apply for Computer Science Internship
- Check C Books
- Practice BCA MCQs