Functions
- Which of the following is the correct output for the program given below?
#include <stdio.h>
void fun ( int*, int* ) ;
int main( )
{
int a = 6, b = 3 ;
fun (&a, &b);
printf ("%d %d\n" , a, b);
return 0 ;
}
void fun (int*a, int*b)
{
*a = *a* *a;
*b = *b* *b ;
}
-
View Hint View Answer Discuss in Forum
Step 1: int a=6, b=3; Here variable a and b are declared as an integer type and initialized to 6 and 3 respectively.
Step 2: fun(&a, &b); Here the function fun() is called with two parameters &a and &b (The & denotes call by reference. So the address of the variable a and b are passed. )
Step 3: void fun(int *a, int *b) This function is called by reference, so we have to use * before the parameters.
Step 4: *a = *a**a; Here *a denotes the value of the variable a. We are multiplying 5*5 and storing the result 25 in same variable a.
Step 5: *b = *b**b; Here *b denotes the value of the variable b. We are multiplying 2*2 and storing the result 4 in same variable b.
Step 6: Then the function void fun(int *a, int *b) return back the control back to main() function.
Step 7: printf("%d, %d", a, b); It prints the value of variable a and b.
Hence the output is 36, 9.Correct Option: D
Step 1: int a=6, b=3; Here variable a and b are declared as an integer type and initialized to 6 and 3 respectively.
Step 2: fun(&a, &b); Here the function fun() is called with two parameters &a and &b (The & denotes call by reference. So the address of the variable a and b are passed. )
Step 3: void fun(int *a, int *b) This function is called by reference, so we have to use * before the parameters.
Step 4: *a = *a**a; Here *a denotes the value of the variable a. We are multiplying 5*5 and storing the result 25 in same variable a.
Step 5: *b = *b**b; Here *b denotes the value of the variable b. We are multiplying 2*2 and storing the result 4 in same variable b.
Step 6: Then the function void fun(int *a, int *b) return back the control back to main() function.
Step 7: printf("%d, %d", a, b); It prints the value of variable a and b.
Hence the output is 36, 9.
- How many times the following program will print "Manjesh"?
#include <stdio.h>
int main ( )
{
printf ("Manjesh\n");
main ( ) ;
return 0 ;
}
-
View Hint View Answer Discuss in Forum
Till the stack doesn't overflow
Correct Option: D
Till the stack doesn't overflow
- Which of the following is the correct output for the program given below?
#include<stdio.h>
int main ( )
{
int fun (int);
int k = fun (20);
printf ("%d\n" , --k) ;
return 0 ;
}
int fun (int i)
{
return (k++) ;
}
-
View Hint View Answer Discuss in Forum
Focus on pre-increment and post-increment of variable.
Correct Option: A
Step 1: int fun(int); Here we declare the prototype of the function fun().
Step 2: int i = fun(20); The variable i is declared as an integer type and the result of the fun(20) will be stored in the variable i.
Step 3: int fun(int k){ return (k++); } Inside the fun() we are returning a value return(k++). It returns 20. because k++ is the post-increment operator.
Step 4: Then the control back to the main function and the value 20 is assigned to variable k.
Step 5: printf("%d\n", --k); Here --k denoted pre-increment. Hence it prints the value 19.