File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jtips exam2.No.28-Exception

 
jordan gong
Ranch Hand
Posts: 37
  • 0
  • 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.
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • 0
  • 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
  • 0
  • 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
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic