Miscellaneous On Data Structures - Study Mode

[#1106] What is the output of the following code? #include<stdio.h>
#include<stdlib.h>
struct Node
{
int val
struct Node* next
}*head
int linear_search(int value)
{
struct Node *temp = head->next
while(temp -> next != 0)
{
if(temp->val == value)
return 1
temp = temp->next
}
return 0
}
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(60)
if(ans == 1)
printf("Found")
else
printf("Not found")
return 0
}
Correct Answer

(B) Not found

[#1107] What does the following code do? #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] = "abcdefg"
char t[100]
strcpy(t,s)
reverse_string(s)
if(strcmp(t,s) == 0)
printf("Yes")
else
printf("No")
return 0
}
Correct Answer

(D) Checks if a string is a palindrome

[#1108] What is the output of the following code? int cnt = 0
void my_recursive_function(char *s, int i)
{
if(s[i] == 'x00')
return
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
cnt++
my_recursive_function(s,i+1)
}
int main()
{
my_recursive_function("thisisrecursion",0)
printf("%d",cnt)
return 0
}
Correct Answer

(A) 6

[#1109] What is the output for the following code? #include <bits/stdc++.h>
using namespace std
void convert(int a[], int n)
{
vector <pair<int, int> > vec
for (int i = 0
i < n
i++)
vec.push_back(make_pair(a[i], i))
sort(vec.begin(), vec.end())
for (int i=0
i<n
i++)
a[vec[i].second] = i
}
void printArr(int a[], int n)
{
for (int i=0
i<n
i++)
cout << a[i] << " "
}
int main()
{
int arr[] = {10,8,2,5,7}
int n = sizeof(arr)/sizeof(arr[0])
convert(arr , n)
printArr(arr, n)
return 0
}
Correct Answer

(A) 4 3 0 1 2

[#1110] What does the following code do? #include<stdio.h>
int cnt = 0
int recursive_search_num(int *arr, int num, int idx, int len)
{
int cnt = 0
if(idx == len)
return cnt
if(arr[idx] == num)
cnt++
return cnt + recursive_search_num(arr, num, idx+1, len)
}
int main()
{
int arr[8] ={0,0,0,0,3,5,-6,7},num = 0,len = 8
int ans = recursive_search_num(arr,num,0,len)
printf("%d",ans)
return 0
}
Correct Answer

(C) Counts the number of occurrences of the number 0