1. What’s the output of the following code ?
Java
1int doSomething(int a, int b)
2{
3 if (b==1)
4 return a;
5 else
6 return a + doSomething(a,b-1);
7}
8
9 doSomething(2,3);
a)4
b)2
c)3
d)6
Ans d
2. Select the correct output.
Java
1 int rec(int num){
2 return (num) ? num%10 +
rec(num/10):0;
3
}
4
5
main(){
6
7
8 printf("%d",rec(4567));
}
a) 4
b) 12
c) 22
d) 21
Ans. c
3. Choose correct option.
Java
1 int something(int number)
2 {
3 if(number <= 0)
4 return 1;
5 else
6 return number * something(number-
1);
7
8
}
9
10
something(4);
a) 12
b) 24
c) 1
d) 0
Ans.b
4. What will be the output
int func(int a, int b){
if(b==0)
return 0;
if(b==1)
return a;
return a + func(a,b-1);
}
what will be the output of func(3,8) .
a) 11
b) 24
c) 22
d) 21
Ans.b
5. What will be the output
void print(int n)
{
if (n == 0)
return;
printf("%d", n%2);
print(n/2);
}
What will be the output of print(12).
a) 0011
b) 1100
c) 1001
d) 1000
Ans.a
6. What will be the output
int sum(int n) {
if (n==0)
return n;
else
return n + sum(n-1);
}
What will be the output of sum(8).
a) 40
b) 36
c) 8
d) 15
Ans.b
7. What will be the output of the following C code?
#include<stdio.h>
main()
{
int n;
n=f1(4);
printf("%d",n);
}
f1(int x)
{
int b;
if(x==1)
return 1;
else
b=x*f1(x-1);
return b;
}
a) 24
b) 4
c) 12
d) 10
Ans a
8. What will be the output of the following C code?
#include<stdio.h>
main()
{
int n,i;
n=f(6);
printf("%d",n);
}
f(int x)
{
if(x==2)
return 2;
else
{
printf("+");
f(x-1);
}
}
a) ++++2
b) +++++2
c) +++++
d) 2
Ans. A
9. How many times is ‘a’ printed when the following C code is
executed?
#include<stdio.h>
main()
{
int a;
a=f1(10);
printf("%d",a);
}
f1(int b)
{
if(b==0)
return 0;
else
{
printf("a");
f1(b--);
}
}
a) 9 times
b) 10 times
c) 0 times
d) Infinite number of times
Ans.d
10. What will be the output of the following C code?
#include<stdio.h>
main()
{
int n=10;
int f(int n);
printf("%d",f(n));
}
int f(int n)
{
if(n>0)
return(n+f(n-2));
}
a) 10
b) 80
c) 30
d) Error
Ans. c
11. What will be the output of the following C code?
#include<stdio.h>
int main()
{
printf("Hello");
main();
return 0;
}
a) Hello is printed once
b) Hello infinite number of times
c) Hello is not printed at all
d) 0 is returned
Ans.b
12. What will be the output of the following C code if the
input given to the code shown below is “skillbout”?
#include<stdio.h>
#define NL '\n'
main()
{
void f(void);
printf("enter the word\n");
f();
}
void f(void)
{
char c;
if((c=getchar())!=NL)
{
f();
printf("%c",c);
}
return;
}
a) skillbout
b) infinite loop
c) tuoblliks
d) likstuobl
Ans. c
[Link] output of following program
#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;
}
a. 4
b. 8
c. 16
d. Runtime error
Ans. C
[Link] 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);
}
a. 13
b. 12
c. 9
d. 10
Ans. A
[Link] does the following function do?
int fun(int x, int y)
{
if (y == 0) return 0;
return (x + fun(x, y-1));
}
a. x+y
b. x+x*y
c. x*y
d. x^y
Ans c
16. Output of following program?
#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;
}
a. 1000 2000 4000
b. 1000 2000 4000 4000 2000 1000
c. 1000 2000 4000 2000 1000
d. 1000 2000 2000 1000
Ans. B
[Link] 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);
}
a. It returns 1 when n is a multiple of 3, otherwise returns 0
b. It returns 1 when n is a power of 3, otherwise returns 0
c. It returns 0 when n is a multiple of 3, otherwise returns 1
d. It returns 0 when n is a power of 3, otherwise returns 1
Ans. B
[Link] will be the Output of following program?
#include <stdio.h>
int fun(int n, int *f_p)
{
int t, f;
if (n <= 1)
{
*f_p = 1;
return 1;
}
t = fun(n- 1,f_p);
f = t+ * f_p;
*f_p = t;
return f;
}
int main()
{
int x = 15;
printf (" %d n", fun(5, &x));
return 0;
}
a. 6
b. 8
c. 14
d. 15
Ans. B
[Link] the following C function:
int f(int n)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
}
The value return by f(1) is
a. 5
b. 6
c. 7
d. 8
Ans c
20. What will be the output
void my_recursive_function()
{
my_recursive_function();
}
int main()
{
my_recursive_function();
return 0;
}
a) The code will be executed successfully and no output will be
generated
b) The code will be executed successfully and random output will be
generated
c) The code will show a compile time error
d) The code will run for some time and stop when the stack overflows
Ans .d
21. What is the output of the following code?
void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) 10
b) 1
c) 10 9 8 … 1 0
d) 10 9 8 … 1
Ans. D
22. What is the base case for the following code?
void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) return
b) printf(“%d “, n)
c) if(n == 0)
d) my_recursive_function(n-1)
Ans. C
23. How many times is the recursive function called, when
the following code is executed?
void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) 9
b) 10
c) 11
d) 12
Ans. C
25. What does the following recursive code do?
void my_recursive_function(int n)
{
if(n == 0)
return;
my_recursive_function(n-1);
printf("%d ",n);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) Prints the numbers from 10 to 1
b) Prints the numbers from 10 to 0
c) Prints the numbers from 1 to 10
d) Prints the numbers from 0 to 10
Ans c
26. will be the output of the following code?
int cnt=0;
void my_recursive_function(int n)
{
if(n == 0)
return;
cnt++;
my_recursive_function(n/10);
}
int main()
{
my_recursive_function(123456789);
printf("%d",cnt);
return 0;
}
a) 123456789
b) 10
c) 0
d) 9
Ans d
27. What will be the output of the following code?
void my_recursive_function(int n)
{
if(n == 0)
{
printf("False");
return;
}
if(n == 1)
{
printf("True");
return;
}
if(n%2==0)
my_recursive_function(n/2);
else
{
printf("False");
return;
}
}
int main()
{
my_recursive_function(100);
return 0;
}
a) True
b) False
Ans. B
28. What is the output of the following code?
int cnt = 0;
void my_recursive_function(char *s, int i)
{
if(s[i] == '\0')
return;
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] ==
'o' || s[i] == 'u')
cnt++;
my_recursive_function(s,i+1);
}
int main()
{
my_recursive_function("thisisrecursion",0);
printf("%d",cnt);
return 0;
}
a) 6
b) 9
c) 5
d) 10
Ans. a
29. What is the output of the following code?
void my_recursive_function(int *arr, int val, int idx, int
len)
{
if(idx == len)
{
printf("-1");
return ;
}
if(arr[idx] == val)
{
printf("%d",idx);
return;
}
my_recursive_function(arr,val,idx+1,len);
}
int main()
{
int array[10] = {7, 6, 4, 3, 2, 1, 9, 5, 0, 8};
int value = 2;
int len = 10;
my_recursive_function(array, value, 0, len);
return 0;
}
a) 3
b) 4
c) 5
d) 6
Ans. b
30. Consider the following iterative implementation to find the
factorial of a number. Which of the lines should be inserted to
complete the below code?
int main()
{
int n = 6, i;
int fact = 1;
for(i=1;i<=n;i++)
_________;
printf("%d",fact);
return 0;
}
a) fact = fact + i
b) fact = fact * i
c) i = i * fact
d) i = i + fact
And. B
[Link] the following recursive implementation to find the
factorial of a number. Which of the lines should be inserted to
complete the below code?
int fact(int n)
{
if(_________)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) n = 0
b) n != 0
c) n == 0
d) n == 1
Ans. C
[Link] time complexity of the following recursive implementation to find the
factorial of a number is ________
int fact(int n)
{
if(_________)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Ans. B
33. Consider the following recursive implementation to find the
factorial of a number. Which of the lines is the base case?
int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) return 1
b) return n * fact(n-1)
c) if(n == 0)
d) if(n == 1)
Ans. C
34. What is the output of the following code?
int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 0;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) 0
b) 1
c) 2
d) 3
Ans. B
35. What is the output of the following code?
int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 1;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) 0
b) 1
c) 2
d) 3
Ans b
36. What is the output of the following code?
int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) 24
b) 120
c) 720
d) 1
Ans. B
37. Consider the following iterative solution to find the sum of first
n natural numbers:
#include<stdio.h>
int get_sum(int n)
{
int sm = 0, i;
for(i = 1; i <= n; i++)
________;
return sm;
}
int main()
{
int n = 10;
int ans = get_sum(n);
printf("%d",ans);
return 0;
}
Which of the following lines completes the above code?
a) sm = i
b) sm += i
c) i = sm
d) i += sm
Ans. B
38. What is the output of the following code?
#include<stdio.h>
int get_sum(int n)
{
int sm, i;
for(i = 1; i <= n; i++)
sm += i;
return sm;
}
int main()
{
int n = 10;
int ans = get_sum(n);
printf("%d",ans);
return 0;
}
a) 55
b) 45
c) 35
d) Depends on compiler
Ans d
39. What is the output of the following code?
#include<stdio.h>
int recursive_sum(int n)
{
if(n == 0)
return 0;
return n + recursive_sum(n - 1);
}
int main()
{
int n = -4;
int ans = recursive_sum(n);
printf("%d",ans);
return 0;
}
a) 0
b) -10
c) 1
d) runtime error
Ans. D
[Link] the following iterative implementation to find the
sum of digits of a number:
#include<stdio.h>
int sum_of_digits(int n)
{
int sm = 0;
while(n != 0)
{
_________;
n /= 10;
}
return sm;
}
int main()
{
int n = 1234;
int ans = sum_of_digits(n);
printf("%d",ans);
return 0;
}
Which of the following lines should be inserted to complete the
above code?
a) sm += n
b) sm += n%10
c) sm += n-10
d) sm += n/10
Ans b
41. What is the output of the following code?
#include<stdio.h>
int cnt =0;
int my_function(int n, int sm)
{
int i, tmp_sm;
for(i=1;i<=n;i++)
{
tmp_sm = recursive_sum_of_digits(i);
if(tmp_sm == sm)
cnt++;
}
return cnt;
}
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 = 20, sum = 3;
int ans = my_function(n,sum);
printf("%d",ans);
return 0;
}
a) 0
b) 1
c) 2
d) 3
42. Consider the following iterative implementation used to
reverse a string:
How_to_Install_TWRP_Recovery_on_any_Android_phone
#include<stdio.h>
#include<string.h>
void reverse_string(char *s)
{
int len = strlen(s);
int i,j;
i=0;
j=len-1;
while(______)
{
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
}
Which of the following lines should be inserted to complete the above
code?
a) i > j
b) i < len
c) j > 0
d) i < j
Ans d
43. is the output of the following code?
#include<stdio.h>
#include<string.h>
void reverse_string(char *s)
{
int len = strlen(s);
int i,j;
i=0;
j=len-1;
while(i < j)
{
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
}
int main()
{
char s[100] = "reverse";
reverse_string(s);
printf("%s",s);
return 0;
}
a) ersevre
b) esrever
c) eserver
d) eresevr
Ans b
[Link] does the following code do?
#include<stdio.h>
#include<string.h>
void reverse_string(char *s)
{
int len = strlen(s);
int i,j;
i=0;
j=len-1;
while(i < j)
{
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
}
int main()
{
char s[100] = "abcdefg";
char t[100];
strcpy(t,s);
reverse_string(s);
if(strcmp(t,s) == 0)
printf("Yes");
else
printf("No");
return 0;
}
a) Copies a string to another string
b) Compares two strings
c) Reverses a string
d) Checks if a string is a palindrome
Ans. D
45. Consider the following iterative code used to convert a decimal
number to its equivalent binary:
Java Tricky Program 16 - Autoboxing, Inheritance and Overriding
#include<stdio.h>
void dec_to_bin(int n)
{
int arr[31],len = 0,i;
if(n == 0)
{
arr[0] = 0;
len = 1;
}
while(n != 0)
{
arr[len++] = n % 2;
_______;
}
for(i=len-1; i>=0; i--)
printf("%d",arr[i]);
}
int main()
{
int n = 10;
dec_to_bin(n);
return 0;
}
Which of the following lines should be inserted to complete the above
code?
a) n–
b) n /= 2
c) n /= 10
d) n++
Ans. B
46. What is the output of the following code?
#include<stdio.h>
int arr[31], len = 0;
void recursive_dec_to_bin(int n)
{
if(n == 0 && len == 0)
{
arr[len++] = 0;
return;
}
if(n == 0)
return;
arr[len++] = n % 2;
recursive_dec_to_bin(n/2);
}
int main()
{
int n = -100,i;
recursive_dec_to_bin(n);
for(i=len-1; i>=0; i--)
printf("%d",arr[i]);
return 0;
}
a) -1100100
b) 1100100
c) 2’s complement of 1100100
d) Garbage value
Ans. D
47. What is the output of the following code?
#include<stdio.h>
int get_len(char *s)
{
int len = 0;
while(s[len] != '\0')
len++;
return len;
}
int main()
{
char *s = "";
int len = get_len(s);
printf("%d",len);
return 0;
}
a) 0
b) 1
c) Runtime error
d) Garbage value
Ans. A
48. Which of the following correctly represents the function to
insert elements at the bottom of stack?
a)
int BottomInsert(int x)
{
if([Link]()!=0) [Link](x);
else
{
int a = [Link]();
[Link]();
BottomInsert(x);
[Link](a);
}
}
b)
int BottomInsert(int x)
{
if([Link]()==0) [Link](x);
else
{
int a = [Link]();
[Link]();
[Link](a);
BottomInsert(x);
}
}
c)
int BottomInsert(int x)
{
if([Link]()==0) [Link](x);
else
{
int a = [Link]();
[Link]();
BottomInsert(x);
[Link](a);
}
}
d)
int BottomInsert(int x)
{
if([Link]()==0) [Link](x);
else
{
[Link]();
int a = [Link]();
BottomInsert(x);
[Link](a);
}
}
Ans. C
49. Consider the following code snippet to find the smallest
element in an array:
int get_min_element(int *arr, int n)
{
int i, min_element = arr[0];
for(i = 1; i < n; i++)
if(_______)
min_element = arr[i];
return min_element;
}
Which of the following lines should be inserted to complete the above
code?
a) arr[i] > min_element
b) arr[i] < min_element
c) arr[i] == min_element
d) arr[i] != min_element
Ans. B
[Link] the following code snippet to find the largest element
in a linked list:
struct Node{
int val;
struct Node *next;
}*head;
int get_max()
{
struct Node* temp = head->next;
int max_num = temp->val;
while(______)
{
if(temp->val > max_num)
max_num = temp->val;
temp = temp->next;
}
return max_num;
}
Which of the following lines should be inserted to complete the
above code?
a) temp->next != 0
b) temp != 0
c) head->next != 0
d) head != 0
Ans. b