Question 22
The function f is defined as follows:
int f (int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n/2);
else return f(3n - 1);
}
Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
1. The function f terminates for finitely many different values of n ≥ 1.
ii. The function f terminates for infinitely many different values of n ≥ 1.
iii. The function f does not terminate for finitely many different values of n ≥ 1.
iv. The function f does not terminate for infinitely many different values of n ≥ 1.
Which one of the following options is true of the above?
Question 23
Consider the code fragment written in JAVA below :
void f (int n)
{
if (n <=1) {
System.out.print(n);
}
else {
f (n/2);
System.out.print(n%2);
}
}
What does f(173) print?
Question 24
What is the output of the following program?
#include <stdio.h>
void print(int n, int j)
{
if (j >= n)
return;
if (n - j > 0 && n - j >= j)
printf("%d %d\n", j, n-j);
print(n, j+1);
}
int main()
{
int n = 8;
print(n, 1);
}
Question 25
What is the name of below recursive program?
void fun(int n, char from_rod, char to_rod,
char aux_rod)
{
if (n == 0) {
return;
}
fun(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod
<< " to rod " << to_rod << endl;
fun(n - 1, aux_rod, to_rod, from_rod);
}
Question 26
What is the output of the below program for the tree:
[caption width="800"]Tree[/caption]void printInorder(struct Node* node)
{
if (node == NULL)
return;
printInorder(node->left);
cout << node->data << " ";
printInorder(node->right);
}
Question 27
What is the output of the following code for the input arr[]={1,2,3,4,5,6} N=6?
int fun(int A[], int N)
{
if (N <= 0)
return 0;
return (fun(A, N - 1) + A[N - 1]);
}
Question 28
Consider the below Program and identify the problem:
void fun2(int arr[], int start_index, int end_index)
{
if (start_index >= end_index)
return;
int min_index;
int temp;
min_index = minIndex(arr, start_index, end_index);
swap(arr[start_index], arr[min_index]);
fun2(arr, start_index + 1, end_index);
}
Question 29
Match the pairs in the following questions:
List 1 | List 2 |
A. Recursion | 1. Sorted Array |
B. Binary Seach | 2. Recursion |
C. Sorting | 3 Base case |
D. Dynamic Programming | 4.O(NlogN) |
Question 30
Consider the below program, what operation is performed below:
void fun(int arr[], int n)
{
if (n == 1)
return;
int count = 0;
for (int i=0; i<n-1; i++)
if (arr[i] > arr[i+1]){
swap(arr[i], arr[i+1]);
count++;
}
return;
fun(arr, n-1);
}
There are 30 questions to complete.