Skip to main content

Java Topic Practice

Return Values

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

For practice use only.

Return Values MCQ Practice

Return Values Java practice with 15 questions, code tracing, and a worked explanation for every answer.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - Return Values: track the final printed value.

public class Main {
  public static int gap(int x, int y) {
      if (x > y) return x - y;
      return y - x;
  }
  public static void main(String[] args) {
    System.out.print(gap(18, 5));
  }
}

All 15 return values questions

Work through the interactive quiz above first. This is the full return values question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - Return Values: track the final printed value.

    public class Main {
      public static int gap(int x, int y) {
          if (x > y) return x - y;
          return y - x;
      }
      public static void main(String[] args) {
        System.out.print(gap(18, 5));
      }
    }
    • 23
    • The code does not compile.
    • 13
    • 0
    Show worked solution

    Correct answer: 13

    An early return exits the method immediately, so the later statement runs only when the condition was false.

  2. 2.Code Tracing 12 - Return Values: follow the variable updates.

    public class Main {
      public static int addOne(int v) {
          v = v + 1;
          return v;
      }
      public static void main(String[] args) {
        int r = addOne(26);
        System.out.print(r);
      }
    }
    • 26
    • 28
    • 0
    • 27
    Show worked solution

    Correct answer: 27

    The parameter is a copy, so the caller never sees the change, but the returned value carries the result back out.

  3. 3.Code Tracing 28 - Return Values: trace the branch and loop path.

    public class Main {
      public static int bump(int x) {
          return x + 10;
      }
      public static void main(String[] args) {
        int v = 38;
        bump(v);
        System.out.print(v);
      }
    }
    • 48
    • 10
    • 0
    • 38
    Show worked solution

    Correct answer: 38

    The returned value is discarded because it is never assigned, so the original variable is unchanged.

  4. 4.Code Tracing 39 - Return Values: watch the index changes.

    public class Main {
      public static int combine(int x, int y) {
          return x / y + x % y;
      }
      public static void main(String[] args) {
        System.out.print(combine(46, 5));
      }
    }
    • 10
    • 9
    • 1
    • 51
    Show worked solution

    Correct answer: 10

    Both operations use integer arithmetic, and the single return expression is evaluated before the value leaves the method.

  5. 5.Code Tracing 50 - Return Values: evaluate the state change step by step.

    public class Main {
      public static boolean isEven(int v) {
          return v % 2 == 0;
      }
      public static void main(String[] args) {
        System.out.print(isEven(54) + ":" + isEven(55));
      }
    }
    • false:false
    • true:false
    • false:true
    • true:true
    Show worked solution

    Correct answer: true:false

    A boolean-returning method hands back the comparison result directly, with no if statement needed.

  6. 6.What is printed by the following code segment?

    public static int f(int n)
    {
        if (n > 5) { return 1; }
        if (n > 2) { return 2; }
        return 3;
    }
    public static void main(String[] args)
    {
        System.out.print(f(6) + f(3) + f(1));
    }
    • 3
    • 6
    • 9
    • 5
    Show worked solution

    Correct answer: 6

    Each call returns at the first condition it satisfies, giving 1, 2, and 3.

  7. 7.What is printed by the following code segment?

    public static boolean p(int n)
    {
        for (int d = 2; d < n; d++)
        {
            if (n % d == 0) { return false; }
        }
        return n > 1;
    }
    public static void main(String[] args)
    {
        System.out.print(p(1) + " " + p(2) + " " + p(9));
    }
    • true true false
    • false true true
    • false true false
    • false false false
    Show worked solution

    Correct answer: false true false

    The early return rejects composites, and the final expression handles the value 1 correctly.

  8. 8.What is printed by the following code segment?

    public static String f(int n)
    {
        String s = "";
        while (n > 0)
        {
            s = n % 2 + s;
            n /= 2;
        }
        return s.equals("") ? "0" : s;
    }
    public static void main(String[] args)
    {
        System.out.print(f(5) + " " + f(0));
    }
    • 101
    • 5 0
    • 110 0
    • 101 0
    Show worked solution

    Correct answer: 101 0

    The conditional at the end supplies a value for the case where the loop never runs.

  9. 9.What is printed by the following code segment?

    public static int f()
    {
        int n = 0;
        for (int i = 0; i < 3; i++)
        {
            n++;
            if (n == 2) { return n; }
        }
        return -1;
    }
    public static void main(String[] args)
    {
        System.out.print(f());
    }
    • 2
    • 3
    • -1
    • 1
    Show worked solution

    Correct answer: 2

    A return exits the method immediately, so the loop never completes.

  10. 10.What is printed by the following code segment?

    public static int f(int n)
    {
        int r = 0;
        while (n > 0)
        {
            r += n % 10;
            n /= 10;
        }
        return r > 9 ? f(r) : r;
    }
    public static void main(String[] args)
    {
        System.out.print(f(9875));
    }
    • 29
    • 2
    • 11
    • 9
    Show worked solution

    Correct answer: 2

    The digit sum is 29, which is still more than one digit, so the method calls itself and returns 11 then 2.

  11. 11.What is printed by the following code segment?

    public static int max(int[] a)
    {
        if (a.length == 0) { return -1; }
        int m = a[0];
        for (int v : a)
        {
            if (v > m) { m = v; }
        }
        return m;
    }
    public static void main(String[] args)
    {
        System.out.print(max(new int[] {3, 9, 2}));
        System.out.print(" " + max(new int[] {}));
    }
    • 9 0
    • 3 -1
    • 9 -1
    • 9 3
    Show worked solution

    Correct answer: 9 -1

    The guard supplies a sentinel for the empty case before any element is read.

  12. 12.What is printed by the following code segment?

    public static int f(int n)
    {
        n++;
        return n++;
    }
    public static void main(String[] args)
    {
        System.out.print(f(1));
    }
    • 3
    • 1
    • 4
    • 2
    Show worked solution

    Correct answer: 2

    The postfix increment returns the value before the increment, so the extra step is lost.

  13. 13.Why does the method below fail to compile?

    public static int sign(int n)
    {
        if (n > 0) { return 1; }
        else if (n < 0) { return -1; }
    }
    • The path where n is 0 reaches the end without returning a value.
    • A method cannot have more than one return statement.
    • The return type should be void.
    • An else clause is required after every else if.
    Show worked solution

    Correct answer: The path where n is 0 reaches the end without returning a value.

    Every path through a value-returning method must return, and the compiler does not reason about which paths are reachable in practice.

  14. 14.What is printed by the following code segment?

    public static int[] f(int n)
    {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) { a[i] = i * 2; }
        return a;
    }
    public static void main(String[] args)
    {
        System.out.print(f(4)[3]);
    }
    • 8
    • 6
    • 3
    • 4
    Show worked solution

    Correct answer: 6

    A method may return an array, and the result can be indexed directly.

  15. 15.What is printed by the following code segment?

    public static int f(int a, int b)
    {
        return a > b ? a : f(b, a);
    }
    public static void main(String[] args)
    {
        System.out.print(f(2, 7));
    }
    • 2
    • 9
    • 7
    • 5
    Show worked solution

    Correct answer: 7

    Swapping the arguments once puts the larger value first, so the second call returns it.