Challenge - 5 Problems
Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate2: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)); }
Attempts:
2 left
💻 code output
intermediate2: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)); }
Attempts:
2 left
💻 code output
advanced2: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)); }
Attempts:
2 left
💻 code output
advanced2: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); }
Attempts:
2 left
🧠 conceptual
expert2: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?
Attempts:
2 left
