aspose file tools*
The moose likes Mock Exam Errata and the fly likes jtips exam2.No.28-Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "jtips exam2.No.28-Exception" Watch "jtips exam2.No.28-Exception" New topic
Author

jtips exam2.No.28-Exception

jordan gong
Ranch Hand

Joined: Jun 12, 2001
Posts: 37
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

Joined: May 09, 2001
Posts: 94
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

Joined: May 09, 2001
Posts: 94
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

Joined: Apr 11, 2001
Posts: 356
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

Joined: May 09, 2001
Posts: 94
Thanks Vanitha, I missed the point. Its clear now.
Jyotsna
jordan gong
Ranch Hand

Joined: Jun 12, 2001
Posts: 37
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
 
subject: jtips exam2.No.28-Exception