• 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

solve this

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output ?

class Test02{

public String fetch(){
return null;
}

public static void main(String args[]){
Test02 obj = new Test02();
String str = obj.fetch();
System.out.println((str+"").toString());
}
}
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to run the program??

Also please quote the source of the question...
 
Greenhorn
Posts: 28
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, nice tricky question



examine the code you can clear your doubt

output:
null< >
null
null<a b>

-vamsi
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi himanshu ,

I think the example mentioned by you will throw a NullpointerException ( Runtime) , since in the system.out.println you are trying to use a reference variable not referring to any object. ( Its having null value).

~Aditya
 
vamsi kris
Greenhorn
Posts: 28
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think it doesnt throw a null pointer exception because you are converting a null object a string object in system.out.println(); statement so it compiles and runs correctly
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually it doesn't throw NullpointerException. Indeed the null is first concatinated with "" string. After that when null has been concatinated to "" String then it convert the null+"" into String with toString() method. So if we convert null+"" into String, no reason for NullpointerException.

Regards.
Pomy
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vamsi,
your right. himunshu thank you for posting such a tricky question!!!
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yes answer is null

BUT ... fallowing code throws Runtime NullPointerException


 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all please quote the source Himanshu !!

This program will throw a nullpointer exception !!

Have you seen executing this code ???
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic