File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q from sun's epractice exams Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "q from sun Watch "q from sun New topic
Author

q from sun's epractice exams

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303


i dont know the ans to this q.plz help.


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What answer does it give when you run it? Is there a particular expression or operator you do not understand? You can usually solve these problems by using a table with variables and their values. And, of course, putting in appropriate println statements at various interesting points in the computation.
[ December 26, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
I am confused. at no point of time, is the condition in the if loop going to be satisfied.so the value of t=0. but the answer options dont reflect the same.

plz help.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The if condition is satisfied when t is 3 and m is 2. Do not forget that there is a t++ in the if that modifies t every time it is tested.
[ December 26, 2004: Message edited by: Barry Gaunt ]
JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
if(t++ > m)
is equivalent to if(t > m){ t++; //rest of the code }

am i right? if so, how is the value of t getting incremented?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
No you are not correct. Check out your BASIC Java book on how an expression like "t++ > m" is evaluated. Or write a simple piece of code to print the values of "t++ > m" and t.
[ December 26, 2004: Message edited by: Barry Gaunt ]
Ramy M. Kamel
Greenhorn

Joined: Sep 13, 2004
Posts: 22
Originally posted by Barry Gaunt:
No you are not correct. Check out your BASIC Java book on how an expression like "t++ > m" is evaluated. Or write a simple piece of code to print the values of "t++ > m" and t.

[ December 26, 2004: Message edited by: Barry Gaunt ]


Barry,

Why it's not correct?
I think what JayaSiji is probably correct, as the postfix ++ operator in this case is evaluated after the comparsion, and the original value for t is used instead.

So, for an example :


This piece of code will evaluate to true, as the value 0 of xxx is used in the comparison, and when the print statement executes, it will print :
Equals 1 : 0

Any thoughts?


<b>If you do things well, do them better. Be daring, be first, be just.</b><br />SCJP <b>98.4%</b>
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"if(t > m){ t++; //rest of the code }" means that t will only be incremented if t is greater than m. That is not the same as "if (t++ > m) {//rest of the code }" in which t will be incremented regardless of the relationship between t and m.
[ December 26, 2004: Message edited by: Barry Gaunt ]
Ramy M. Kamel
Greenhorn

Joined: Sep 13, 2004
Posts: 22
Originally posted by Barry Gaunt:
"if(t > m){ t++; //rest of the code }" means that t will only be incremented if t is greater than m. That is not the same as "if (t++ > m) {//rest of the code }" in which t will be incremented regardless of the relationship between t and m.

[ December 26, 2004: Message edited by: Barry Gaunt ]


Barry, you are correct, I didn't see it from this prespective.

I just wanted to show that the result of the ++ operator is not used in the comparison itself.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I think Barry's basic point is that JayaSiji should start compiling and executing these programs himself. He can even try changing the programs to see how the results change.

There is a basic principle of education that the more senses you involve in studying, the better you learn. This is strongly backed by recent research in brain functionality.

So, JayaSiji, please run some of these programs yourself. If you have difficulty setting up the Java SDK, just ask us for help.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: jrebel
 
subject: q from sun's epractice exams
 
Similar Threads
where is wrong?
for loop doubt
sun's sample ques
may be this is very basic question...........
Sun ePractice question 5 loop/post-increment