• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

solve this

 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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());
}
}
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to run the program??

Also please quote the source of the question...
 
vamsi kris
Greenhorn
Posts: 28
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, nice tricky question



examine the code you can clear your doubt

output:
null< >
null
null<a b>

-vamsi
 
Adi Kulkarni
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Muhammad Ijaz
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vamsi,
your right. himunshu thank you for posting such a tricky question!!!
 
Chaitanya Jadhav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yes answer is null

BUT ... fallowing code throws Runtime NullPointerException


 
Rocky Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic