C Questions and Answers – Token Concatenation

«
»

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.

advertisement

1. Which of the following operators is used to concatenate two strings without space?
a) #
b) < >
c) **
d) ##
View Answer

Answer: d
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?

advertisement
advertisement
#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

Answer: b
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
advertisement

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

Answer: d
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.
advertisement

4. What will be the output of the following C code?

advertisement
#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,5);
}

a) 4356
b) 3456
c) 4365
d) 3465
View Answer

Answer: a
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

Answer: b
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

Answer: d
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

Answer: c
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

Answer: d
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

Answer: d
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=10
View Answer
Answer: a
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.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.