C Programming Questions and Answers – Basics of Functions
C Programming Questions and Answers – Basics of Functions
1.What is the output of this C code?
#include <stdio.h>
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
View Answer
2.What is the output of this C code?
#include <stdio.h>
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
View Answer
3.What is the output of this C code?
#include <stdio.h>
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
View Answer
Explanation: 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 where as standard C compiler doesn’t.
4.What is the output of this C code?
#include <stdio.h>
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
View Answer
5.What is the output of this C code?
#include <stdio.h>
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
View Answer
6.What is the output of this C code?
#include <stdio.h>
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
View Answer
7.What is the output of this C code?
include <stdio.h>
void m()
{
printf(“hi”);
}
void main()
{
m();
}
a) hi
b) Run time error
c) Nothing
d) Varies
View Answer
8.What is the output of this C code?
#include <stdio.h>
void m();
void n()
{
m();
}
void main()
{
void m()
{
printf(“hi”);
}
}
a) hi
b) Compile time error
c) Nothing
d) Varies
View Answer
9.What is the output of this C code?
#include <stdio.h>
void main()
{
m();
void m()
{
printf(“hi”);
}
}
a) hi
b) Compile time error
c) Nothing
d) Varies
View Answer
10.What is the output of this C code?
#include <stdio.h>
void main()
{
m();
}
void m()
{
printf(“hi”);
m();
}
a) Compile time error
b) hi
c) Infinite hi
d) Nothing
View Answer
11.What is the output of this C code?
#include <stdio.h>
void main()
{
static int x = 3;
x++;
if (x <= 5)
{
printf(“hi”);
main();
}
}
a) Run time error
b) hi
c) Infinite hi
d) hi hi
View Answer
12.Which of the following is a correct format for declaration of function?
a) return-type function-name(argument type);
b) return-type function-name(argument type) {}
c) return-type (argument type)function-name;
d) Both (a) and (b)
View Answer
13.Which of the following function declaration is illegal?
a) int 1bhk(int);
b) int 1bhk(int a);
c) int 2bhk(int*, int []);
d) All of the mentioned
View Answer
14.Which function definition will run correctly?
a) int sum(int a, int b)
return (a + b);
b) int sum(int a, int b)
{return (a + b);}
c) int sum(a, b)
return (a + b);
d) Both (a) and (b)
View Answer
15.Can we use a function as a parameter of another function? [ Eg: void wow(int func()) ] a) Yes, and we can use the function value conveniently
b) Yes, but we call the function again to get the value, not as convenient as in using variable
c) No, C does not support it.
d) This case is compiler dependent
View Answer
16.The value obtained in the function is given back to main by using ________ keyword?
a) return
b) static
c) new
d) volatile