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    ID:13 SCJP Question of the Day  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "   ID:13 SCJP Question of the Day  " Watch "   ID:13 SCJP Question of the Day  " New topic

ID:13 SCJP Question of the Day

wbrian radley

Joined: Apr 03, 2008
Posts: 22

the usual questions:
does this compile? does it throw exceptions? what is the output (if any)? is there an infinite loop?
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446

The explanation for lines line 1 and line 2 is as follows

now we are looping till the while condition returns true
the add method in set returns true if the element is added to the set
the set contains only the unique elements and not duplicate ones
in our example
we add "boo" twice
first time the array index 0 refers to the boo String
but when we add it again then the array index 3 refers to it
the loop execution is as follows

for the first element
i.e. new String("boo");
the add method returns true as there is no duplicate element and loop executes
for the second element
i.e. new String("who")
same operation happens
now for the "are" again same thing happens

and now again we are adding new String("boo")
now this object already exists in set and duplicates are not allowed and the add method returns false
and hence the loop terminates and we do not get infinite loop
here is the output

C:\Users\Prasad\Desktop>java Question
nonce 0 who nonce 1 are nonce 2 boo

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Set : Duplicates are not allowed
String : Overrides equals() .

boo who and are are successfully added after which loop condition becomes false.

I am just a bit confused about ; (Semicolon) after main method().

If it is allowed then output should be nonce nonce nonce
Complie time error!!!

SCJP 6.0 96%

(Connecting the Dots ....)
wbrian radley

Joined: Apr 03, 2008
Posts: 22
yeah, the extra semi-colons were added to make the code look odd and
thus a temptation to choose compile error, but they have no effect at all (at least in this example).

the key to a correct evaluation of the output is to consider the difference between:


in the both cases indx++ is computed but only in the second is the value of indx
(before the addition) printed.

a rule with printf seems (please correct is your experience is different) to be that while
there must be a matching arg for each conversion there is no requirement that each arg be
matched with a conversion.

if the arg or its result is to be output then there must be a matching conversion.

see also Question 12 for another trick with printf.

also note:

wbrian radley

Joined: Apr 03, 2008
Posts: 22
in order to see better what rules the compiler is using when adding to a set consider
the question as originally posted and add the following code right
after the while loop; what is the output ?

I agree. Here's the link:
subject: ID:13 SCJP Question of the Day
It's not a secret anymore!