• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock Exam Questions (null)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 3.
Read the code below. Will be the result of attempting to compile and run the code below.

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);
}
}
Answers
1.The code does not 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.
Correct Answer is 3
-----------------------------------------------------------------
Question 4.
Read the code below. Will be the result of attempting to compile and run the code below.

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);
}
}
Answers
1.The code does not 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.
Correct answer is 1
Can anyone explain above answers? Thanks.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Snehal,
I don't know about the second question. But I can help you on the first. According to the Java API Spec the following holds:


The null type has one value, the null reference, represented by the literal null, which is formed from ASCII characters.


Since it is made up of ASCII characters it makes sense that it would seek out the String method.
Regards,
Manfred.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Snehal,
After some API reading, I have come to the following conclusion regarding the second question. Since internally Java used StringBuffer to perform String concatenation, I would suspect that the compiler would be complaining about similar method signatures. Or maybe the compiler couldn't tell which method to use ...
Regards,
Manfred.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is regarding the 2nd question.
A compile-time error occurs because of the ambiguos method,
method(StringBuffer sb) and method(String s).
Here the method signature is same i.e, the name of the method and the return type is same. That's why the compiler treats these two methods as same and hence the compile-time error.
Note: StringBuffer(String s)- can take String object as an argument.
If you change any one of these methods to a different name, you will get the correct output.
- Suresh Selvaraj
[ www.jtips.net ]
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
U r on the right track in solving problems.but in this case it is not bcoz of the string version or stringbuffer.Try using any other super class and subclass instead of object and string u will get the same result.
I will tell u the reason in the first case u agree that two methods are there one takes object and other takes String.String is the subclass of Object.In this case when u call null it calles the most specific of the two and that is the String version.
In the second case we have String and StringBuffer which are at the same level in inheritance hierrachy and java is unable to resolve which method to call.
Try this


In the first case u will get the out put subclass version and in the second case the code wont compile bcoz compiler cant resolve this.
 
snehal shah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. Thank you very much for your response. I agree with Cherry. Actually I had suspected that but wasn't sure. But now it makes sense.
Snehal
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic