Nice explanation Cherry n Guoqiao
here r my views ;
About
Question 1 :
The question seems a bit flawed to me , as any of the given options will not compile !
instead of using assignment operator '=' in second part of if-condition,
if we use equilavence operator '==' , then options become
1. if ((s !=null) | ( i == s.length()))
2. if ((s ==null) | ( i == s.length()))
3. if ((s !=null) | | ( i == s.length()))
4. if ((s ==null) | | ( i == s.length()))
Now ,the question is about
short-circuit evaluation ( conditional operators
&& and | | are short-circuit operators )
in these type of operators, if the result of the boolean expression can be determined from the left hand operand , the right hand operand is not evaluated
now , the q involves words '..
MAY throw exception.. ' , so we hav to presume conditions under which any of these may produce exception
invoking the length() method over a null string-reference will through exception
assuming s to be null , our given conditions boil down to
1 if ( false | ( i == s.length()))
2 if ( true | ( i == s.length()))
3 if ( false | | ( i == s.length()))
4 if ( true | | ( i == s.length()))
options 1 and 2 do not use short-circuit operator , so
i == s.length() will be evaluated ,
and a call to length() over
null s will throw exception
option 3 is short-circuited , but as first-part of condition has become 'false' , second has to be evaluated
so it will also invoke length() over
null s , n thus throw exception
option 4 is short-circuited , and as the first part is 'true' , there isnt any need to evaluate second part , so it wont throw exception
so correct options to above q are 1 , 2 and 3
About
Question 2 :
Sure , the q is a bit vauge
any statement ( which compiles correct , of course ) n u like , can be placed at the required-place in given code
But I guess ,the point they are trying to push in this question is , tht how the statements to be used at required place should access memeber variable
c At the required-place , 2 variables with name
c are in scope ,
1. Memeber variable
c ( int c=0; )
2. Parameter variable
c ( float c )
the parameter-variable shadows the member-variable in third-constructor
so any statement which want to access memeber-variable
c , should access it as
this.c (like in this.c = (int) c+b*(a/12) ; )
have chilling preparation ! n good-luck !!
------------------
Gagan (/^_^\)