Miscellaneous On Data Structures - Study Mode

[#1051] What will be the output of the following code? #include <stdio.h>
#include <string.h>
#include <iostream.h>
using namespace std
void swap(char *x, char *y)
{
char temp
temp = *x
*x = *y
*y = temp
}

void func(char *a, int l, int r)
{
int i
if (l == r)
cout<<a<<” ,”
else
{
for (i = l
i <= r
i++)
{
swap((a+l), (a+i))
func(a, l+1, r)
swap((a+l), (a+i))
}
}
}

int main()
{
char str[] = "AA"
int n = strlen(str)
func(str, 0, n-1)
return 0
}
Correct Answer

(D) AA,AA,

[#1052] How many times is the function recursive_sum_of_digits() called when the following code is executed? #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 = 1201
int ans = recursive_sum_of_digits(n)
printf("%d",ans)
return 0
}
Correct Answer

(C) 5

[#1053] What will be the time complexity of the following code? int xpowy(int x, int n)
{
if (n==0)
return 1
if (n==1)
return x
if ((n % 2) == 0)
return xpowy(x*x, n/2)
else
return xpowy(x*x, n/2) * x
}
Correct Answer

(A) O(log n)

[#1054] What is the output of the following code? int fibo(int n)
{
if(n == 1)
return 0
else if(n == 2)
return 1
return fibo(n - 1) + fibo(n - 2)
}
int main()
{
int n = 5
int ans = fibo(n)
printf("%d",ans)
return 0
}
Correct Answer

(C) 3

[#1055] What will be the output for the following code? #include<stdio.h>
void combination(int arr[],int n,int r,int index,int aux[],int i)
void print(int arr[], int n, int r)
{
int aux[r]
combination(arr, n, r, 0, aux, 0)
}
void combination(int arr[], int n, int r, int index, int aux[], int i)
{
if (index == r)
{
for (int j=0
j<r
j++)
printf("%d ",aux[j])
printf(", ")
return
}
if (i >= n)
return
aux[index] = arr[i]
combination(arr, n, r, index+1, aux, i+1)
combination(arr, n, r, index, aux, i+1)
}
int main()
{
int arr[] = {1, 2,2}
int r = 2
int n = sizeof(arr)/sizeof(arr[0])
print(arr, n, r)
return 0
}
Correct Answer

(B) 1 2, 1 2, 2 2 ,