Assignment-8 Solution July 2019
Assignment-8 Solution July 2019
Assignment-8 Solution July 2019
Solution: (c) A function prototype tells the compiler what kind of arguments a function is
looking to receive and what kind of return value a function is going to give back. This
approach helps the compiler ensure that calls to a function are made correctly and that no
erroneous type conversions are taking place.
2. What is the default return type if it is not specified in function definition?
a) void
b) integer
c) double
d) float
Solution: (b) Integer is the default data type if not specified in the function.
int main()
{
{
int a = 70;
}
{
printf("%d", a);
}
return 0;
}
a) 70
b) Garbage value
c) Compilation error
d) None
f = factorial(n);
printf("%d! = %ld\n", n, f);
return 0;
}
int factorial(int n)
{
if (n == 0)
return 1;
ASSIGNMENT-8 SOLUTION
else
return(n * factorial(n-1));
}
1
a)
6
b)
7
c)
Infinite times
d)
Solution: (c) This is an example of recursion. The function ‘factorial’ will be called 7 times
to compute the factorial of 6.
Solution: (a) In a ternary operator, we cannot use the return statement. The ternary
operator requires expressions but not code.
8. How many times ‘Hi’ will be printed in the program given below
#include<stdio.h>
int i = 0;
int fun();
int main()
{
while(i)
{
fun();
}
printf("Hello\n");
ASSIGNMENT-8 SOLUTION
return 0;
}
int fun()
{
printf("Hi");
}
a) Only once
b) Zero times
c) Infinite times
d) Compilation error
Solution: (b) The default value of i is ‘0’. Thus, the while loop will never be executed and the
control will not come into the function. Thus, ‘Hi’ will never be printed.
int main()
{
float result, avg[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
result = func(avg);
printf("Result is = %0.2f", result);
}
a) Result is = 27.08
b) Result is = 27.083334
c) Compiler error as result is declared twice
d) Error: Invalid prototype declaration
Solution: (a) The program finds the average of the array elements. As the variables declared
in the function are local, they can be used in the main as well. 0.2f will print two decimal
points. Thus the output is 27.08.
#include<stdio.h>
int main()
{
printf(“Hello world\n”);
main();
return 0;
}
a) Infinite times
b) 32767
c) 65535
d) Till stack overflows
Solution: The program finds the sum of the integer numbers till 5. Thus the output is 15.
12. How many times the function get() will be invoked if get(6) is called from the main
function
void get(int n)
{
ASSIGNMENT-8 SOLUTION
if (n<1) return;
get(n-1);
get(n-3);
}
a) 6 times
b) 25 times
c) 12 times
d) Infinite times
Solution: (b) This is a recursive function. You can easily verify the counting by declaring a
global int variable count and defining count+=1; inside the function.
#include<stdio.h>
void func(int n, int sum)
{
int k = 0, j = 0;
if (n == 0) return;
k = n % 10;
j = n / 10;
sum = sum + k;
func(j, sum);
printf ("%d,", k);
}
int main ()
{
int a = 2048, sum = 0;
func(a, sum);
printf ("%d ", sum);
}
a) 8, 4, 0, 2, 14
b) 8, 4, 0, 2, 0
c) 2, 0, 4, 8. 14
d) 2, 0, 4, 8, 0
Solution: (d) 2, 0, 4, 8, 0
sum has no use in func(), it is there just to confuse. Function func() just prints all digits of a
number. In main, there is one more printf statement after func(), so one more 0 is printed after
all digits of n.
Solution: 9
f(20,1) = 9.
f(10,2) - 1 = 9
f(5,4) - 2 = 10
f(2,8) + 4 = 12
f(1,16) - 8 = 8
f(0,32) + 16 = 16