This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
wood burning stoves
 
subject: jtips exam2.No.28-Exception
 
Similar Threads
Exception
casting problem
doubts in 4 questions
Exception
classes