Java Daily, Quiz 5

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.

🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.

▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube

Welcome to Java daily quiz 5 of Java daily quiz series, you can test yourself with this Java quiz. The answer and explanation are given at the end for your reference.
Check out all the Java daily quiz questions at https://www.javaguides.net/p/java-daily-quiz.html.
Check out my YouTube channel at https://www.youtube.com/c/javaguides.

Quiz 5 - Consider the following program and predict the output:

public class Test {
    public void print(Integer i) {
        System.out.println("Integer");
    }

    public void print(int i) {
        System.out.println("int");
    }

    public void print(long i) {
        System.out.println("long");
    }

    public static void main(String args[]) {
        Test test = new Test();
        test.print(10);
    }
}
a) The program results in a compiler error (“ambiguous overload”).
b) long
c) Integer
d) int

Answer

d) int
Explanation:   For an integer literal, the JVM matches in the following order: int, long, Integer, int.... In other words, it first looks for an int type parameter; if it is not provided, then it looks for a long type; and so on. Here, a literal will match to int. So, the program prints int.

The automatic type promotion in java method overloading concept is very important for an interview. The following diagram summarizes the automatic type promotions for the method overloading:

Check out all the Java daily quiz questions at https://www.javaguides.net/p/java-daily-quiz.html.
Check out my YouTube channel at https://www.youtube.com/c/javaguides.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare