Java MCQ

Previous Java Multiple Choice Questions Next

Java Tricky Methods and Recursion Questions

1

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;
}

Correct Answer: A) 5

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.

2

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);
}

Correct Answer: A) 120

This is a recursive factorial function: 5! = 5 × 4 × 3 × 2 × 1 = 120. The base case (n==0) returns 1 to stop recursion.

3

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;
}

Correct Answer: B) 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.

4

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 + " ");
}

Correct Answer: C) 3 2 1 1 2 3

First print happens before recursion (descending: 3,2,1), then after recursion completes, second print happens in reverse order (ascending: 1,2,3).

5

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";
}

Correct Answer: A) Hello

Strings are immutable in Java. The method receives a copy of the reference, and reassigning it doesn't affect the original reference.

6

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);
}

Correct Answer: B) 5

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.

7

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--);
}

Correct Answer: C) StackOverflowError

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.

8

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);
}

Correct Answer: B) 13

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.

9

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);
}

Correct Answer: B) 8

This recursive function calculates base^exp. 2^3 = 2 × 2 × 2 = 8.

10

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);
  }
}

Correct Answer: D) 1 2 1 3 1 2 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".

Previous Java Multiple Choice Questions Next