Java MCQ
Java Tricky Methods and Recursion Questions
What is the output?
public static void main(String[] args) {
int x = 5;
modify(x);
System.out.println(x);
}
static void modify(int num) {
num = 10;
}
int x = 5;
modify(x);
System.out.println(x);
}
static void modify(int num) {
num = 10;
}
Java is pass-by-value. The method receives a copy of the primitive value, so changes inside the method don't affect the original variable.
What will be printed?
public static void main(String[] args) {
int result = calculate(5);
System.out.println(result);
}
static int calculate(int n) {
if(n == 0) return 1;
return n * calculate(n - 1);
}
int result = calculate(5);
System.out.println(result);
}
static int calculate(int n) {
if(n == 0) return 1;
return n * calculate(n - 1);
}
This is a recursive factorial function: 5! = 5 × 4 × 3 × 2 × 1 = 120. The base case (n==0) returns 1 to stop recursion.
What is the output?
public static void main(String[] args) {
int[] arr = {1, 2, 3};
modifyArray(arr);
System.out.println(arr[0]);
}
static void modifyArray(int[] array) {
array[0] = 10;
}
int[] arr = {1, 2, 3};
modifyArray(arr);
System.out.println(arr[0]);
}
static void modifyArray(int[] array) {
array[0] = 10;
}
While Java is pass-by-value, for objects (including arrays), the value passed is the reference. So changes to the array elements are reflected in the original array.
What will be printed?
public static void main(String[] args) {
printNumbers(3);
}
static void printNumbers(int n) {
if(n <= 0) return;
System.out.print(n + " ");
printNumbers(n - 1);
System.out.print(n + " ");
}
printNumbers(3);
}
static void printNumbers(int n) {
if(n <= 0) return;
System.out.print(n + " ");
printNumbers(n - 1);
System.out.print(n + " ");
}
First print happens before recursion (descending: 3,2,1), then after recursion completes, second print happens in reverse order (ascending: 1,2,3).
What is the output?
public static void main(String[] args) {
String str = "Hello";
modifyString(str);
System.out.println(str);
}
static void modifyString(String s) {
s = "World";
}
String str = "Hello";
modifyString(str);
System.out.println(str);
}
static void modifyString(String s) {
s = "World";
}
Strings are immutable in Java. The method receives a copy of the reference, and reassigning it doesn't affect the original reference.
What will be printed?
public static void main(String[] args) {
System.out.println(fibonacci(5));
}
static int fibonacci(int n) {
if(n <= 1) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
System.out.println(fibonacci(5));
}
static int fibonacci(int n) {
if(n <= 1) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
Fibonacci sequence: F(0)=0, F(1)=1, F(2)=1, F(3)=2, F(4)=3, F(5)=5. The method correctly calculates the 5th Fibonacci number.
What is the output?
public static void main(String[] args) {
test(5);
}
static void test(int n) {
if(n == 0) return;
System.out.print(n + " ");
test(n--);
}
test(5);
}
static void test(int n) {
if(n == 0) return;
System.out.print(n + " ");
test(n--);
}
Post-decrement (n--) passes the original value (5) to the recursive call, then decrements. This creates infinite recursion since n never changes in the recursive call.
What will be printed?
public static void main(String[] args) {
System.out.println(mystery(7));
}
static int mystery(int n) {
if(n < 2) return n;
return mystery(n-1) + mystery(n-2);
}
System.out.println(mystery(7));
}
static int mystery(int n) {
if(n < 2) return n;
return mystery(n-1) + mystery(n-2);
}
This is the Fibonacci sequence: F(0)=0, F(1)=1, F(2)=1, F(3)=2, F(4)=3, F(5)=5, F(6)=8, F(7)=13.
What is the output?
public static void main(String[] args) {
System.out.println(power(2, 3));
}
static int power(int base, int exp) {
if(exp == 0) return 1;
return base * power(base, exp - 1);
}
System.out.println(power(2, 3));
}
static int power(int base, int exp) {
if(exp == 0) return 1;
return base * power(base, exp - 1);
}
This recursive function calculates base^exp. 2^3 = 2 × 2 × 2 = 8.
What will be printed?
public static void main(String[] args) {
recursive(3);
}
static void recursive(int n) {
if(n > 0) {
recursive(n-1);
System.out.print(n + " ");
recursive(n-1);
}
}
recursive(3);
}
static void recursive(int n) {
if(n > 0) {
recursive(n-1);
System.out.print(n + " ");
recursive(n-1);
}
}
This creates a binary recursion pattern. For n=3: recursive(2) prints "1 2 1", then prints "3", then recursive(2) again prints "1 2 1".