These are two questions from one of mock exams
1).
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(
String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
What will be result if you try to compile and run this code.
1. Doesn't compile
2. The code compiles cleanly and shows "Object Version".
3. The code compiles cleanly and shows "String Version"
4. The code throws an Exception at Runtime
2).
public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
What will be the output when you try to compile and run this code?
1. Doesn't compile
2. The code compiles cleanly and shows "StringBuffer Version".
3. The code compiles cleanly and shows "String Version"
4. The code throws an Exception at Runtime
I thought the correct answer for both of these would be 1. means It shouldn't compile because of ambiguity.
But correct answers are 3 and 1 respectively for question 1 & 2.
can anyone explain this to me?
Thanks in advance