🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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'));
    }
}
  1. A.int double char
  2. B.int double double
  3. C.int double int
  4. 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 →