• 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 question

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Can someone explain me a behaviour of this program and what will be the output?
Q13.What will be the result of compiling and running the given program?
Select one correct answer.
1 class sample
2 {
3 sample(String s)
4 {
5 System.out.println("String");
6 }
7 sample(Object o)
8 {
9 System.out.println("Object");
10 }
11 }
12 class constructor
13 {
14 public static void main(String arg[])
15 {
16 sample s1=new sample(null);
17 }
18 }
thank you in advance.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output will be "String" as null is not an object.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shyamsundar, I don�t understand because null is not a String.

Sample s1 = null; ------> compile
String s = null; ------> compile
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the JLS 15.2 section method invocations are dealt with
Sample(String) is chosen because is more specific than Sample(Object)
It is more specific because String can be coneverted via a wideninnig convertion (or identity conversion) to object.
Sample(Object) is not more specific than Sample(String) because Object can't be converted via widenning conversion (or identity) to String
So null is not the reason here.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose...
I understand it now... When I comment out the
method having a string as an argument, the program prints "Object".
But regarding "null", a question still remains in my mind. I wrote a small program..



This program gave the following output...



This means that while a null object can be created, no operaton can be carried out on it..right?... While I can understand the exception at line 4, I fail to do so for line 10.
Do comment on this.
Thanks in Advance
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 09, 2001).]
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic