Question 1
Predict output of following program
#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; }
#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; }
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)); } }
def fun(n): if n == 4: return n else: return 2 * fun(n + 1) print(fun(2))
function fun(n) { if (n === 4) return n; else return 2 * fun(n + 1); } console.log(fun(2));
4
8
16
Runtime Error
Question 2
Consider the following recursive function fun(x, y). What is the value of fun(4, 3)
int fun(int x, int y) { if (x == 0) return y; return fun(x - 1, x + y); }
int fun(int x, int y) { if (x == 0) return y; return fun(x - 1, x + y); }
int fun(int x, int y) { if (x == 0) return y; return fun(x - 1, x + y); }
def fun(x, y): if x == 0: return y return fun(x - 1, x + y)
function fun(x, y) { if (x === 0) return y; return fun(x - 1, x + y); }
13
12
9
10
Question 3
What does the following function print for n = 25?
#include <iostream> using namespace std; void fun(int n) { if (n == 0) return; cout << n % 2; fun(n / 2); }
void fun(int n) { if (n == 0) return; printf("%d", n%2); fun(n/2); }
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 } }
def fun(n): if n == 0: return print(n % 2, end='') fun(n // 2)
function fun(n) { if (n === 0) return; process.stdout.write((n % 2).toString()); fun(Math.floor(n / 2)); }
11001
10011
11111
00000
Question 4
What does the following function do?
int fun(int x, int y) { if (y == 0) return 0; return (x + fun(x, y-1)); }
x + y
x + x*y
x*y
xy
Question 5
What does fun2() do in general?
#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)); }
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)); }
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 } }
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))
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)); }
x*y
x+x*y
xy
yx
Question 6
Output of following program?
#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; }
#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; }
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); } }
def print_numbers(n): if n > 4000: return print(n, end=' ') print_numbers(2 * n) print(n, end=' ') print_numbers(1000)
function print(n) { if (n > 4000) return; process.stdout.write(n + ' '); print(2 * n); process.stdout.write(n + ' '); } print(1000);
1000 2000 4000
1000 2000 4000 4000 2000 1000
1000 2000 4000 2000 1000
1000 2000 2000 1000
Question 7
What does the following function do?
int fun(unsigned int n) { if (n == 0 || n == 1) return n; if (n % 3 != 0) return 0; return fun(n / 3); }
int fun(unsigned int n) { if (n == 0 || n == 1) return n; if (n%3 != 0) return 0; return fun(n/3); }
int fun(unsigned int n) { if (n == 0 || n == 1) return n; if (n % 3 != 0) return 0; return fun(n / 3); }
def fun(n): if n == 0 or n == 1: return n if n % 3 != 0: return 0 return fun(n // 3)
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
Question 8
Predict the output of following program
#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; }
#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; }
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)); } }
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))
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));
Stack Overflow
3
4
5
Question 9
Predict the output:
#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; }
#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; }
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); } }
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)
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
1 4 10
2 4 8
1 8 6
3 4 5
3 4 5
1 5 9
2 5 7
1 7 7
Question 10
Consider the following recursive C++ function that takes two arguments
unsigned int foo(unsigned int n, unsigned int r) { if (n > 0) return (n%r + foo (n/r, r )); else return 0; }
unsigned int foo(unsigned int n, unsigned int r) { if (n > 0) return (n % r + foo(n / r, r)); else return 0; }
public class Main { public static int foo(int n, int r) { if (n > 0) return (n % r + foo(n / r, r)); else return 0; } }
def foo(n, r): if n > 0: return (n % r + foo(n // r, r)) else: return 0
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)?
345
12
5
3
There are 30 questions to complete.