aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection" Watch "Collection" New topic
Author

Collection

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
previous post of mine is not printed.i don't know why.that's why i am again sending the same question.

Given two collection objects referenced by c1 and c2, which of these statements are true?

Select 2 correct options



(1) c1.retainAll(c2) will not modify c1
(2) c1.removeAll(c2) will not modify c1
(3) c1.addAll(c2) will return a new collection object, containing elements from both c1 and c2
(4) For: c2.retainAll(c1); c1.containsAll(c2); 2nd statement will return true
(5) For: c2.addAll(c1); c1.retainAll(c2); 2nd statement will have no practical effect on c1

the answers are 4 and 5.

Its a javabeat question.

I have no idea how to check all these options in a coding way since collection is a new topic for me.please help.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Shiva Mohan:
previous post of mine is not printed.i don't know why.that's why i am again sending the same question.

Given two collection objects referenced by c1 and c2, which of these statements are true?

Select 2 correct options



(1) c1.retainAll(c2) will not modify c1
(2) c1.removeAll(c2) will not modify c1
(3) c1.addAll(c2) will return a new collection object, containing elements from both c1 and c2
(4) For: c2.retainAll(c1); c1.containsAll(c2); 2nd statement will return true
(5) For: c2.addAll(c1); c1.retainAll(c2); 2nd statement will have no practical effect on c1

the answers are 4 and 5.

Its a javabeat question.

I have no idea how to check all these options in a coding way since collection is a new topic for me.please help.


Think of retainAll as an intersection.

c1.retainAll(c2) will only keep those elements from c1 that are also in c2.

So 1 is wrong if c1 and c2 had elements in common.

Option 2 is again suspect because if c1 and c2 had no elements in common, then c1 won't be changed by c1.removeAll(c2).

If c1 and c2 do have elements in common, then those elements in c2 will be removed from c1.

c1.addAll(c2) will not create a new Collection. It will just add all of the elements in c2 to c1.

In option 4, c2.retainAll(c1) will leave in c2 only elements that are also in c1. Then c1.containsAll(c2) will definitely return true.

In option 5, c2.addAll(c1) will add all of the elements of c1 to c2. Then c1.retainAll(c2) won't have any effect because you know that everything in c1 is now in c2.
[ March 13, 2007: Message edited by: Keith Lynn ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
previous post of mine is not printed.i don't know why.that's why i am again sending the same question.


There is something weired with that other "Collection" topic. It's due to a UBB forum software bug I guess - do not worry, it is not your fault.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection