Miscellaneous On Data Structures - Study Mode
[#1026] In which of the below cases will the following code produce a wrong output? int recursive_binary_search(int *arr, int num, int lo, int hi)
{
if(lo > hi)
return -1
int mid = (lo + hi)/2
if(arr[mid] == num)
return mid
else if(arr[mid] < num)
lo = mid + 1
else
hi = mid - 1
return recursive_binary_search(arr, num, lo, hi)
}
Correct Answer
(C) Array: {5,4,3,2,1} Search: 1
[#1027] What is the output of the following code? #include<stdio.h>
#include<stdlib.h>
struct Node
{
int val
struct Node *next
}*head
int recursive_get_len(struct Node *current_node)
{
if(current_node == 0)
return 0
return 1 + recursive_get_len(current_node->next)
}
int main()
{
int arr[10] = {-1,2,3,-3,4,5}, n = 6, 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->val = arr[i]
newNode->next = 0
temp->next = newNode
temp = temp->next
}
int len = recursive_get_len(head->next)
printf("%d",len)
return 0
}
Correct Answer
(B) 6
[#1028] What is the output of the following code? #include<stdio.h>
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 = 1234321
int ans = recursive_sum_of_digits(n)
printf("%d",ans)
return 0
}
Correct Answer
(B) 16
[#1029] Consider the following iterative implementation to find the factorial of a number. Which of the lines should be inserted to complete the below code? int main()
{
int n = 6, i
int fact = 1
for(i=1
i<=n
i++)
_________
printf("%d",fact)
return 0
}
Correct Answer
(B) fact = fact * i
[#1030] What will be the output for the following code? #include <stdio.h>
void combination(int arr[], int aux[], int start, int end, int index, int r)
void print(int arr[], int n, int r)
{
int aux[r]
combination(arr, aux, 0, n-1, 0, r)
}
void combination(int arr[], int aux[], int start, int end, int index, int r)
{
if (index == r)
{
for (int j=0
j<r
j++)
printf("%d ", aux[j])
printf(", ")
return
}
for (int i=start
i<=end && end-i+1 >= r-index
i++)
{ aux[index] = arr[i]
combination(arr, aux, i+1, end, index+1, r)
}
}
int main()
{
int arr[] = {1, 2, 3}
int r = 2
int n = sizeof(arr)/sizeof(arr[0])
print(arr, n, r)
}
Correct Answer
(C) 1 2, 1 3, 2 3,