Here is a listing of C Objective Questions on “Basics of Functions” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include -
int main()
-
{ -
void foo();
-
printf("1 ");
-
foo();
-
} -
void foo()
-
{ -
printf("2 ");
-
}
a) 1 2
b) Compile time error
c) 1 2 1 2
d) Depends on the compiler
Answer: a
Clarification: None.
2. What will be the output of the following C code?
-
#include -
int main()
-
{ -
void foo(), f();
-
f();
-
} -
void foo()
-
{ -
printf("2 ");
-
} -
void f()
-
{ -
printf("1 ");
-
foo();
-
}
a) Compile time error as foo is local to main
b) 1 2
c) 2 1
d) Compile time error due to declaration of functions inside main
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include -
int main()
-
{ -
void foo();
-
void f()
-
{ -
foo();
-
} -
f();
-
} -
void foo()
-
{ -
printf("2 ");
-
}
a) 2 2
b) 2
c) Compile time error
d) Depends on the compiler
Answer: d
Clarification: Even though the answer is 2, this code will compile fine only with gcc. GNU C supports nesting of functions in C as a language extension whereas standard C compiler doesn’t.
4. What will be the output of the following C code?
-
#include -
void foo();
-
int main()
-
{ -
void foo();
-
foo();
-
return 0;
-
} -
void foo()
-
{ -
printf("2 ");
-
}
a) Compile time error
b) 2
c) Depends on the compiler
d) Depends on the standard
Answer: b
Clarification: None.
5. What will be the output of the following C code?
-
#include -
void foo();
-
int main()
-
{ -
void foo(int);
-
foo(1);
-
return 0;
-
} -
void foo(int i)
-
{ -
printf("2 ");
-
}
a) 2
b) Compile time error
c) Depends on the compiler
d) Depends on the standard
Answer: a
Clarification: None.
6. What will be the output of the following C code?
-
#include -
void foo();
-
int main()
-
{ -
void foo(int);
-
foo();
-
return 0;
-
} -
void foo()
-
{ -
printf("2 ");
-
}
a) 2
b) Compile time error
c) Depends on the compiler
d) Depends on the standard
Answer: b
Clarification: None.
7. What will be the output of the following C code?
-
#include -
void m()
-
{ -
printf("hi");
-
} -
void main()
-
{ -
m();
-
}
a) hi
b) Run time error
c) Nothing
d) Varies
Answer: a
Clarification: None.
8. What will be the output of the following C code?
-
#include -
void m();
-
void n()
-
{ -
m();
-
} -
void main()
-
{ -
void m()
-
{ -
printf("hi");
-
} -
}
a) hi
b) Compile time error
c) Nothing
d) Varies
Answer: b
Clarification: None.
