This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

vector  RSS feed

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Select the most appropriate answer.

Can this question be answered by what is there in Kathy Sierra book ??
Can we expect such questions on collections in the real exam ??

Please advise...

Thanks,
JP
 
author
Posts: 23813
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Java Docs, it will throw an exception, if you try to insert into an index that is greater than the number of elements.

Henry

 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but that is not my question....
Let me repeat my question is ...

1. Can this question be answered by what is there in Kathy Sierra book ??
2. Can we expect such questions on collections in the real exam ??

Please advise...

Thanks,
JP
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether adding the sixth element throws an exception depends on how it is added to the Vector. If you use add(element), it will be appended to the tail with no exception. If you use add(index, element), it will throw an exception.

You can answer this question with what is in K&B plus reading the API for the Java Collection classes (as recommended by K&B).
 
Sheriff
Posts: 21610
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For answering this question you must know the API for Vector. Now I've read the entire K&B book, and found no API's for any Collection. Hence I think it's very doubtful this kind of questions will be in the exam.

PS: the answer is 3: you create a Vector with initial capacity of 5 and increment of 10. When adding the 6th element, you get an overflow and capacity for another 10 elements is added - there is now capacity for 15 elements.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, but if you add an element at the sixth place, vector_name[5], straightaway, without filling the vector, you get ArrayOutOfBounds exception. I don't think you will see this question on the exam because it is an ambiguous question.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!