File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A doubt" Watch "A doubt" New topic
Author

A doubt

Charu Murali
Ranch Hand

Joined: Jul 12, 2002
Posts: 37
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

Joined: Jul 03, 2001
Posts: 2120
I would say ArrayIndexOutOfBoundException is raised


SCJP2. Please Indent your code using UBB Code
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A doubt