C Multiple Choice Questions & Answers (MCQs) on “Token Concatenation”.
1. Which of the following operators is used to concatenate two strings without space?
a) #
b) < >
c) **
d) ##
Answer: d
Clarification: 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#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
Answer: b
Clarification: 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#define p( n,m ) printf( "%d", m##n ) int main() { p(3,4); }
a) Error
b) Junk value
c) 34
d) 43
Answer: d
Clarification: 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#define p( n,m ) printf( "%d", m##n ) #define q(a,b) printf("%d",a##b) main() { p(3,4); q(5,5); }
a) 4356
b) 3456
c) 4365
d) 3465
Answer: a
Clarification: 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#define world( n ) printf( "t^^" #n" = %c", t##n ) int t3=1; int main() { world(3); }
a) True
b) False
Answer: b
Clarification: 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#define display( n ) printf( "a" #n " = %d", a##n ) int main() { display(3); }
a) a3
b) 31
c) a 3
d) error
Answer: d
Clarification: 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#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
Answer: c
Clarification: 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#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
Answer: d
Clarification: 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#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
Answer: d
Clarification: 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#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=10
View Answer
Answer: a
Clarification: 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