Java Interview Questions Based on Recursion Set 1
Q1) options describes best the method call : method1(numbers, val, numbers.kength)? It is assumed that numbers and val have been declared and initialized.

public static int method( int[] nums, int v, int numVals) {

int k=0;

if(v == nums[numVals1]) {

k=1;

}

if(numVals == 1) {

return k;

}else {

return k+method(nums, v, numVals1);

}

}
Options :
 Returns the number of elements in numbers that are not equal to val
 Returns the number of elements in numbers that are equal to val
 Returns the value of last element
 Returns the index of last element
 Returns the index of previous element of last element
Q2)
Consider the following method and answer which of the following options describes best the method call : method1(anyNumber)? It is assumed that anyNumber > 0.

public static int method1(int num) {

if(num <10)

{

return 1;

}else {

return 1+ method1(num/10);

}

}
Options :
 Returns 1
 Returns the sum of digits
 Returns sum of 1 number of digits time
 Returns decimal representation of number
Q3)
Consider the following method and answer what will be the output of method call : method1(anyNumber)? It is assumed that anyNumber > 0.

public static int method1(int n) {

if(n==1)

return 5;

else

return 5 * method1(n1);

}
Options :
 5 time n
 5 to the power of n
 5
 n
Q4)
Consider the following method and answer How many times the method will be called to evaluate : method1(anyNumber)? It is assumed that anyNumber > 0.

public static int method1(int n) {

if(n==1)

return 5;

else

return 5 * method1(n1);

}
Options :
 5 time n
 5 to the power of n
 5
 n
Q5)
Consider the following method and answer what the method call : method1(anyNumber) will evaluate to ? It is assumed that anyNumber > 0.

public static int method1(int n) {

if(n == 1)

return 1;

else

return n*method1(n1);

}
Options :
 n time n
 n to the power of anyNumber
 anyNumber to the power of n
 factorial of n
To Practice the above questions, get the answer and watch video explanations please choose any below option.
Free Android App  Ad Free Premium Course 

You must log in to post a comment.