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 Sierra & Bates book do while question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sierra & Bates book do while question" Watch "Sierra & Bates book do while question" New topic

Sierra & Bates book do while question

Carol Bloch

Joined: Nov 21, 2005
Posts: 18
I can not figure out what I am missing here. this is question 8 at the end of chapter 4 in the SCJP study guide by Sierra & Bates

public class Test {
public static void main(Strinf []args){
int I=1;
do while (I < 1)
system.out.print("I is " + I);
while (I>1) ;

The quiz answer says that no output is produced, but doesn't a do while always execute at least once? The answer also states the while is nested in the do while, but it is not. I haven't been able to find any posts aying that the answer to this is wrong. If it doesn't print anything, can someone tell me why not?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What about if you format the code like this ?

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

Joined: Nov 21, 2005
Posts: 18
The code was formatted exactly as I typed it. My first thought was that it was a compile error, because I thought that the while had to be separated from the do, but that wasn't the answer they gave. Is it because the while is at the front of the code that it does not even execute once?
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
Hi Carol,

I just want to make sure that it's clear that Barry's response represents EXACTLY the same functionality as the code in the book! With Barry's added parentheses it should be a lot clearer how this code works, and the reason we didn't add the parentheses in the book is because we need to toughen you up for the real exam - where the evil test creators (who shall remain unnamed) will often use confusing, parentheses-sparse code in an attempt to trick you



p.s. If you study the posts in the results forum, you'll discover that almost everyone who does well on the exam writes a LOT of little Java programs! We totally recommend that when you have a question like this, you write yourself a little code - go wild and add a few println()s
[ November 21, 2005: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Carol Bloch

Joined: Nov 21, 2005
Posts: 18
Ok, thanks! I get it. Boy those test creators (they who shall not be named) are very evil.
I agree. Here's the link:
subject: Sierra & Bates book do while question
It's not a secret anymore!