0
0
Javaprogramming~15 mins

Return values in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
What is the output of this Java method call?

Consider the following Java method:

public static int multiplyByTwo(int x) {
    return x * 2;
}

public static void main(String[] args) {
    System.out.println(multiplyByTwo(5));
}

What will be printed when main runs?

Java
public static int multiplyByTwo(int x) {
    return x * 2;
}

public static void main(String[] args) {
    System.out.println(multiplyByTwo(5));
}
A25
B5
CCompilation error
D10
Attempts:
2 left
💻 code output
intermediate
2:00remaining
What does this method return?

Look at this Java method:

public static String greet(String name) {
    if (name == null) {
        return "Hello, Guest!";
    }
    return "Hello, " + name + "!";
}

public static void main(String[] args) {
    System.out.println(greet(null));
}

What will be printed when main runs?

Java
public static String greet(String name) {
    if (name == null) {
        return "Hello, Guest!";
    }
    return "Hello, " + name + "!";
}

public static void main(String[] args) {
    System.out.println(greet(null));
}
AHello, null!
BHello, Guest!
CHello, !
DNullPointerException
Attempts:
2 left
💻 code output
advanced
2:00remaining
What is the output of this recursive method?

Consider this Java method:

public static int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

public static void main(String[] args) {
    System.out.println(factorial(4));
}

What will be printed when main runs?

Java
public static int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

public static void main(String[] args) {
    System.out.println(factorial(4));
}
A10
B1
C24
DStackOverflowError
Attempts:
2 left
💻 code output
advanced
2:00remaining
What is the value of x after this method call?

Given this Java code:

public static int addFive(int num) {
    num = num + 5;
    return num;
}

public static void main(String[] args) {
    int x = 10;
    addFive(x);
    System.out.println(x);
}

What will be printed when main runs?

Java
public static int addFive(int num) {
    num = num + 5;
    return num;
}

public static void main(String[] args) {
    int x = 10;
    addFive(x);
    System.out.println(x);
}
A10
B15
C5
DCompilation error
Attempts:
2 left
🧠 conceptual
expert
2:00remaining
Which option causes a compilation error due to return type mismatch?

Which of the following Java methods will cause a compilation error because the return type does not match the returned value?

Apublic static int getNumber() { return 3.14; }
Bpublic static double getDouble() { return 3.14; }
Cpublic static String getText() { return "Hello"; }
Dpublic static boolean isTrue() { return true; }
Attempts:
2 left