C Questions and Answers Part-29

1. How is search done in #include and #include “somelibrary.h” according to C standard?
a) When former is used, current directory is searched and when latter is used, standard directory is searched
b) When former is used, standard directory is searched and when latter is used, current directory is searched
c) When former is used, search is done in implementation defined manner and when latter is used, current directory is searched
d) For both, search for ‘somelibrary’ is done in implementation-defined places

Answer: b

2. How is search done in #include and #include”somelibrary.h” normally or conventionally?
a) When former is used, current directory is searched and when latter is used, standard directory is searched
b) When former is used, predefined directory is searched and when latter is used, current directory is searched and then predefined directories are searched
c) When former is used, search is done in implementation defined manner and latter is used to search current directory
d) For both, search for somelibrary is done in implementation-defined manner

Answer: b

3. Can function definition be present in header files?
a) yes
b) no
c) Depends on the compiler
d) Depends on the standard

Answer: a

4. Comment on the output of the following C code.
#include < stdio.h >
#include "test.h"
#include "test.h"
int main()
{
//some code
}
a) true
b) Compile time error
c) false
d) Depends on the compiler

Answer: b

5. What will be the output of the following C code?
#include < stdio.h >
#define foo(m, n) m ## n
void myfunc();
int main()
{
myfunc();
}
void myfunc()
{
printf("%d\n", foo(2, 3));
}
a) 23
b) 2 3
c) Compile time error
d) Undefined behaviour

Answer: a

6. What will be the output of the following C code?
#include < stdio.h >
#define foo(m, n) m ## n
int main()
{
printf("%s\n", foo(k, l));
}
a) k l
b) kl
c) Compile time error
d) Undefined behaviour

Answer: c

7. What will be the output of the following C code?
#include < stdio.h >
#define foo(m, n) " m ## n "
int main()
{
printf("%s\n", foo(k, l));
}
a) k l
b) kl
c) Compile time error
d) m ## n

Answer: d

8. What will be the output of the following C code?
#include < stdio.h >
#define foo(x, y) #x #y
int main()
{
printf("%s\n", foo(k, l));
return 0;
}
a) kl
b) k l
c) xy
d) Compile time error

Answer: a

9. What will be the output of the following C code?
#include < stdio.h >
#define foo(x, y) x / y + x
int main()
{
int i = -6, j = 3;
printf("%d\n",foo(i + j, 3));
return 0;
}
a) Divided by zero exception
b) Compile time error
c) -8
d) -4

Answer: c

10. What will be the output of the following C code?
#include < stdio.h >
void f();
int main()
{
#define foo(x, y) x / y + x
f();
}
void f()
{
printf("%d\n", foo(-3, 3));
}
a) -8
b) -4
c) Compile time error
d) Undefined behaviour

Answer: b