Consider the following C function.
C++
int fun(int n) {
int x = 1, k;
if (n == 1)
return x;
for (k = 1; k < n; ++k)
x = x + fun(k) * fun(n - k);
return x;
}
C
int fun (int n){
int x = 1, k;
if (n == 1)
return x;
for (k = 1; k < n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}
Java
int fun(int n) {
int x = 1;
if (n == 1)
return x;
for (int k = 1; k < n; ++k)
x = x + fun(k) * fun(n - k);
return x;
}
Python
def fun(n):
x = 1
if n == 1:
return x
for k in range(1, n):
x += fun(k) * fun(n - k)
return x
JavaScript
function fun(n) {
let x = 1;
if (n === 1)
return x;
for (let k = 1; k < n; ++k)
x += fun(k) * fun(n - k);
return x;
}
The return value of fun(5) is __________.
Consider the below program, what operation is performed below:
C++
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);
}
C
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]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
count++;
}
fun(arr, n - 1);
}
Java
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]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
count++;
}
fun(arr, n - 1);
}
Python
def fun(arr, n):
if n == 1:
return
count = 0
for i in range(n - 1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
count += 1
fun(arr, n - 1)
JavaScript
function fun(arr, n) {
if (n === 1)
return;
let count = 0;
for (let i = 0; i < n - 1; i++)
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
count++;
}
fun(arr, n - 1);
}
Insertion Sort Recursively
Selection Sort Recursively
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)A – 2, B – 1, C – 4, D – 3
A – 3, B – 4, C – 1, D – 2
A – 3, B – 1, C – 4, D – 2
A – 2, B – 4, C – 1, D – 3
Consider the below Program and identify the problem:
C++
#include <iostream>
using namespace std;
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);
}
C
#include <stdio.h>
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);
temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;
fun2(arr, start_index + 1, end_index);
}
Java
public class Main {
public static void fun2(int[] arr, int start_index, int end_index) {
if (start_index >= end_index)
return;
int min_index;
min_index = minIndex(arr, start_index, end_index);
int temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;
fun2(arr, start_index + 1, end_index);
}
}
Python
def fun2(arr, start_index, end_index):
if start_index >= end_index:
return
min_index = minIndex(arr, start_index, end_index)
arr[start_index], arr[min_index] = arr[min_index], arr[start_index]
fun2(arr, start_index + 1, end_index)
JavaScript
function fun2(arr, start_index, end_index) {
if (start_index >= end_index)
return;
let min_index = minIndex(arr, start_index, end_index);
let temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;
fun2(arr, start_index + 1, end_index);
}
Selection Sort Recursive implementation
Bubble sort Recursive implementation
Finding Pair Recursive implementation
What is the output of the following code for the input arr[]={1,2,3,4,5,6} N=6?
C++
int fun(int arr[], int n)
{
if (n <= 0)
return 0;
return (fun(arr, n - 1) + arr[n - 1]);
}
C
#include <stdio.h>
int fun(int arr[], int n) {
if (n <= 0)
return 0;
return (fun(arr, n - 1) + arr[n - 1]);
}
Java
public class Main {
public static int fun(int[] arr, int n) {
if (n <= 0)
return 0;
return (fun(arr, n - 1) + arr[n - 1]);
}
}
Python
def fun(arr, n):
if n <= 0:
return 0
return fun(arr, n - 1) + arr[n - 1]
JavaScript
function fun(arr, n) {
if (n <= 0)
return 0;
return fun(arr, n - 1) + arr[n - 1];
}
What is the output of the below program for the tree:
Tree C++
void printInorder(struct Node* node)
{
if (node == NULL)
return;
printInorder(node->left);
cout << node->data << " ";
printInorder(node->right);
}
C
void printInorder(struct Node* node) {
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
Java
void printInorder(Node node) {
if (node == null)
return;
printInorder(node.left);
System.out.print(node.data + " ");
printInorder(node.right);
}
Python
def print_inorder(node):
if node is None:
return
print_inorder(node.left)
print(node.data, end=' ')
print_inorder(node.right)
JavaScript
function printInorder(node) {
if (node === null)
return;
printInorder(node.left);
process.stdout.write(node.data + ' ');
printInorder(node.right);
}
What is the name of below recursive program?
C++
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);
}
C
#include <stdio.h>
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);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
fun(n - 1, aux_rod, to_rod, from_rod);
}
Java
public class TowerOfHanoi {
public static 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);
System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + to_rod);
fun(n - 1, aux_rod, to_rod, from_rod);
}
}
Python
def fun(n, from_rod, to_rod, aux_rod):
if n == 0:
return
fun(n - 1, from_rod, aux_rod, to_rod)
print(f"Move disk {n} from rod {from_rod} to rod {to_rod}")
fun(n - 1, aux_rod, to_rod, from_rod)
JavaScript
function fun(n, from_rod, to_rod, aux_rod) {
if (n === 0) {
return;
}
fun(n - 1, from_rod, aux_rod, to_rod);
console.log(`Move disk ${n} from rod ${from_rod} to rod ${to_rod}`);
fun(n - 1, aux_rod, to_rod, from_rod);
}
What is the output of the following program?
C++
#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);
}
C
#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);
}
Java
public class Main {
public static void print(int n, int j) {
if (j >= n)
return;
if (n - j > 0 && n - j >= j)
System.out.println(j + " " + (n - j));
print(n, j + 1);
}
public static void main(String[] args) {
int n = 8;
print(n, 1);
}
}
Python
def print_pairs(n, j):
if j >= n:
return
if n - j > 0 and n - j >= j:
print(j, n - j)
print_pairs(n, j + 1)
n = 8
print_pairs(n, 1)
JavaScript
function printPairs(n, j) {
if (j >= n)
return;
if (n - j > 0 && n - j >= j)
console.log(j + ' ' + (n - j));
printPairs(n, j + 1);
}
let n = 8;
printPairs(n, 1);
Consider the code fragment below :
C++
#include <iostream>
using namespace std;
void f(int n) {
if (n <= 1) {
cout << n;
} else {
f(n / 2);
cout << n % 2;
}
}
C
#include <stdio.h>
void f(int n) {
if (n <= 1) {
printf("%d", n);
} else {
f(n / 2);
printf("%d", n % 2);
}
}
Java
void f (int n)
{
if (n <=1) {
System.out.print(n);
}
else {
f (n/2);
System.out.print(n%2);
}
}
Python
def f(n):
if n <= 1:
print(n, end='')
else:
f(n // 2)
print(n % 2, end='')
JavaScript
function f(n) {
if (n <= 1) {
process.stdout.write(n.toString());
} else {
f(Math.floor(n / 2));
process.stdout.write((n % 2).toString());
}
}
What does f(173) print?
The function f is defined as follows:
C++
int f(int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n / 2);
else return f(3 * n - 1);
}
C
int f (int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n/2);
else return f(3n - 1);
}
Java
int f(int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n / 2);
else return f(3 * n - 1);
}
Python
def f(n):
if n <= 1:
return 1
elif n % 2 == 0:
return f(n // 2)
else:
return f(3 * n - 1)
JavaScript
function f(n) {
if (n <= 1) return 1;
else if (n % 2 === 0) return f(n / 2);
else return f(3 * n - 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?
There are 30 questions to complete.
Take a part in the ongoing discussion
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4