Exercise: Python

Questions for: Functions

Is it true that too many recursive calls may result into stack overflow?
A:
Yes
B:
No
C:
D:
Answer: A

Yes, too many recursive calls may result into stack overflow. because when a function is called its return address is stored in stack.

After sometime the stack memory will be filled completely. Hence stack overflow error will occur.

Usually recursion works slower than loops.
A:
Yes
B:
No
C:
D:
Answer: A

When a recursive call is made, the function/process clones itself and then process that funtion. This leads to time and space constrains.

In a loop, there is no recursive call involved that saves a lot of time and space too.

Will the following functions work?
int f1(int a, int b)
{
    return ( f2(20) );
}
int f2(int a)
{
    return (a*a);
}
A:
Yes
B:
No
C:
D:
Answer: A

Yes, It will return the value 20*20 = 400

Example:


#include <stdio.h>
int f1(int, int); /* Function prototype */
int f2(int); /* Function prototype */

int main()
{
    int a = 2, b = 3, c;
    c = f1(a, b);
    printf("c = %d\n", c);
    return 0;
}

int f1(int a, int b)
{
    return ( f2(20) );
}

int f2(int a)
{
    return (a * a);
}

Output:
c = 400

Maximum number of arguments that a function can take is 12
A:
Yes
B:
No
C:
D:
Answer: B

No, C can accept upto 127 maximum number of arguments in a function.

If a function contains two return statements successively, the compiler will generate warnings. Yes/No ?
A:
Yes
B:
No
C:
D:
Answer: A

Yes. If a function contains two return statements successively, the compiler will generate "Unreachable code" warnings.

Example:


#include<stdio.h>
int mul(int, int); /* Function prototype */

int main()
{
    int a = 4, b = 3, c;
    c = mul(a, b);
    printf("c = %d\n", c);
    return 0;
}
int mul(int a, int b)
{
   return (a * b);
   return (a - b); /* Warning: Unreachable code */
}

Output:
c = 12

Ad Slot (Above Pagination)
Quiz