Programming and Data Structures - Numerical Answer Type (NAT) Questions | GATE CSE Notes

I am come up with collection of Numerical Answer Type(NAT) Questions of Programming and Data Structures for better preparation. Let's learn together...

Q1. on Recursion (Programming and Data Structures)

Consider the following C program:

#include <stdio.h>

int counter = 0;

int calc (int a, int b) {
int c;

counter++;
if (b==3) return (a*a*a);
else {
c = calc(a, b/3);
return (c*c*c);
}
}


int main (){
calc(4, 81);
printf ("%d", counter);
}

The output of this program is _____.

Answer is : 4 

Explanation:

- In given problem we have to find value of ‘counter’ variable.

- Counter variable will increment by one until function calc(4,81)return any value. When value of b becomes 3 (b=3) then, function calc(4,81)return value of a.

Recursion (Programming and Data Structures), Data Structure

Q2. on Recursion (Programming and Data Structures)

Consider the following program written in pseudo-code. Assume that x and y are integers.

Count(x,y) {
        if (y != 1){
            if (x != 1) {
                    print("*");
                    Count(x/2, y);
                            }
                    else {
                        y = y-1;
                        Count(1024, y);
                            }

                        }
                    }


The number of times that the print statement is executed by the call Count(1024,1024) is _____.

The answer is: 
10230

Explanation:

Programming and Data Structures - Numerical Answer Type (NAT) Questions | GATE CSE Notes, Data structures

When Count(1024,1024) function called, the value of x=1024, and y=1024. Now as given in problem when if(y!=1) is true, then inner if…else body will be get executed. In inner if…else body,
if(x!=1) is true, then print("*"); and Count(x/2, y); will be get executed.
Else
y = y-1; and Count(1024, y); Will be executed.

From the given problem value of x=1024, and y=1024. So inner if part:
if(x!=1) 
{
print("*");
Count(x/2, y); 
}
will be executed. This will be executed 10 times so print("*") will execute 10 times.
Then the value of x =1 so else part of inner if…else will be executed and value of y =1023.
And in this else part of inner else Count(1024, y) will be called.

Now value of x = 1024 and y =1023.
Again all steps from starting with the new value of x, and y will be executed. This will end when y becomes 1.

Every time value of y decrease with 1 and each time print function executed 10 times.
So initial value of y=1024, and this all steps repeat 1023 times and in each turn print(“*”) will executed 10 times.
when y = 1024 it execute print("*") 10 times
y = 1023 it execute print("*") 10 times
y = 1022 it execute print("*") 10 times
.
.
.
.
y = 2 it execute print("*") 10 times
y = 1 it will terminated.
So, 1023 x 10 times print("*") will get executed.
Answer is 10230
So, 1023 x 10 = 10230 times printf(“*”) will be executed.

Q3. on Recursion (Programming and Data Structures)

Consider the following program:
    int f(int *p, int n)
    {
          if (n <= 1) return 0;
          else return max(f(p+1,n-1),p[0]-p[1]);
    }

    int main()
   {
          int a[] = {3,5,2,6,4};
          printf("%d", f(a,5));
    }

Note: max(x,y) returns the maximum of x and y.

The value printed by this program is _________.

Answer: 3

Explanation:

Let's take int a=100;
int *p;
p=&a

Programming and Data Structures - Numerical Answer Type (NAT) Questions | GATE CSE Notes

Q4. on Recursion (Programming and Data Structures)

Consider the following C function.
int fun(int n)
{
    int x = 1, k;
    if (n = = 1) return x;
    for (k =1; k<n; ++k)
         x = x + fun(k) * fun(n–k);
    return x;
}
The return value of fun(5) is _____. 

GATE-CS-2015 (Set 2)

Answer is: 51 

Explanation:

fun(1)  = 1 

fun(2) : x=1 + fun(1)*fun(1)
x = 1 + 1 * 1
x = 2
fun(2) = 2

fun(3) : x = x+fun(1)*fun(2)
x = 1 + 1 * 2
x = 3

x = 3+fun(2)*fun(1)
x =  3 + 2 * 1
x = 5
fun(3) = 5

fun(4) : x = x+fun(1)*fun(3)
x = 1 + 1 * 5
x = 6

x = x+fun(2)*fun(2)
x = 6 + 2 * 2
x = 10

x = x+fun(3)*fun(1)
x = 10 + 5 * 1
x = 15

fun(5): x = x+fun(1)*fun(4)
x = 1 + 1 * 15
x= 16

x = x+fun(2)*fun(3)
x= 16+2*5
x= 26

x = x+fun(3)*fun(2)
x= 26+5*2
x= 36

x = x+fun(4)*fun(1)
x= 36+15*1
x= 51


Q5. on Recursion (Programming and Data Structures)

Consider the C program below.
#include <stdio.h>
int *A, stkTop;
int stkFunC(int opcode, int val)
{
static int size = 0, stkTop = 0;
switch (opcode)
{
case –1: size = val; break;
case 0: if(stkTop < size) A[stkTop++]= val; break;
default: if (stkTop) return A[– –stkTop];
}
return – 1;
}
int main( )
{
int B[20]; A = B; stkTop = –1;
stkFunc(–1, 10);
stkFunc(0, 5);
stkFunc(0, 10);
printf(“%d\n”, stkFunc(1,0)+stkFun(1,0);
}
The value printed by the above program is ______.

Answer is : 15

Explanation: 

Case  -1   =  Set Size of the Stack
Code : size = val

Case 0 = Push Operation
- Code: 
f (stkTop < size )   // verify stack is full or not?
A[stkTop++]=val;  //If not full then push the element in stkTop And increment stkTop.

Case (default) (here when 1 is as choice)[Pop Operation]
Code:
if (stkTop) return A[--stkTop];
In above code
- if stack is not empty thenPop the element from stkTop
- decrement stkTop

In this program: 
1.set stack size 10
2.push 2 elements in top of the stack as
    5 and 10
3.pop both ;then add them

So the result is 15.

Q6. on Recursion (Programming and Data Structures)

Consider the following C program.
#include<stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x = 10;
int main ( )
{
    int x = 1;
    x + = f1 ( ) + f2 ( ) + f3( ) + f2( );
    pirntf (“%d”, x);
    return 0;
}
int f1 ( ) { int x = 25; x++; return x;}
int f2 ( ) { static int x = 50; x++; return x;}
int f3 ( ) {x *= 10; return x};

The output of the program is ______.

- GATE CSE 2015

Answer is: 230

Explanation: 
int x=1;
x += f1() + f2 () + f3() + f2();
x = x + f1() + f2 () + f3() + f2();

f1()= 26
f2 ()=51
f3()= 100 because = x= x*10 // global x= 10
f2()= 52


That's it for today. 😊 Practice more and more. Happy Learning. 
Please leave suggestions, query, more about recursion in comment section. 
Keep Learning Keep Sharing Stay  Safe.😷

Post a Comment

0 Comments