Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q5 of 15
Java - Polymorphism

What will be the output of the following Java code?

class Display {
    void show(int x) { System.out.println("Int: " + x); }
    void show(String x) { System.out.println("String: " + x); }
}
public class Test {
    public static void main(String[] args) {
        Display d = new Display();
        d.show(10);
        d.show("Hello");
    }
}
ACompilation error due to ambiguous method calls
BString: 10 Int: Hello
CInt: 10 String: Hello
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Identify method calls

    show(int) called with 10, show(String) called with "Hello".
  2. Step 2: Output reasoning

    Methods print respective types with prefixes.
  3. Final Answer:

    Int: 10 String: Hello -> Option C
  4. Quick Check:

    Overloaded methods called based on argument type [OK]
Quick Trick: Method called matches argument type exactly [OK]
Common Mistakes:
  • Assuming ambiguous calls cause errors
  • Mixing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes