Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

jtips exam2.No.28-Exception

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me? I could not understand why the output is 4.
The third try is not reached?
28.Given
class Test {
static String s="Java";
boolean b;
Test() {
super();
}
Test(String x) {
x=null;
try {
if ( b=(s.length() == 4) && (x.length() == 4))
System.out.println(b);
}
catch(java.lang.NullPointerException e) {
System.out.println("Exception 1 caught");
}

try {
if ( b=(s.length() == 4) | | (x.length() == 4))
System.out.println(b);
}
catch(java.lang.NullPointerException e) {
System.out.println("Exception 2 caught");
}
try {
if ( b=(s.length() != 4) && (x.length() == 4))
System.out.println(b);
}
catch(java.lang.NullPointerException e) {
System.out.println("Exception 3 caught");
}
}
public static void main(String [] args) {
Test t = new Test(s);
}
}
What is the output?
1.Prints
Exception 1 caught
true
true
2.Prints
Exception 1 caught
true
false
3.Prints
true
true
Exception 3 caught
4.Prints
Exception 1 caught
true

5.compile-time error occurs.
6.Run time Exception is thrown.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
But I don't understand why even "Exception 1 caught" line is printed becoz the string that was passed was not null, it was later on ,in the method made null, so why it goes in catch 'nullpointer exception' ?
Thanks in advance,
Jyotsna
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After giving a second thought I got it.
It is actually in first try since x is null, it is catched by null pointer exception, in second try block it prints true since the first condition is true and its is short circuit operator so it doesn't actually checks the second condition.
Now it goes third try block and in b now takes value false so the right ans. should be
Exception 1 caught
true
false
which is I think option 2.
Can anybody please justify this ans.
Jyotsna
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think choice #4 is correct.
See the comments.

hope this helps,
Vanitha.

[This message has been edited by Vanitha Sugumaran (edited June 17, 2001).]
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vanitha, I missed the point. Its clear now.
Jyotsna
 
jordan gong
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
I got it now, I made a mistake at the "if()".
jordan
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!