Miscellaneous On Data Structures - Study Mode

[#991] What will be the output for the following code? #include <stdio.h>
bool func(int arr[], int n, int sum)
{
if (sum == 0)
return true

if (n == 0 && sum != 0)
return false

if (arr[n-1] > sum)
return func(arr, n-1, sum)

return func(arr, n-1, sum) || func(arr, n-1, sum-arr[n-1])

}
int main()
{
int arr[] = {4,6, 12, 2}

int sum = 12

int n = sizeof(arr)/sizeof(arr[0])

if (func(arr, n, sum) == true)
printf("true")

else
printf("false")

return 0

}
Correct Answer

(C) True

[#992] Which of the following can be the base case for the recursive implementation used to find the length of linked list? #include<stdio.h>
#include<stdlib.h>
struct Node
{
int val

struct Node *next

}*head

int get_len()
{
struct Node *temp = head->next

int len = 0

while(temp != 0)
{
len++

temp = temp->next

}
return len

}
int main()
{
int arr[10] = {1,2,3,4,5}, n = 5, i

struct Node *temp, *newNode

head = (struct Node*)malloc(sizeof(struct Node))

head->next = 0

int len = get_len()

printf("%d",len)

return 0

}
Correct Answer

(D) if(current_node == 0) return 0

[#993] How many times is the function linear_search() called when the following code is executed? #include<stdio.h>
#include<stdlib.h>
struct Node
{
int val

struct Node* next

}*head

int linear_search(struct Node *temp,int value)
{
if(temp == 0)
return 0

if(temp->val == value)
return 1

return linear_search(temp->next, value)

}
int main()
{
int arr[6] = {1,2,3,4,5,6}

int n = 6,i

head = (struct Node*)malloc(sizeof(struct Node))

head->next = 0

struct Node *temp

temp = head

for(i=0

i<n

i++)
{
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node))

newNode->next = 0

newNode->val = arr[i]

temp->next = newNode

temp = temp->next

}
int ans = linear_search(head->next,6)

if(ans == 1)
printf("Found")

else
printf("Not found")

return 0

}
Correct Answer

(B) 6

[#994] What is the output of the following code: #include<stdio.h>
#include<stdlib.h>
struct Node
{
int val

struct Node* next

}*head

int get_max()
{
struct Node* temp = head->next

int max_num = temp->val

while(temp != 0)
{
if(temp->val > max_num)
max_num = temp->val

temp = head->next

}
return max_num

}
int main()
{
int n = 9, arr[9] ={5,1,3,4,5,2,3,3,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 max_num = get_max()

printf("%d %d",max_num)

return 0

}
Correct Answer

(C) runtime error

[#995] What is the space complexity of the given code? #include<stdio.h>
int power(int x, int y)
{
if (y == 0)
return 1

else if (y%2 == 0)
return power(x, y/2)*power(x, y/2)

else
return x*power(x, y/2)*power(x, y/2)

}
int main()
{
int x = 2

int y = 3

printf("%d", power(x, y))

return 0

}
Correct Answer

(A) O(1)