File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Caught in Fundas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Caught in Fundas" Watch "Caught in Fundas" New topic

Caught in Fundas

tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
What is the funda behind such question?
Is it checking whether we know shortcircuit operator operation?
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>

1. 1
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.
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) {

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;
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...
tvs sundaram
[This message has been edited by tvs sundaram (edited August 21, 2001).]
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
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...
Originally posted by tvs sundaram:
What is the funda behind such question?
Is it checking whether we know shortcircuit operator operation?

Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="" target="_blank" rel="nofollow"></a>, Java resource, mock exam, forum
Cherry Sta. Romana

Joined: Aug 08, 2001
Posts: 18
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.

Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346

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
(like in this.c = (int) c+b*(a/12) ; )
have chilling preparation ! n good-luck !!

Gagan (/^_^\)

Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="" target="_blank" rel="nofollow">My Blog</a>
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

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!

Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
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!
Bindesh Vijayan
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Thanks friends; I have learnt something today.
Good Show; Keep it up.

tvs sundaram
I agree. Here's the link:
subject: Caught in Fundas
It's not a secret anymore!