• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Caught in Fundas

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the funda behind such question?
Is it checking whether we know shortcircuit operator operation?
--------------------------------------
<pre>
Question
Which of the following may throw an Exception?
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())) </pre>


Options
1. 1
2.2
3.3
4.4
Select all right answers.
--------------------------------------
First of all, s is not defined.

If i guess s=null.
Then I will get Exception in 1,2,3.
If I guess s="tvs"
Then I wont get any exception at all.
How should I proceed?

---------------------------------------------------------------------------------
One more Question.
-----------------------------
<pre>
public class Test {
int c = 0;
public Test(int a, int b) {
c = b*(a/12);
}
public Test(int a){
c = a/12;
}
public Test(int a, int b, float c) {
<<statement>>
}
}
</pre>


What statement should be entered to initialize c?
--------------------------
I can initialize c in the third constructor the way I like. Isn't it. Should there be any restriction or limitation just because it was defined in some fashion in other two constructors.
i.e for me c=a+b;
or
c=a*3+b/7 etc anything is Ok as long as it meets my design requirement. But the answer given is not so.
I thought I need not bias by giving the answer here and may be let us discuss the concepts before discussing the answers given.
----------------------
Any thoughts...
Thankx
tvs sundaram
[This message has been edited by tvs sundaram (edited August 21, 2001).]
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your first question, please notice the word may<code></code>, which makes the answers 1, 2 and 3 right.
I don't have any idea about question 2. I am wondering whether it is testing constructor overloading...
Guoqiao

Originally posted by tvs sundaram:
What is the funda behind such question?
Is it checking whether we know shortcircuit operator operation?


 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
The first question I guess is about short circuit evaluation. So you have to know if the second test will be executed or not.
Now if s is null, the second expression ( i = s.length()) will throw an exception when evaluated.
The question states: Which of the following MAY throw an exception? There is a difference between MAY throw and WILL throw. (When I take mock exams, most of the time I get wrong answers because I sometimes mis-understood what the question is about.So when you take the exam you have to read the question very carefully.)
I think you were right about assuming that s might be NULL, in which case, 1,2,3 will throw exception.
With regards to the second question, I do not understand it very much.
I hope this helps.

 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 (/^_^\)
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent Gagan! I want add that in TVS's first code we may satisfy the compiler only with the initializer expression
String s = null;
Other string literal or 'new' accept null is also welcome but out of our interest in this discussion.

------------------
Muhammad Ashikuzzaman (Fahim)
When you learn something, learn it by heart!
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sundaram,
I totally agree with Gagan's explanation.Surely they want to know how a programmer will access member variable, when we have the name of parameter matching with the member variable.
Good work Gagan!
thanks.
Bindesh Vijayan
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks friends; I have learnt something today.
Good Show; Keep it up.

tvs sundaram
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic