• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JXam Qns

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please tell me the link to jxam practice test
Please...........
Thanks
sdev
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone who has taken java 2 exam; e-mail me some similar exam questions at [email protected] and I will also need the list of practice exam sites.
Will appreciate this.
Thanks in advance
Khalid
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Men call me Jim. Women look past me to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic