Recursive Programs questions

Q21

GATE CS 2018 CSRecursionNAT1 mark
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 _____.
Show answer without attempting

Answer: 4

Q32

GATE CS 2018 CSRecursionMCQ2 marks−2/3 if wrong
Consider the following C code. Assume that unsigned long int type length is 64 bits.
unsigned long int fun(unsigned long int n){
 unsigned long int i, j = 0, sum = 0;
 for (i = n; i > 1; i = i/2) j++;
 for ( ; j > 1; j = j/2) sum++;
 return(sum);
}
The value returned when we call fun with the input 2402^{40} is
Show answer without attempting

Answer: B

Q45

GATE CS 2018 CSRecursionNAT2 marks
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 _____.
Show answer without attempting

Answer: 10230

0/30 correct0 marks