Miscellaneous On Data Structures - Study Mode
[#1011] What is the output of the following code? #include<stdio.h>
#include<stdlib.h>
struct Node
{
int val
struct Node* next
}*head
int min_of_two(int a, int b)
{
if(a < b)
return a
return b
}
int recursive_get_min(struct Node* temp)
{
if(temp->next == 0)
return temp->val
return min_of_two(temp->val,recursive_get_min(temp->next))
}
int main()
{
int n = 5, arr[5] ={1,1,1,1,1},i
struct Node *temp, *newNode
head = (struct Node*)malloc(sizeof(struct Node))
head -> next =0
temp = head
for(i=0
i<n
i++)
{
newNode =(struct Node*)malloc(sizeof(struct Node))
newNode->next = 0
newNode->val = arr[i]
temp->next =newNode
temp = temp->next
}
int min_num = recursive_get_min(head->next)
printf("%d",min_num)
return 0
}
Correct Answer
(A) 1
[#1012] What will be the output for following code? float power(float x, int y)
{
float temp
if( y==0)
return 1
temp = power(x, y/2)
if (y%2 == 0)
return temp*temp
else
{
if(y > 0)
return x*temp*temp
else
return (temp*temp)/x
}
}
int main()
{
float x = 2
int y = -3
printf("%f", power(x, y))
return 0
}
Correct Answer
(D) 0.25
[#1013] What will happen when the below code snippet is executed? void my_recursive_function()
{
my_recursive_function()
}
int main()
{
my_recursive_function()
return 0
}
Correct Answer
(D) The code will run for some time and stop when the stack overflows
[#1014] The time complexity of the following recursive implementation to find the factorial of a number is . . . . . . . . int fact(int n)
{
if(_________)
return 1
return n * fact(n - 1)
}
int main()
{
int n = 5
int ans = fact(n)
printf("%d",ans)
return 0
}
Correct Answer
(B) O(n)
[#1015] Consider the following recursive implementation to find the largest element in a linked list: struct Node
{
int val
struct Node* next
}*head
int max_of_two(int a, int b)
{
if(a > b)
return a
return b
}
int recursive_get_max(struct Node* temp)
{
if(temp->next == 0)
return temp->val
return max_of_two(______, _______)
} Which of the following arguments should be passed to the function max_of two() to complete the above code?
Correct Answer
(A) temp->val,recursive_get_max(temp->next)