Java · Question 23 · Medium

What will be the output of the following Java program?

public class Main {
    static void f(int a) { System.out.println("int"); }
    static void f(long a) { System.out.println("long"); }
    static void f(double a) { System.out.println("double"); }

    public static void main(String[] args) {
        f('A');
    }
}
  1. Compilation error
  2. long
  3. double
  4. int
Answer and explanation

Answer: D. int

Explanation: There is no f(char), so Java picks the most specific method reachable by widening. char widens to int, long and double, and int is the most specific, so f(int) runs.

Previous: What will be the output of the following Java program? · Next: What will be the output of the following Java program?

Practise java

Try a fresh set of java questions with an explanation after every answer, free and without signing up: Java practice. Or sit a full placement mock test.

More java questions

More technical & programming practice

Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge