Exercise ::
Functions – Find Output of Program
Functions – General Questions
Functions – Find Output of Program
Functions – Point Out Errors
Functions – Point Out Correct Statements
Functions – True / False Questions
Functions – Yes / No Questions
1.
The keyword used to transfer control from a function back to the calling function is
A. switch
B. goto
C. go back
D. return
Answer: Option D
Explanation:
The keyword return is used to transfer control from a function back to the calling function.
Example:
#include<stdio.h>
int sum(int, int); /* Function prototype / declaration*/
int main()
{
int a = 10, b = 20, c;
c = sum(a, b); /* Function calling */
printf(“Sum = %d\n”, c);
return 0;
}
int sum(int a, int b) /* function definition */
{
/* returns the value and control back to main() function */
return (a+b);
}
Output:
Sum = 30
2.
How many times the program will print “Catalyst” ?
#include<stdio.h>
int main()
{
printf("Catalyst");
main();
return 0;
}
A. Infinite times
B. 32767 times
C. 65535 times
D. Till stack overflows
Answer: Option D
Explanation:
A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing.
A stack overflow occurs when too much memory is used on the call stack.
Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.
3.
What will be the output of the program?
#include<stdio.h>
void fun(int*, int*);
int main()
{
int i=5, j=2;
fun(&i, &j);
printf("%d, %d", i, j);
return 0;
}
void fun(int *i, int *j)
{
*i = *i**i;
*j = *j**j;
}
A. 5, 2
B. 10, 4
C. 2, 5
D. 25, 4
Answer: Option D
Explanation:
Step 1: int i=5, j=2; Here variable i and j are declared as an integer type and initialized to 5 and 2 respectively.
Step 2: fun(&i, &j); Here the function fun() is called with two parameters &i and &j (The & denotes call by reference. So the address of the variable i and j are passed. )
Step 3: void fun(int *i, int *j) This function is called by reference, so we have to use * before the parameters.
Step 4: *i = *i**i; Here *i denotes the value of the variable i. We are multiplying 5*5 and storing the result 25 in same variable i.
Step 5: *j = *j**j; Here *j denotes the value of the variable j. We are multiplying 2*2 and storing the result 4 in same variable j.
Step 6: Then the function void fun(int *i, int *j) return back the control back to main() function.
Step 7: printf(“%d, %d”, i, j); It prints the value of variable i and j.
Hence the output is 25, 4.
4.
What will be the output of the program?
#include<stdio.h>
void fun(int*, int);
int main()
{
int i=10, j=20;
fun(&i, j);
printf("%d, %d", i, j);
return 0;
}
void fun(int *p, int q)
{
*p = *p * q;
q = *p + q;
}
A. 10, 20
B. 200, 220
C. 200, 20
D. 25, 4
Answer: Option C
Explanation:
Step 1: int i=5, j=2; Here variable i and j are declared as an integer type and initialized to 10 and 20 respectively.
Step 2: fun(&i, j); Here the function fun() is called with two parameters &i and j (The & denotes call by reference. So the address of the variable i is passed and j is passed by value. )
Step 3: void fun(int *p, int q) This function is called by reference and by value , so we have to use * before the parameters(for by reference) and one is passed by value.
Step 4: *p = *p * q; Here *p denotes the value of the variable i. We are multiplying 10*20 and storing the result 200 in variable i.
Step 5: p = *p + q; Here q denotes the value of the variable j. We are multiplying 200 + 20 and storing the result 220 in same variable j.(But the value is not reflected back as it is called by value).
Step 6: Then the function void fun(int *p, int q) return back the control back to main() function.
Step 7: printf(“%d, %d”, i, j); It prints the value of variable i and j.
Hence the output is 200, 20.
5.
What will be the output of the program?
#include<stdio.h>
int i;
int fun();
int main()
{
while(i)
{
fun();
main();
}
printf("Hello\n");
return 0;
}
int fun()
{
printf("Hi");
}
Answer: Option A
Explanation:
Step 1: int i; The variable i is declared as an integer type.
Step 2: int fun(); This prototype tells the compiler that the function fun() does not accept any arguments and it returns an integer value.
Step 3: while(i) The value of i is not initialized (i is a global variable so its default value is 0 ) so while condition is failed. So, it does not execute the while block.
Step 1: printf(“Hello\n”); It prints “Hello”.
Hence the output of the program is “Hello”.