GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Adding an element in a Vector... ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Adding an element in a Vector... ???" Watch "Adding an element in a Vector... ???" New topic
Author

Adding an element in a Vector... ???

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What is the effect of adding the sixth element to a vector created in the following manner:

new Vector(5, 10);
A.An IndexOutOfBounds exception is raised.
B.The vector grows in size to a capacity of 10 elements
C.The vector grows in size to a capacity of 15 elements
D.Nothing, the vector will have grown when the fifth element was added.
What should be the answer for the above. One of the Mock Exam says that it should be C. But I feel it should be B. Since JLS says that 2nd argument is the "capacityIncrement - the amount by which the capacity is increased when the vector overflows."
Thanks & Regards.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
import java.util.*;
public class Class1 {
public static void main(String args[]) {
Vector v = new Vector(5, 10);
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
System.out.println(v.capacity());
v.add("6");
System.out.println(v.capacity());
}
}
Output :
5
15
Vector v = new Vector(5, 10); indicates -
Create a vector with initial capacity of 5
When it become full increase the capacity by 10 i.e 5 + 10.
Let me know if I'm wrong.
 
GeeCON Prague 2014
 
subject: Adding an element in a Vector... ???