Predict output of following program
C++
#include <iostream>
int fun ( int n )
{
if ( n == 4 )
return n ;
else return 2 * fun ( n + 1 );
}
int main ()
{
std :: cout << fun ( 2 );
return 0 ;
}
C
#include <stdio.h>
int fun ( int n )
{
if ( n == 4 )
return n ;
else return 2 * fun ( n + 1 );
}
int main ()
{
printf ( "%d" , fun ( 2 ));
return 0 ;
}
Java
public class Main {
public static int fun ( int n ) {
if ( n == 4 )
return n ;
else return 2 * fun ( n + 1 );
}
public static void main ( String [] args ) {
System . out . println ( fun ( 2 ));
}
}
Python
def fun ( n ):
if n == 4 :
return n
else :
return 2 * fun ( n + 1 )
print ( fun ( 2 ))
JavaScript
function fun ( n ) {
if ( n === 4 )
return n ;
else return 2 * fun ( n + 1 );
}
console . log ( fun ( 2 ));
Consider the following recursive function fun(x, y). What is the value of fun(4, 3)
C++
int fun ( int x , int y ) {
if ( x == 0 )
return y ;
return fun ( x - 1 , x + y );
}
C
int fun ( int x , int y )
{
if ( x == 0 )
return y ;
return fun ( x - 1 , x + y );
}
Java
int fun ( int x , int y ) {
if ( x == 0 )
return y ;
return fun ( x - 1 , x + y );
}
Python
def fun ( x , y ):
if x == 0 :
return y
return fun ( x - 1 , x + y )
JavaScript
function fun ( x , y ) {
if ( x === 0 )
return y ;
return fun ( x - 1 , x + y );
}
What does the following function print for n = 25?
C++
#include <iostream>
using namespace std ;
void fun ( int n ) {
if ( n == 0 )
return ;
cout << n % 2 ;
fun ( n / 2 );
}
C
void fun ( int n )
{
if ( n == 0 )
return ;
printf ( "%d" , n % 2 );
fun ( n / 2 );
}
Java
public class Main {
public static void fun ( int n ) {
if ( n == 0 )
return ;
System . out . print ( n % 2 );
fun ( n / 2 );
}
public static void main ( String [] args ) {
fun ( 10 ); // Example call
}
}
Python
def fun ( n ):
if n == 0 :
return
print ( n % 2 , end = '' )
fun ( n // 2 )
JavaScript
function fun ( n ) {
if ( n === 0 )
return ;
process . stdout . write (( n % 2 ). toString ());
fun ( Math . floor ( n / 2 ));
}
What does the following function do?
C
int fun ( int x , int y )
{
if ( y == 0 ) return 0 ;
return ( x + fun ( x , y -1 ));
}
What does fun2() do in general?
C++
#include <iostream>
int fun ( int x , int y ) {
if ( y == 0 ) return 0 ;
return ( x + fun ( x , y -1 ));
}
int fun2 ( int a , int b ) {
if ( b == 0 ) return 1 ;
return fun ( a , fun2 ( a , b -1 ));
}
C
int fun ( int x , int y )
{
if ( y == 0 ) return 0 ;
return ( x + fun ( x , y -1 ));
}
int fun2 ( int a , int b )
{
if ( b == 0 ) return 1 ;
return fun ( a , fun2 ( a , b -1 ));
}
Java
public class Main {
public static int fun ( int x , int y ) {
if ( y == 0 ) return 0 ;
return ( x + fun ( x , y - 1 ));
}
public static int fun2 ( int a , int b ) {
if ( b == 0 ) return 1 ;
return fun ( a , fun2 ( a , b - 1 ));
}
public static void main ( String [] args ) {
// Example usage
}
}
Python
def fun ( x , y ):
if y == 0 :
return 0
return x + fun ( x , y - 1 )
def fun2 ( a , b ):
if b == 0 :
return 1
return fun ( a , fun2 ( a , b - 1 ))
JavaScript
function fun ( x , y ) {
if ( y === 0 ) return 0 ;
return x + fun ( x , y - 1 );
}
function fun2 ( a , b ) {
if ( b === 0 ) return 1 ;
return fun ( a , fun2 ( a , b - 1 ));
}
Output of following program?
C++
#include <iostream>
using namespace std ;
void print ( int n ) {
if ( n > 4000 )
return ;
cout << n << " " ;
print ( 2 * n );
cout << n << " " ;
}
int main () {
print ( 1000 );
cin . get ();
return 0 ;
}
C
#include <stdio.h>
void print ( int n )
{
if ( n > 4000 )
return ;
printf ( "%d " , n );
print ( 2 * n );
printf ( "%d " , n );
}
int main ()
{
print ( 1000 );
getchar ();
return 0 ;
}
Java
public class Main {
public static void print ( int n ) {
if ( n > 4000 )
return ;
System . out . print ( n + " " );
print ( 2 * n );
System . out . print ( n + " " );
}
public static void main ( String [] args ) {
print ( 1000 );
}
}
Python
def print_numbers ( n ):
if n > 4000 :
return
print ( n , end = ' ' )
print_numbers ( 2 * n )
print ( n , end = ' ' )
print_numbers ( 1000 )
JavaScript
function print ( n ) {
if ( n > 4000 )
return ;
process . stdout . write ( n + ' ' );
print ( 2 * n );
process . stdout . write ( n + ' ' );
}
print ( 1000 );
1000 2000 4000 4000 2000 1000
What does the following function do?
C++
int fun ( unsigned int n ) {
if ( n == 0 || n == 1 )
return n ;
if ( n % 3 != 0 )
return 0 ;
return fun ( n / 3 );
}
C
int fun ( unsigned int n )
{
if ( n == 0 || n == 1 )
return n ;
if ( n % 3 != 0 )
return 0 ;
return fun ( n / 3 );
}
Java
int fun ( unsigned int n ) {
if ( n == 0 || n == 1 )
return n ;
if ( n % 3 != 0 )
return 0 ;
return fun ( n / 3 );
}
Python
def fun ( n ):
if n == 0 or n == 1 :
return n
if n % 3 != 0 :
return 0
return fun ( n // 3 )
JavaScript
function fun ( n ) {
if ( n === 0 || n === 1 )
return n ;
if ( n % 3 !== 0 )
return 0 ;
return fun ( n / 3 );
}
It returns 1 when n is a multiple of 3, otherwise returns 0
It returns 1 when n is a power of 3, otherwise returns 0
It returns 0 when n is a multiple of 3, otherwise returns 1
It returns 0 when n is a power of 3, otherwise returns 1
Predict the output of following program
C++
#include <iostream>
using namespace std ;
int f ( int n ) {
if ( n <= 1 )
return 1 ;
if ( n % 2 == 0 )
return f ( n / 2 );
return f ( n / 2 ) + f ( n / 2 + 1 );
}
int main () {
cout << f ( 11 );
return 0 ;
}
C
#include <stdio.h>
int f ( int n )
{
if ( n <= 1 )
return 1 ;
if ( n % 2 == 0 )
return f ( n / 2 );
return f ( n / 2 ) + f ( n / 2 + 1 );
}
int main ()
{
printf ( "%d" , f ( 11 ));
return 0 ;
}
Java
public class Main {
public static int f ( int n ) {
if ( n <= 1 )
return 1 ;
if ( n % 2 == 0 )
return f ( n / 2 );
return f ( n / 2 ) + f ( n / 2 + 1 );
}
public static void main ( String [] args ) {
System . out . println ( f ( 11 ));
}
}
Python
def f ( n ):
if n <= 1 :
return 1
if n % 2 == 0 :
return f ( n // 2 )
return f ( n // 2 ) + f ( n // 2 + 1 )
print ( f ( 11 ))
JavaScript
function f ( n ) {
if ( n <= 1 )
return 1 ;
if ( n % 2 === 0 )
return f ( n / 2 );
return f ( Math . floor ( n / 2 )) + f ( Math . floor ( n / 2 ) + 1 );
}
console . log ( f ( 11 ));
Predict the output:
C++
#include <iostream>
using namespace std ;
void crazy ( int n , int a , int b )
{
if ( n <= 0 )
return ;
crazy ( n - 1 , a , b + n );
cout << n << " " << a << " " << b << endl ;
crazy ( n - 1 , b , a + n );
}
int main ()
{
crazy ( 3 , 4 , 5 );
return 0 ;
}
C
#include <stdio.h>
void crazy ( int n , int a , int b )
{
if ( n <= 0 )
return ;
crazy ( n - 1 , a , b + n );
printf ( "%d %d %d \n " , n , a , b );
crazy ( n -1 , b , a + n );
}
int main ()
{
crazy ( 3 , 4 , 5 );
return 0 ;
}
Java
public class Crazy {
public static void crazy ( int n , int a , int b ) {
if ( n <= 0 )
return ;
crazy ( n - 1 , a , b + n );
System . out . println ( n + " " + a + " " + b );
crazy ( n - 1 , b , a + n );
}
public static void main ( String [] args ) {
crazy ( 3 , 4 , 5 );
}
}
Python
def crazy ( n , a , b ):
if n <= 0 :
return
crazy ( n - 1 , a , b + n )
print ( n , a , b )
crazy ( n - 1 , b , a + n )
crazy ( 3 , 4 , 5 )
JavaScript
function crazy ( n , a , b ) {
if ( n <= 0 )
return ;
crazy ( n - 1 , a , b + n );
console . log ( n , a , b );
crazy ( n - 1 , b , a + n );
}
crazy ( 3 , 4 , 5 );
1 4 10 2 4 8 1 8 6 3 4 5 1 5 9 2 5 7 1 7 7 3 4 5 1 4 10 2 4 8 1 8 6 1 5 9 2 5 7 1 7 7 Consider the following recursive C++ function that takes two arguments
C++
unsigned int foo ( unsigned int n , unsigned int r ) {
if ( n > 0 ) return ( n % r + foo ( n / r , r ));
else return 0 ;
}
C
unsigned int foo ( unsigned int n , unsigned int r ) {
if ( n > 0 ) return ( n % r + foo ( n / r , r ));
else return 0 ;
}
Java
public class Main {
public static int foo ( int n , int r ) {
if ( n > 0 ) return ( n % r + foo ( n / r , r ));
else return 0 ;
}
}
Python
def foo ( n , r ):
if n > 0 :
return ( n % r + foo ( n // r , r ))
else :
return 0
JavaScript
function foo ( n , r ) {
if ( n > 0 ) return ( n % r + foo ( Math . floor ( n / r ), r ));
else return 0 ;
}
What is the return value of the function foo when it is called foo(345, 10)?
Master DSA and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
Step into the Three 90 Challenge – 90 days to push limits, break barriers, and become the best version of yourself! Don't miss your chance to upskill and get 90% refund . What more motivation do you need? Start the challenge right away!
There are 30 questions to complete.
Take a part in the ongoing discussion View All Discussion