← Quiz: Java Methods: all 10 questions
Quiz: Java Methods · Question 6 of 10
What does this print? (code)
What does this print?
public class Main {
static String f(int x) { return "int"; }
static String f(double x) { return "double"; }
public static void main(String[] args) {
System.out.println(f(5) + " " + f(5.0) + " " + f('a'));
}
}
- A.int double char
- B.int double double
- C.int double int
- D.Compile error
Answer & explanation
Correct answer: int double int
There is no f(char), so the char 'a' is widened to the closest type, int. Overloads are chosen at compile time.
Can you get the rest right without peeking?
Take the timed Quiz: Java Methods quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →