Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A doubt

 
Charu Murali
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
I came across the following question in a mock exam in the link:
" http://www.geocities.com/SiliconValley/Or chard/9362/java/javacert/JDCert.html "
. CAn anyone tell and explain me the answer. The answer is not given in the results.
The question is...
1. What is the effect of adding sixth element to an array arr which is defined as follows?
new arr[5].
a) An IndexOutofBoundException is raised.
b) The arr grows to accomodate the sixth elemt
c) the new elemet will be lost
d) Nothing, The arr will hae grown when the 5th elemet was added
I thought the answer ia a. But it is not a.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say ArrayIndexOutOfBoundException is raised
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code attempts to assign a value to the sixth element of array i.

The result is the expected ArrayIndexOutOfBounds exception. However, I can go ahead and add a sixth element to the array as described in the question.

After "adding [a] sixth element to [the] array" as described in the question the code runs without failure. Based on this example the correct answer would be "d". "Nothing" happens when an additional element is added to an array declaration.
If you selected answer "a" then you must have mentally translated the question to mean "what happens when an attempt is made to assign a value to the sixth element. In reality, the question asked what happens when you declare an array with six elements instead of only five.
If my answer is correct, then the question was ridiculous. I really hope that my answer is wrong.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Dan I can see a 6 in the second example and the question was made with 5.

Please the general subjects as "a doubt" or "help" are a bad choice because they don't give a clue about the real matter. People cannot decide if they are interested in the post beforehand. Also it is not very helpful for searches.
[ August 07, 2002: Message edited by: Jose Botella ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Botella:
But Dan I can see a 6 in the second example and the question was made with 5.
[ August 07, 2002: Message edited by: Jose Botella ]

Jose,
You are correct. The second examples adds a sixth element to the array. The question was as follows.

1. What is the effect of adding sixth element to an array arr which is defined as follows?
new arr[5].

My example did as the question specified: I added a sixth element to the array. As I stated earlier, if my answer is correct, then the question was ridiculous.
Now that I am taking another look at the question I've noticed that the type of the array specified in the question is arr? What sort of type is that? To bad "compiler error" wasn't one of the answer options.
Jose, I hope that your are not taking my answers on this threat seriously. My earlier post was intended to be humorous.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan - no, you added a sixth element to a different array, not the one specified in the problem. In any event the orignal question is poorly defined. ("new arr[5]"? It wouldn't even compile.) Just ignore this mock exam question, as the author was mistaken.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic