C Programming :: Functions

Home > C Programming > Functions > General Questions

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

1 / 47

 What is the output of the following problem ?

#include
int main()
{
int j,ans;
j = 4;
ans = count(4);
printf("%d\n",ans);
return 0;
}
int count(int i)
{
if ( i < 0)
return(i);
else
return( count(i-2) + count(i-1));
}

A-18

B18

C8

DCompilation Error

ENo output

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

2 / 47

 What is the output of the following problem ?

#include
int main()
{
int j;
for(j=0;j<3;j++)
foo();
return 0;
}
foo() {
static int i = 10;
i+=10;
printf("%d",i);
}

A20 20 20

B20 30 40

C10 10 10

DNone of these

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

3 / 47

 What is the output of the following problem ?

#include

int main() {
int factorial(int n);
int i,ans;
ans = factorial(5);
printf("\nFactorial by recursion = %d\n", ans);
return 0;
}
int factorial(int n)
{
if (n <= 1)
return (1);
else
return ( n * factorial(n-1));
}

A120

B5

C1

DNoneof these

Answer: Option A

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

4 / 47

 What is the output of the following 'C' program?

aaa() {
printf("hi");
}
bbb() {
printf("hello");
}
ccc() {
printf("bye");
}
void main()
{
int (*ptr[3])();
ptr[0]=aaa;
ptr[1]=bbb;
ptr[2]=ccc;
ptr[2]();
}

Ahi

Bbye

Caaa

DCompilation Error

ENone of these

Answer: Option B

Explanation:

ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in effect of writing ccc(), since ptr[2] points to ccc.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

5 / 47

 What is the output of the following 'C' program?

void main(){
int i=3,val;
val=f(i)+ +f(i=1)+ +f(i-1);
printf("%d",val);
}
int f(int num){
return num*5;
}

A20

B30

C21

D31

ECompilation error

Answer: Option A

Explanation:

when f(i) executes first then i=5is there. therefore, f(5)return 15.

next i=1 then f(1) return 5

now f(1-1)=f(0) return 0

adding all : 15+5+0=20

 

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

6 / 47

 void main()
{
printf("%p",main);
}

ACompilation error

BGarbage Value

CSome address will be printed.

DNone of these

Answer: Option C

Explanation:

Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

7 / 47

 int main()
{
show();
return 0;
}
void show()
{
printf("I'm the greatest");
}

ACompier error: Type mismatch in redeclaration of show.

BI', the greatest

CLinker error

DNone of these

Answer: Option B

Explanation:

When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when the compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence there will be a warning, if you have set treat warning as error then option A will be, else option B will be. Here Option B will be correct.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

8 / 47

 #include
int main()
{
int i=_l_abc(10);
printf("%d\n",--i);
return 0;
}
int _l_abc(int i)
{
return(i++);
}

A10

B8

C9

DCompilation error

ENone of these

Answer: Option C

Explanation:

return(i++) it will first return i and then increments. i.e. 10 will be returned.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

9 / 47

 #include
int main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i>2)
goto here;
i++;
}
}
fun()
{
here:
printf("PP");
}

ACompiler error: label 'here' used but not defined

BPP

C1 2 PP

DNone of these

Answer: Option A

Explanation:

Labels have functions scope, in other words the scope of the labels is limited to functions. The label 'here' is available in function fun() Hence it is not visible in function main.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

10 / 47

 #include
int AX = 0;
int main()
{
int i=0;
i = abc();
printf("%d",i);
return 0;
}
abc()
{
AX = 1000;
return AX;
}

A0

B1000

C100

DGarbage Value

ECompilation Error

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace