Forums Register Login

Questions from Jxam

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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

Can you shoot lasers out of your eyes? Don't look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 600 times.
Similar Threads
Jxam
Jxam
Jxam Question
Jxam
Jxam #47
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:28:22.