Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions from Jxam

 
Javed Khan
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
These are questions from Jxam.
1.Given the following defenition
String s=null;
which code fragment will cause a NullPointerException to be thrown?
(a)if((s!=null)&(s.length()>0))
(b)if((s!=null)&&(s.length()>0))
(c)if((s!=null)|(s.length()>0))
(d)if((s!=null)| |s.length()>0))
(e)None of the above
The answer given is a,b,c,d.But I think choice b is wrong.The reason is,as LHS is false so the RHS does not needs to be
evaluated because of &&.This makes the entire expression to be false.
Correct me if i'm wrong.

2.Given the following ,what would be the correct answers?

class Test
{
int x=5;
static String s="abcd";
public static void method()
{
System.out.println(s+x);
}
}
(a)code fails to compile,because cannot make a static reference to non-static variables.
(b)compiles fine,if x is declared to be static.
(c)compiles fine by removing the keyword static from the declaration of method()
The answers are a,b,c.
I do not agree with c.The reason is eventhough by removing the keyword static from the method(),in the expression
System.out.println(s+x) s is a String declared as static.so code fails to compile,coz cannot make non- static reference to static
variables.
Am I correct?If not correct me.
 
rajani peddi
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javed Khan,

With the first question i agree with u. b will not throw any exception.
But in the second question option c is also correct. This is because only STATIC methods are retricted to access static variable , but non-static methods may access either static or non-static variables.
rajani

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic