Miscellaneous On Data Structures - Study Mode
[#1031] What will be output for the given code? #include<bits/stdc++.h>
using namespace std
string encrypter(string keyword)
{
string encoded = ""
bool arr[26] = {0}
for (int i=0
i<keyword.size()
i++)
{
if(keyword[i] >= 'A' && keyword[i] <= 'Z')
{
if (arr[keyword[i]-65] == 0)
{
encoded += keyword[i]
arr[keyword[i]-65] = 1
}
}
else if (keyword[i] >= 'a' && keyword[i] <= 'z')
{
if (arr[keyword[i]-97] == 0)
{
encoded += keyword[i] - 32
alpha[keyword[i]-97] = 1
}
}
}
for (int i=0
i<26
i++)
{
if(arr[i] == 0)
{
arr[i]=1
encoded += char(i + 65)
}
}
return encoded
}
string ciphertxt(string msg, string encoded)
{
string cipher=""
for (int i=0
i<msg.size()
i++)
{
if (msg[i] >='a' && msg[i] <='z')
{
int pos = msg[i] - 97
cipher += encoded[pos]
}
else if (msg[i] >='A' && msg[i] <='Z')
{
int pos = msg[i] - 65
cipher += encoded[pos]
}
else
{
cipher += msg[i]
}
}
return cipher
}
int main()
{
string keyword
keyword = "cipher"
string encoded = encrypter(keyword)
string message = "hello"
cout << ciphertxt(message,encoded) << endl
return 0
}
Correct Answer
(C) BEJJM
[#1032] Find the output of the following code. #include<math.h>
#include<iostream.h>
using namespace std
void distance(float x, float y, float a, float b, float c)
{
float d = fabs((a * x + b * y + c)) / (sqrt(a * a + b * b))
cout<<d
return
}
int main()
{
float x = -2
float y = -3
float a = 5
float b = -2
float c = - 4
distance(x, y, a, b, c)
return 0
}
Correct Answer
(C) 1.4
[#1033] What is the output of the following code? #include<stdio.h>
int get_max_element(int *arr,int n)
{
int i, max_element = arr[0]
for(i = 1
i < n
i++)
if(arr[i] > max_element)
max_element = arr[i]
return max_element
}
int get_min_element(int *arr, int n)
{
int i, min_element = arr[0]
for(i = 1
i < n
i++)
if(arr[i] < min_element)
min_element = arr[i]
return min_element
}
int main()
{
int n = 7, arr[7] = {5,2,4,7,8,1,3}
int max_element = get_max_element(arr,n)
int min_element = get_min_element(arr,n)
printf("%d %d",max_element,min_element)
return 0
}
Correct Answer
(C) 8 1
[#1034] Consider the following recursive implementation of linear search: #include<stdio.h>
int recursive_search_num(int *arr, int num, int idx, int len)
{
if(idx == len)
return -1
if(arr[idx] == num)
return idx
return __________
}
int main()
{
int arr[5] ={1,3,3,3,5},num=2,len = 5
int indx = recursive_search_num(arr,num,0,len)
printf("Index of %d is %d",num,indx)
return 0
} Which of the following recursive calls should be added to complete the above code?
Correct Answer
(C) recursive_search_num(arr, num, idx+1, len)
[#1035] What will be the time complexity of the following code? #include<stdio.h>
int power(int x, int y)
{
int temp
if( y == 0)
return 1
temp = power(x, y/2)
if (y%2 == 0)
return temp*temp
else
return x*temp*temp
}
int main()
{
int x = 2
int y = 3
printf("%d", power(x, y))
return 0
}
Correct Answer
(C) O(log n)