C Questions and Answers – Inline

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Inline”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. Name the function whose definition can be substituted at a place where its function call is made _________
a) friends function
b) inline function
c) volatile function
d) external function
View Answer

Answer: b
Explanation: The inline function, whose definitions being small can be substituted at a place where its function call is made. They are inlined with their function calls.

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

advertisement
advertisement
#include <stdio.h>
void inline func1(int a, int b) 
{
    printf ("a=%d and b=%d\n", a, b);
}
int inline func2(int x)
{
    return x*x;
}
int main()
{
    int tmp;
    func1(1,4);
    tmp = func2(6);
    printf("square val=%d\n", tmp);
    return 0;
}

a)

a=1 and b=4
square val = 36
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

b) a=4 and b=1
c) error
d) square val = 36
View Answer

Answer: a
Explanation: The code shown above is an example of inline function. Both functions 1 and 2 are inline functions. Hence the output will be as shown in option

a=1 and b=4
square val = 36

.

advertisement

3. What will be the error (if any) in the following C code?

#include <stdio.h>
void inline func1(float b) 
{
    printf ("%lf\n",b*2);
}
int main() 
{
     inline func1(2.2);
     return 0;
}

a) No error
b) Error in statement: void inline func1(float b)
c) Error in statement: printf(“%lf\n”,b*2);
d) Error in statement: inline func1(2.2);
View Answer

Answer: d
Explanation: The keyword inline is used only with the function definition. In the code shown above it has been used with the function call. Hence there is an error in the statement: inline func1(2.2);
advertisement

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

#include <stdio.h>
void inline f1(char b) 
{
    printf ("%d\n",b);
}
int main() 
{
    f1('a');
    return 0;
}

a) a
b) 65
c) error
d) 97
View Answer

Answer: d
Explanation: The definition of the function f1 is replaced with the function call. Since the format specifier used is %d, the ASCII value of the said character is printed. Hence the output of the code shown above is 97 (ASCII value of the alphabet ‘a’).

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

#include <stdio.h>
void inline func1(char b[10]) 
{
    printf ("%c\n",b[2]);
}
int main() 
{
     func1("sanfoundry");
     return 0;
}

a) s
b) n
c) a
d) error
View Answer

Answer: b
Explanation: In the code shown above, the definition of function func1 is replaced with its function call. The function prints the alphabet at the second index of the given string. Hence the output of this code is ‘n’.

6. The following C code results in an error. State whether this statement is true or false.

#include <stdio.h>
void f(double b) 
{
    printf ("%ld\n",b);
}
int main() 
{
     inline f(100.56);
     return 0;
}

a) True
b) False
View Answer

Answer: a
Explanation: The above code will result in an error because we have used the keyword inline in the function call. Had the keyword inline been used in the function definition, the code would not have thrown an error.

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

#include<stdio.h>
static inline int max(int a, int b) 
{
  return a > b ? a : b;
}
main()
{
    int m;
    m=max(-6,-5);
    printf("%d",m);
}

a) -6
b) -5
c) Junk value
d) Error
View Answer

Answer: b
Explanation: The code shown a replaces the definition of a function max with it’s function call. This function is used to print the bigger of the two arguments. Hence -5 is printed.

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

#include<stdio.h>
#define inline
inline f(char a)
    {
        #ifdef inline
        printf("%c",a);
        #endif 
    }
main()
{
    f('a');
}

a) Error
b) a
c) No error but nothing will be printed as output
d) 97
View Answer

Answer: b
Explanation: In the code shown above, we have defined identifier names inline using the macro #define. The output will be printed only if inline is defined. Since it is defined, ‘a’ is printed as output.

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

#include<stdio.h>
extern inline int min(int a, int b) 
{
  return a < b ? a : b;
}
main()
{
    int m;
    m=min(3,-5);
    printf("%d",m);
}

a) Error
b) 3
c) -5
d) Junk value
View Answer

Answer: a
Explanation: The above code will result in an error due to the statement: extern inline int min(int a, int b). The keyword ‘extern’ causes an error: undefined reference to min.

10. To have GCC inline the given function regardless of the level of optimization, we must declare the function with the attribute _________________
a) optimize_inline
b) packed_inline
c) always_inline
d) level_inline
View Answer

Answer: b
Explanation: To have GCC inline the given function regardless of the level of optimization, we declare the function with the attribute always_inline.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.