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. 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.
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.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Joined: Jul 03, 2001
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 ]
Joined: Jul 02, 2002
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.
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.
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"? It wouldn't even compile.) Just ignore this mock exam question, as the author was mistaken.