Miscellaneous On Data Structures - Study Mode
[#1006] How many times is the function recursive_sum() called when the following code is executed? #include<stdio.h>
int recursive_sum(int n)
{
if(n == 0)
return 0
return n + recursive_sum(n - 1)
}
int main()
{
int n = 5
int ans = recursive_sum(n)
printf("%d",ans)
return 0
}
Correct Answer
(C) 6
[#1007] How many times is the function recursive_reverse_string() called when the following code is executed? #include<stdio.h>
#include<string.h>
void recursive_reverse_string(char *s, int left, int right)
{
if(left < right)
{
char tmp = s[left]
s[left] = s[right]
s[right] = tmp
recursive_reverse_string(s, left+1, right-1)
}
}
int main()
{
char s[100] = "madam"
int len = strlen(s)
recursive_reverse_string(s,0,len-1)
printf("%s",s)
return 0
}
Correct Answer
(A) 3
[#1008] What is the base case for the following code? void my_recursive_function(int n)
{
if(n == 0)
return
printf("%d ",n)
my_recursive_function(n-1)
}
int main()
{
my_recursive_function(10)
return 0
}
Correct Answer
(C) if(n == 0)
[#1009] Consider the following recursive implementation used to convert a decimal number to its binary equivalent: #include<stdio.h>
int arr[31], len = 0
void recursive_dec_to_bin(int n)
{
if(n == 0 && len == 0)
{
arr[len++] = 0
return
}
if(n == 0)
return
__________
recursive_dec_to_bin(n/2)
}
int main()
{
int n = 100,i
recursive_dec_to_bin(n)
for(i=len-1
i>=0
i--)
printf("%d",arr[i])
return 0
} Which of the following lines should be inserted to complete the above code?
Correct Answer
(C) arr[len++] = n % 2
[#1010] What is the output of the following code? #include<stdio.h>
int cnt =0
int my_function(int n, int sm)
{
int i, tmp_sm
for(i=1
i<=n
i++)
{
tmp_sm = recursive_sum_of_digits(i)
if(tmp_sm == sm)
cnt++
}
return cnt
}
int recursive_sum_of_digits(int n)
{
if(n == 0)
return 0
return n % 10 + recursive_sum_of_digits(n/10)
}
int main()
{
int n = 20, sum = 3
int ans = my_function(n,sum)
printf("%d",ans)
return 0
}
Correct Answer
(C) 2