Miscellaneous On Data Structures - Study Mode
[#1016] What will be the output for the given code? #include <stdio.h>
bool func (int arr[], int n)
{
int sum = 0
int i, j
for (i = 0
i < n
i++)
sum += arr[i]
if (sum%2 != 0)
return false
bool partition[sum/2+1][n+1]
for (i = 0
i <= n
i++)
partition[0][i] = true
for (i = 1
i <= sum/2
i++)
partition[i][0] = false
for (i = 1
i <= sum/2
i++)
{
for (j = 1
j <= n
j++)
{
partition[i][j] = partition[i][j-1]
if (i >= arr[j-1])
partition[i][j] = partition[i][j] || partition[i - arr[j-1]][j-1]
}
}
return partition[sum/2][n]
}
int main()
{
int arr[] = {3, 3, 4, 4, 7}
int n = sizeof(arr)/sizeof(arr[0])
if (func(arr, n) == true)
printf("true")
else
printf("false")
return 0
}
Correct Answer
(B) false
[#1017] What is the output of the following code? #include<stdio.h>
int recursive_get_len(char *s, int len)
{
if(s[len] == 0)
return 0
return 1 + recursive_get_len(s, len+1)
}
int main()
{
char *s = "123-1-2-3"
int len = recursive_get_len(s,0)
printf("%d",len)
return 0
}
Correct Answer
(C) 9
[#1018] What is the output of the following code? #include<stdio.h>
#include<string.h>
void reverse_string(char *s)
{
int len = strlen(s)
int i,j
i=0
j=len-1
while(i < j)
{
char tmp = s[i]
s[i] = s[j]
s[j] = tmp
i++
j--
}
}
int main()
{
char s[100] = "reverse"
reverse_string(s)
printf("%s",s)
return 0
}
Correct Answer
(B) esrever
[#1019] Choose the correct statement for the following code segment? bool check (int N)
{
if( N & (1 << i) )
return true
else
return false
}
Correct Answer
(C) function returns true if ith bit of N is set
[#1020] Consider the following code snippet to find the smallest element in an array: int get_min_element(int *arr, int n)
{
int i, min_element = arr[0]
for(i = 1
i < n
i++)
if(_______)
min_element = arr[i]
return min_element
} Which of the following lines should be inserted to complete the above code?
Correct Answer
(B) arr[i] < min_element