Basics of Functions Questions and Answers

C Programming Questions and Answers – Basics of Functions

Here is a listing of C interview questions on “Basics of Functions” along with answers, explanations and/or solutions:

1. What will be the output of the following 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

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

#include <stdio.h>
void m()
{
printf(“hi”);
}
void main()
{
m();
}
a) hi
b) Run time error
c) Nothing
d) Varies

Explanation: None.
3. What will be the output of the following 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

Explanation: None.

4. What will be the output of the following 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

Explanation: None.
5. What will be the output of the following 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

Explanation: None.

6. What will be the output of the following 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

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 whereas standard C compiler doesn’t.
7. What will be the output of the following 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

Explanation: None.
8. What will be the output of the following 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

Explanation: None.

A function is a set of ordered pairs in which each x-element is connected with only ONE y-element. While two y-values cannot be assigned to the same x-value, two x-values can be allocated to the same y-value. There is only ONE y-value for each x-value! We’ll have a function… A function is a type of relationship in which each domain value, or input, corresponds to one range value, or output. y = f ( x ) displaystyle y=fleft(xright) y=fright(xright) y=fleft(xright) y=fright(xright) y=fright(xright) y=fright(xright) y=fright(xright) y=fright(xright) y=fright(xright) y=fright(xright) y=fright (x).

 

Leave a Reply

Your email address will not be published. Required fields are marked *