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.
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.