programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Vector.

Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Hello all...
True or False:-
Q.1) What is the effect of adding the sixth element to a vector created in the following manner:
new Vector(5, 10);
1) An IndexOutOfBounds exception is raised.
2) The vector grows in size to a capacity of 10 elements
3) The vector grows in size to a capacity of 15 elements
4) Nothing, the vector will have grown when the fifth element was added
The answer given was 3, but i think it should be 2.
Correct me if i m wrong.
Q.2) Which of the following describes an interface which
can be used to store non-duplicate info in an Ordered fashion. (Choose only one)
1) Set
2) List
3) Map
4) SortedSet
5) SortedMap
My Ans:- 5. Correct me if i m wrong.
Thanks

Ranch Hand
Posts: 54
• Number of slices to send:
Optional 'thank-you' note:
the constructor Vector(int capacity,int increment) creates a Vector object with an initial capacity of 'capacity' and the capacity will be increased by 'increment' elements each time more space is needed.
In your case a Vector object is created initially with a capacity of 5 and when a sixth element is to be added it's capacity is incremented by 10 i.e 5+10=15 .Hence the answer is 3.

Ranch Hand
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:
Hey
In my humble opinion SortedSet should store non repeatind ordered elements
I would welcome any corrections though
Regards Denish

mister krabs
Posts: 13974
• Number of slices to send:
Optional 'thank-you' note:
Set holds non-duplicate entries... SortedSet holds ordered non-duplicate entries.

Ranch Hand
Posts: 327
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by sai challa:
the constructor Vector(int capacity,int increment) creates a Vector object with an initial capacity of 'capacity' and the capacity will be increased by 'increment' elements each time more space is needed.
In your case a Vector object is created initially with a capacity of 5 and when a sixth element is to be added it's capacity is incremented by 10 i.e 5+10=15 .Hence the answer is 3.

Hello,
I just wanted to confirm one thing.
Consider the same case
Vector v = new Vector( 5, 10 )
Does it mean that total capacity of vector is (5+10)=15?
So i can add upto 15 elements.

Ranch Hand
Posts: 216
• Number of slices to send:
Optional 'thank-you' note:
Hi Folks,
Let me have the pleasure to sum up the discussion in this thread
[**]First question , the answer is (3) the reason is given below:
This is what the JAVA's API says about the <code> vector() </code>

Vector
public Vector(int initialCapacity,
int capacityIncrement)
Constructs an empty vector with the specified initial capacity and capacity increment.
Parameters:
initialCapacity - the initial capacity of the vector.
capacityIncrement - the amount by which the capacity is increased when the vector overflows.
Throws:
IllegalArgumentException - if the specified initial capacity is negative

So no doubts on that, because on addition of sixth element it adds further ten elements in the vector apart from 5
[**] Second question The answer is (4) SortedSet and (5) SortedMap the reason is as per the API again and I am quoting it here.

public abstract interface Set
extends Collection
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
--------------------------------------------------------------
public abstract interface SortedSet
extends Set
A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap.)

public abstract interface Map
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
-------------------------------------------------------
public abstract interface SortedMap
extends Map
A map that further guarantees that it will be in ascending key order, sorted according to the natural ordering of its keys (see the Comparable interface), or by a comparator provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods). Several additional operations are provided to take advantage of the ordering. (This interface is the map analogue of the SortedSet interface.)

[**]Thus, by seeing at the document of API for SortedSet and SortedMap we can easily conclude that both are analogous to each other. "Set" which is the super interface of "SortedSet" interface does not allow duplicate keys, while "Map" which is the super interface of "SortedMap" interface also DOES NOT allow duplicate keys .
Guys I hope clarifies the issue here.

Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 05, 2001).]

Shah Chunky
Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Dear Ravindran...
if u have to choose one than what will be the answer
Thanks.

Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Dear Ravindran..
See the q. 1st --->
In q no. 2 it has been mentioned that u have to choose one.
How can u choose two ans.
Soo..if u choose one wht. would be the answer !!!

Thanks.

Ravindra Mohan
Ranch Hand
Posts: 216
• Number of slices to send:
Optional 'thank-you' note:
First of all guys my name is Ravindra and not "Ravindran"
Anyway, coming back to the question,, I will definitely
mark my answer as SortedSet because that is what
the certification guide mention . But the point that I was making
was that as per Java's API (which the official statement) the
answer can be both SortedMap and SortedSet. But I think the question is not phrased correctly, if it were asked like

MY INSERTIONS ARE MADE IN ITALICS AND BOLD
Then there would have been no ambiguity as SortedMap is not part of "Collection" Interface .
Hope this clarifies.
Cheers, .
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 06, 2001).]

 Ever since I found this suit I've felt strange new needs. And a tiny ad: Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat