LearnFrenzy
Sign in Take a free test

Methods and Recursion · Exercise 2 of 4

Methods and Recursion Predict the Output

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Java program and choose what it prints. Every program was compiled and run on Java 21, so the answers are the real output.

Question 1 Easy

What is the output of the following Java program?

Main.java
public class Main {
    static void swap(int a, int b) {
        int t = a; a = b; b = t;
        System.out.print(a + " " + b + " | ");
    }

    public static void main(String[] args) {
        int x = 1, y = 2;
        swap(x, y);
        System.out.println(x + " " + y);
    }
}
A 2 1 | 1 2
B 2 1 | 2 1
C 1 2 | 1 2
D 1 2 | 2 1
Question page & discussion
Question 2 Easy

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    static void swap(int[] a, int i, int j) {
        int t = a[i]; a[i] = a[j]; a[j] = t;
    }

    public static void main(String[] args) {
        int[] nums = {10, 20, 30};
        swap(nums, 0, 2);
        System.out.println(Arrays.toString(nums));
    }
}
A [30, 20, 10]
B [10, 20, 30]
C [20, 10, 30]
D Compilation error
Question page & discussion
Question 3 Hard

What is the output of the following Java program?

Main.java
public class Main {
    static String f(int x)       { return "int"; }
    static String f(double x)    { return "double"; }
    static String f(Character x) { return "Character"; }
    static String f(Object x)    { return "Object"; }

    public static void main(String[] args) {
        char c = 'A';
        short s = 3;
        System.out.println(f(c) + " " + f(s) + " " + f(3.5f) + " " + f("text") + " " + f(Character.valueOf('B')));
    }
}
A int int double Object Character
B Character int double Object Character
C int double double Object Object
D Character Object double Object Character
Question page & discussion
Question 4 Hard

What is the output of the following Java program?

Main.java
public class Main {
    static String g(Integer x)   { return "Integer"; }
    static String g(int... xs)   { return "varargs"; }
    static String h(long x)      { return "long"; }
    static String h(Integer x)   { return "Integer"; }

    public static void main(String[] args) {
        System.out.println(g(5) + " " + g() + " " + g(1, 2) + " " + h(5) + " " + h(Integer.valueOf(5)));
    }
}
A Integer varargs varargs long Integer
B varargs varargs varargs long Integer
C Integer varargs varargs Integer Integer
D Integer Integer varargs long long
Question page & discussion
Question 5 Easy

What is the output of the following Java program?

Main.java
public class Main {
    static String info(String label, int... values) {
        int max = Integer.MIN_VALUE;
        for (int v : values) max = Math.max(max, v);
        return label + ":" + values.length + (values.length > 0 ? ":" + max : "");
    }

    public static void main(String[] args) {
        System.out.println(info("a") + " " + info("b", 4) + " " + info("c", 3, 9, 1) + " " + info("d", new int[]{7, 2}));
    }
}
A a:0 b:1:4 c:3:9 d:2:7
B a:1 b:1:4 c:3:9 d:1:7
C a:0 b:1:4 c:3:1 d:2:7
D Compilation error
Question page & discussion
Question 6 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static void f(int n) {
        if (n == 0) return;
        System.out.print(n + " ");
        f(n - 1);
        System.out.print(n + " ");
    }

    public static void main(String[] args) {
        f(3);
        System.out.println();
    }
}
A 3 2 1 1 2 3
B 3 2 1
C 1 2 3 3 2 1
D 3 2 1 3 2 1
Question page & discussion
Question 7 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static int calls = 0;

    static int f(int n) {
        calls++;
        if (n <= 1) return 1;
        return f(n - 1) + f(n - 2);
    }

    public static void main(String[] args) {
        System.out.println(f(6) + " " + calls);
    }
}
A 13 25
B 8 15
C 13 13
D 21 25
Question page & discussion
Question 8 Easy

What is the output of the following Java program?

Main.java
public class Main {
    static int calls = 0;

    static int twice(int x) {
        calls++;
        return 2 * x;
    }

    public static void main(String[] args) {
        int r = twice(twice(twice(3)));
        System.out.println(r + " " + calls);
    }
}
A 24 3
B 24 1
C 12 3
D 6 3
Question page & discussion
Question 9 Medium

What is the output of the following Java program?

Main.java
public class Main {
    static class Box { int value; Box(int v) { value = v; } }

    static void change(Box b) {
        b.value = 50;
        b = new Box(99);
        b.value = 100;
    }

    public static void main(String[] args) {
        Box box = new Box(1);
        change(box);
        System.out.println(box.value);
    }
}
A 50
B 100
C 1
D 99
Question page & discussion
Question 10 Easy

What is the output of the following Java program?

Main.java
public class Main {
    static String shout(String s) {
        s = s.toUpperCase() + "!";
        return s;
    }

    public static void main(String[] args) {
        String msg = "hello";
        String r = shout(msg);
        System.out.println(msg + " " + r);
    }
}
A hello HELLO!
B HELLO! HELLO!
C hello hello
D HELLO! hello
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links