Bird
0
0

What is the output of this code?

medium📝 Predict Output Q4 of 15
Java - Methods and Code Reusability

What is the output of this code?

class Demo {
  void display(int a) { System.out.println("Int: " + a); }
  void display(String s) { System.out.println("String: " + s); }
}
public class Test {
  public static void main(String[] args) {
    Demo d = new Demo();
    d.display(5);
    d.display("Hi");
  }
}
AInt: 5 Int: Hi
BInt: 5 String: Hi
CString: 5 Int: Hi
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Identify which display method is called

    display(5) calls display(int), display("Hi") calls display(String).
  2. Step 2: Check output statements

    display(int) prints "Int: 5", display(String) prints "String: Hi".
  3. Final Answer:

    Int: 5 String: Hi -> Option B
  4. Quick Check:

    Overloaded methods called by matching parameter type [OK]
Quick Trick: Method called matches argument type exactly [OK]
Common Mistakes:
  • Mixing output order
  • Assuming wrong method called
  • Thinking compilation error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes