[go: up one dir, main page]

0% found this document useful (0 votes)
243 views8 pages

Assignment-8 Solution July 2019

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

ASSIGNMENT-8 SOLUTION

1. A function prototype is used for


a) Declaring the function logic
b) Calling the function from the main body
c) Telling the compiler, the kind of arguments used in the function
d) Telling the user for proper use of syntax while calling the function

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.

3. What is the output of the following C program?


#include <stdio.h>
void foo(), f();
int main()
{
f();
return 0;
}
void foo()
{
printf("2 ");
}
void f()
{
printf("1 ");
foo();
}

a)Compiler error as foo() is not declared in main


b)12
c)21
d)Compile time error due to declaration of functions inside main
Solution: (b) The function f() is called from the main. After printing ‘1’, foo() will be called
and ‘2’ will be printed. This is an example of nested function.

4. Which statement is correct about Passing by value parameters?


a) It cannot change the actual parameter value
ASSIGNMENT-8 SOLUTION

b) It can change the actual parameter value


c) Parameters is always in read-write mode
d) None of them

Solution: (a) It cannot change the actual parameter value

5. What will be the output?

int main()
{
{
int a = 70;
}
{
printf("%d", a);
}
return 0;
}
a) 70
b) Garbage value
c) Compilation error
d) None

Solution: (c) Compilation error.


A Block is a set of statements enclosed within left and right braces ({and} respectively). A
variable declared in a block is accessible in the block and all inner blocks of that block, but
not accessible outside the block. Thus the program produces compiler error.

6. How many times the function factorial will be executed?


#include<stdio.h>
int factorial(int);
int main()
{
int n=6;
long int f;

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.

7. What is the error in the following program


#include<stdio.h>
int f(int a)
{
a > 20? return(10): return(20);
}
int main()
{
int b;
b = f(20);
printf("%d\n", b);
return 0;
}
a) Error: Return statement cannot be used with conditional operators
b) Error: Prototype declaration
c) Error: Two return statements cannot be used in any function
d) No error

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.

9. What is the output of the C code given below


#include <stdio.h>
float func(float avg[]);

int main()
{
float result, avg[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
result = func(avg);
printf("Result is = %0.2f", result);
}

float func(float avg[])


{
int i;
float result, sum = 0.0;
for (i = 0; i < 6; ++i) {
sum += avg[i];
}
result = (sum / 6);
return 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.

10. How many times “Hello world” will be printed?


ASSIGNMENT-8 SOLUTION

#include<stdio.h>
int main()
{
printf(“Hello world\n”);
main();
return 0;
}

a) Infinite times
b) 32767
c) 65535
d) Till stack overflows

Solution: (d) Till stack overflows


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.

11. What is the output of the following C program?


#include <stdio.h>
int fun(int n)
{
int i, j, sum = 0;
for(i = 1; i<=n; i++)
for(j=i; j<=i; j++)
sum=sum+j;
return(sum);
}
int main()
{
printf("%d", fun(5));
return 0;
}

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.

13. What will be the output?

#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.

14. What will be the output?


#include<stdio.h>
ASSIGNMENT-8 SOLUTION

int f(int n, int k)


{
if (n == 0)
return 0;
else if (n % 2)
return f(n/2, 2*k) + k;
else return f(n/2, 2*k) - k;
}
int main()
{
printf("%d", f(20, 1));
return 0;
}

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

15. Consider the function


int fun(int x)
{
int f;
if (x>100)
f = x-10;
else
f = fun(fun(x+11));
return f;
}

For the input x = 95, the function will return


a) 89
b) 90
c) 91
d) 92
Solution: (c)
f(95)–>f(f(106)) = f(96)–>f(f(107)) = f(97)–>f(f(108)) = f(98)–>f(f(109)) = f(99)–
>f(f(110))=f(100)–>f(f(111))=f(101)=91
So the correct option is (c)
ASSIGNMENT-8 SOLUTION

You might also like