aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JXam Qns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JXam Qns" Watch "JXam Qns" New topic
Author

JXam Qns

Amandeep Waraich
Ranch Hand

Joined: Jul 14, 2000
Posts: 56
Hi,
I just took the JXam mock exam. I have a doubt regarding this qn. Could somebody help me out?

1. Given the following definition:
String s = null;
Which code fragment will cause an exception of type NullPointerException to be thrown.
1. if ((s != null) & (s.length()>0))
2. if ((s != null) && (s.length()>0))
3. if ((s != null) | (s.length()>0))
4. if ((s != null) | | (s.length()>0))
5. none of the above.

My answer: 1, 3, 4. Answer given: 1, 2, 3, 4. I don't think answer 2 is correct b'coz the second part will never be evaluated as s!=null will return false, and there won't be any need to evaluate s.lenght()>0 and the exception won't be thrown.
Thanks in advance,
Aman
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
Hi,
You are right in your answer
I enclose the program below to conform what i say.
class demo
{
public static void main(String[] args)
{
String s=null;
//System.out.println("first");
//if ((s != null) & (s.length()>0))
//System.out.println("first1");
if ((s != null) && (s.length()>0))
System.out.println("first2");
//if ((s != null) | (s.length()>0))
//System.out.println("first3");
//if ((s != null) | | (s.length()>0))
//System.out.println("first4");

System.out.println("Hello World!");
}
}
The program compiles sucessfully and at runtime prints out Hello World sucessfully.
Regds.
Rahul.
[This message has been edited by Rahul Mahindrakar (edited July 31, 2000).]
Netla Reddy
Greenhorn

Joined: Jun 18, 2000
Posts: 15
Hi,
What Aman is saying is correct.Second part will never be performed.
But I was wondering what Rahul is saying that His code is printing literal "first2".How will print it b'coz It returns false so it will not print immediate print statement.It will skip to next statement and it prints literal "Hello world"
Thanks
Netla
Originally posted by Rahul Mahindrakar:
Hi,
You are right in your answer
I enclose the program below to conform what i say.
class demo
{
public static void main(String[] args)
{
String s=null;
//System.out.println("first");
//if ((s != null) & (s.length()>0))
//System.out.println("first1");
if ((s != null) && (s.length()>0))
System.out.println("first2");
//if ((s != null) | (s.length()>0))
//System.out.println("first3");
//if ((s != null) | | (s.length()>0))
//System.out.println("first4");

System.out.println("Hello World!");
}
}
The program compiles sucessfully and at runtime prints out first2 sucessfully.
Regds.
Rahul.

[This message has been edited by Netla Reddy (edited July 31, 2000).]
[This message has been edited by Netla Reddy (edited July 31, 2000).]
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
Can you please tell me the link to jxam practice test
Please...........
Thanks
sdev


"SCJP5 | SCWCD5| DEVELOPER"
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
i think only 3 & 4 are right..even with 1 since the first part is false (0) it won't test the second part as 1&1 return 1 and all other return 0 so it does'nt have to check the second eq to return false...
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
1 will throw an exception as well, & doesn't short circuit, even though (s != null) is false, (s.length()>0) gets evaluated and an exception is thrown.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
hi Netla Reddy,
You are right first2 will not be printed as the result of " if ((s != null) && (s.length()>0))" is false
Regds.
Rahul

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Can someone who has taken java 2 exam; e-mail me some similar exam questions at khossain@altavista.com and I will also need the list of practice exam sites.
Will appreciate this.
Thanks in advance
Khalid
John Fairbairn
Ranch Hand

Joined: Jul 12, 2000
Posts: 31
sdev... the link to jxam is http://eddiemcnally.hypermart.net/jxam.html
I found a couple of questions on this mock very similar to the real thing.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JXam Qns