Assignment8 July 2024
Assignment8 July 2024
1. What is the default return type if it is not specified in the function definition?
a) void
b) int
c) double
d) float
Solution: (b) Integer is the default data type if not specified in the function.
a) 70
b) Garbage value
c) Compilation error
d) None
a) Infinite times
b) 32767
c) 65535
d) Till stack overflows
int main ()
{
int a = 2048, sum = 0;
func (a, sum);
printf ("%d ", sum);
}
Week 8: Assignment 8 Solution
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.
a) 25
b) 5
c) 15
d) 10
Solution: (c) The program finds the sum of the integer numbers till 5. Thus the output is 15.
int main()
{
int num1 = 10, num2 = 20;
printf("Before swapping num1 = %d num2 = %d\n", num1, num2);
swap(num1, num2);
printf("After swapping num1 = %d num2 = %d \n", num1, num2);
return 0;
}
Week 8: Assignment 8 Solution
The function above has a flaw that may result in a serious error during some
invocations.
Which one of the following describes the deficiency illustrated above?
(a) For some values of n, the environment will almost certainly exhaust its stack
space before the calculation completes.
(b) An error in the algorithm causes unbounded recursion for all values of n.
(c) A break statement should be inserted after each case. Fall-through is not
desirable here.
(d) The fibonacci() function includes calls to itself. This is not directly supported
by Standard C due to its unreliability.
Solution (a) For some values of n, the environment will almost certainly exhaust its stack
space before the calculation completes.
The function calls itself. So it's an example of recursion. As there is no proper terminating
condition for the function, for a large value of n it will exhaust the storage space (stack)
which will stop the execution without final result.