About Question enthuware.ocajp.i.v7.2.1225 :

Moderator: admin

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

About Question enthuware.ocajp.i.v7.2.1225 :

Post by Ambiorix » Thu Feb 07, 2013 4:39 am

When I run the code below, I get a StringIndexOutOfBoundsException not an IndexOutOfBoundsException as your explanation suggests I should.

Code: Select all

class TestException 
{
	public static void main(String[] args) 
	{
		String str="12345";
		char ch = str.charAt(str.length());
	}
}
Is the explanation wrong, or have I misunderstood something?

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by admin » Thu Feb 07, 2013 8:05 am

The given explanation explains exactly what you are asking -
As per the API documentation for charAt, it throws IndexOutOfBoundsException if you pass an invalid value. Both - ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException, extend IndexOutOfBoundsException and although in practice, the charAt method throws StringIndexOutOfBoundsException, it is not a valid option because the implementation is free to throw some other exception as long as it is an IndexOutOfBoundsException.
Could you please let me know what is not clear so that I can explain more?

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by Ambiorix » Wed Feb 13, 2013 3:15 am

admin wrote:The given explanation explains exactly what you are asking -
As per the API documentation for charAt, it throws IndexOutOfBoundsException if you pass an invalid value. Both - ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException, extend IndexOutOfBoundsException and although in practice, the charAt method throws StringIndexOutOfBoundsException, it is not a valid option because the implementation is free to throw some other exception as long as it is an IndexOutOfBoundsException.
Could you please let me know what is not clear so that I can explain more?

HTH,
Paul.
I find the above explanation very unclear.

1. The documentation says that an IndexOutOfBoundsException will be thrown, but in practice StringIndexOutOfBoundsException is thrown. Are you saying that the documentation is wrong?

2. "although in practice, the charAt method throws StringIndexOutOfBoundsException, it is not a valid option "

How can it not be a valid option if this is what actually happens?

3. "because the implementation is free to throw some other exception as long as it is an IndexOutOfBoundsException"

In what way is it free to throw some other exception? It throws a StringIndexOutOfBoundsException.

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by admin » Wed Feb 13, 2013 8:23 am

String is a standard JDK class. i.e. a class whose source code is *technically* not available to you. The standard practice in such cases where you use someone else's class is to take a look at the API documentation because that is the contract that the class developer agrees to.

In this case, the documentation says it throws IndexOutofBoundsException and so you have to work with that. The fact that it actually throws StringIndexOutofBoundsException is irrelevant. It is highly improbably but they are free to change the implementation to throw any other subclass of IndexOutofBoundsException.

That is the point of the question.

So, to answer your questions:
1. No, the documentation is not wrong. In fact, the documentation is what you have to go with and not the actual exception that is thrown at run time.
2. It is not a valid option because it is not guaranteed that it will throw StringIndexOutofBoundsException. It is only guaranteed that it will throw IndexOutofBoundsException. StringIndexOutofBoundsException IS A IndexOutofBoundsException so they are still within the contract.
3. As explained above, it may throw any other subclass of IndexOutofBoundsException such as ArrayIndexOutofBoundsException or any other and you won't be able to complain because they never agreed that they will throw StringIndexOutofBoundsException. They only agreed to IndexOutofBoundsException.

Again, if you write code to catch StringIndexOutofBoundsException instead of IndexOutofBoundsException, you would be wrong. Your code will work but would not be appropriate and would not be robust. It may break because you are violating the contract.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by Ambiorix » Thu Feb 14, 2013 3:33 am

I think I have it now.

The spec isn't saying that the class literally throws the stated exception. It's saying that if I write my code as if it throws that exception, they guarantee that I won't have any problems either now or in future.

They're free to throw either the stated exception or one of its subclasses, and they may change it to a different one of these in future releases. However they promise it'll never throw an exception outside that exception hierarchy so, if I write my code according to the spec, I'll never need to worry about future changes.

rfoust
Posts: 1
Joined: Sat Jun 08, 2013 2:58 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by rfoust » Sat Jun 08, 2013 3:00 pm

Wow, nice discussion. This is very helpful. Thank you both.

rasdfg
Posts: 1
Joined: Tue Oct 29, 2013 3:07 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by rasdfg » Tue Oct 29, 2013 5:10 am

Thanks for the details admin. Thanks for digging into deep Ambiorix.

:)

paraglohiya
Posts: 1
Joined: Fri Feb 14, 2014 1:42 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by paraglohiya » Fri Feb 14, 2014 1:45 am

Hi Admin,

Don't you think that the second option
The index of the first character is 0 is irrelevant to the charAt()
as it has nothing to do with the question

By default java takes 0 for any stating index.

The answer should have something more related to charAt() instead of a generic one.

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by admin » Fri Feb 14, 2014 3:06 am

It is indirectly related because charAt depends on how the characters are numbered.
By default java takes 0 for any stating index.
This is not correct. JDBC related methods for columns start indexing with 1!
If you like our products and services, please help us by posting your review here.

foksware
Posts: 1
Joined: Wed Mar 19, 2014 4:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by foksware » Wed Mar 26, 2014 4:24 pm

very important discussion for me. I would also think the explanation is not clear enough, but much better now

_udel_
Posts: 1
Joined: Thu Jul 10, 2014 4:11 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by _udel_ » Thu Jul 10, 2014 5:22 pm

Thanks for clarifying the explanation. This was helpful.

bh00mi
Posts: 1
Joined: Wed Jul 09, 2014 12:20 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by bh00mi » Fri Jul 11, 2014 1:48 am

This is really helpful for me :) Thx Ambiorix

brj4321
Posts: 2
Joined: Wed Dec 17, 2014 6:17 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by brj4321 » Wed Dec 17, 2014 6:32 am

The question asks:

Which of these statements concerning the charAt() method of the String class are true?

One of the answer options is :

It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0).

So how can it possibly be argued that the above is not true when looking at the code for the charAt method as shown below:

Code: Select all

   public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }
The method clearly does throw a StringIndexOutOfBoundsException.

If you catch the thrown exception as an IndexOutOfBoundsException, a StringIndexOutOfBoundsException will still be assigned to the catch block parameter!

Code: Select all

String s = "abcd";

        try {
            s.charAt(5);
        } catch (IndexOutOfBoundsException ex) { 
           
            ex.printStackTrace();

        }
I accept that you SHOULD follow the contract and the other points made by Admin.

But it is true that method charAt() of class String does throw a StringIndexOutOfBoundsException.

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by admin » Wed Dec 17, 2014 8:26 am

Yes, there is nothing wrong with your argument. Please see the discussion above. It is up to you which answer you think is more correct.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

brj4321
Posts: 2
Joined: Wed Dec 17, 2014 6:17 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by brj4321 » Wed Dec 17, 2014 10:33 am

Thanks for your response. But I think you need to rephrase the question.

Thanks for this excellent product!

winddd
Posts: 18
Joined: Wed Feb 11, 2015 10:48 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by winddd » Wed Feb 18, 2015 7:15 am

admin wrote:String is a standard JDK class. i.e. a class whose source code is *technically* not available to you.
Exactly! Standard class! It's available in source code! But it isn't important.
This class cannot has more than one implementation. And it's throws StringIndexOutOfBoundsException when index < 0 || index > length ONLY.

johnos
Posts: 1
Joined: Tue Aug 18, 2015 3:37 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by johnos » Tue Aug 18, 2015 3:57 am

Hi,
The solution for Test #2, Q60 says that the correct answers are Options #1 and #5.
But is Option #6 not also correct?

Option #6 states:
It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0).

This is precisely what happens when you run the code - you get a StringIndexOutOfBoundsException. So Option #6 is also correct, right?

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by admin » Tue Aug 18, 2015 5:59 am

Yes, please go through the discussion above. As discussed above, it is a matter of interpretation. If you think StringIndexOutOfBoundsException, that is fine. We don't think so, and that is fine too. It is subjective.
If you like our products and services, please help us by posting your review here.

DanMan
Posts: 2
Joined: Tue Sep 08, 2015 7:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by DanMan » Tue Sep 08, 2015 7:59 am

Love the testing product! :D

This is the exact description of the StringIndexOutOfBoundsException class from Oracle's website: http://docs.oracle.com/javase/8/docs/ap ... ption.html

"Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string."

I find it interesting that they specifically mention the charAt method.

admin stated "In this case, the documentation says it throws IndexOutofBoundsException and so you have to work with that.", and that is correct, but the documentation for StringIndexOutOfBoundsException is totally contradicting that argument.

Exceptions used to be my weak point, so I have been studying the RuntimeException subclasses in great detail preparing for this test. So when I came across this question, #5 was a no-brainer, and #6 was practically verbatim for the StringIndexOutOfBoundsException class description - checked it in a heartbeat.

Can you please explain why one piece of documentation should take precedence over another?

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by admin » Tue Sep 08, 2015 8:37 am

What the documentation of StringIndexOutOfBoundsException says is irrelevant when you are talking about the charAt method. Whoever coded StringIndexOutOfBoundsException wrote the documentation from his/her understanding of what it might be used for. Here, we are concerned with the charAt method, so we have to give preference to the documentation for that!

Even so, I will have to repeat what I said above. There is no 100% right answer. Oracle's documentation is inconsistent. But based on whatever they have given, in our judgement, the documentation for the method takes precedence over anything else. You are free to disagree with that.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

DanMan
Posts: 2
Joined: Tue Sep 08, 2015 7:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by DanMan » Tue Sep 08, 2015 8:58 pm

Okay, thanks for the answer on the precedence regarding your position - I really appreciate and respect that! I get the feeling that Enthuware employs some people that really know what they are doing ... you didn't write the Java language, but I now realize that you are doing your best to test on imperfect documentation.

I just had one of these really... are you kidding me moments??? - not towards Enthuware, but the Java API.

I went through the entire documentation for the API for the String class and not one single method documents throwing the StringIndexOutOfBoundsException!
I looked at the source code for the String class and found over two dozen constructors and methods that throw the StringIndexOutOfBoundsException (I stopped counting).

In the String class there are exactly two methods that throw IndexOutOfBoundsException: offsetByCodePoints and codePointCount. Who even uses these methods?

The String class is the first class that most everybody learns about - primitive data types then the String class!

If any class should be 100% documented properly, it should be the String class.

Whoever wrote the String class documentation (and should be fired IMO) might as well have gone further up the hierarchy and documented that is throws Throwable! What the heck it is beer Friday and who reads this stuff anyway - right?

I tried for a moment to find a class anywhere in the API documentation that throws StringIndexOutOfBoundsException before I thought to myself... duh - String is immutable - search all you want! After all, isn't that the sole and exclusive purpose of the StringIndexOutOfBoundsException according to the documentation?

Can you imagine the carnage it would cause if Oracle decided to actually throw a different exception on the 2+ dozen methods in the String class??? Admin stated "the implementation is free to throw some other exception as long as it is an IndexOutOfBoundsException." and that is correct and crazy scary!!! 3 billion devices run Java ... 1.8 billion crash after that update!

All of the above questions are rhetorical, but can you tell me if this sort of lazy (superclass cop-out) documentation is common?

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by admin » Tue Sep 08, 2015 9:21 pm

No, such issues are not common. In fact, I would say that due to the large size of Java community, Java is scrutinized very thoroughly and any inconsistencies are removed fairly quickly.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Mushfiq Mammadov
Posts: 33
Joined: Fri Oct 16, 2015 4:33 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by Mushfiq Mammadov » Wed Nov 04, 2015 8:39 am

Difficulty level of this question (very easy) was surprised me. I thought that the difficulty level of question is determined with user’s answers but I saw some users confuse in this question, me too. It is interesting that how difficulty level of question is determined?

kashyapjoshi
Posts: 1
Joined: Sat Nov 28, 2015 6:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by kashyapjoshi » Sun Nov 29, 2015 1:45 am

very good discussion. thanks to all.

On a side note, question was simple but different option makes it difficult so it is not a "Very Easy" question as stated in toughness
part. :)
Is this toughness also available in real exam?

jax502
Posts: 2
Joined: Mon Nov 14, 2016 3:33 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1225 :

Post by jax502 » Mon Nov 14, 2016 3:42 pm

Great discussion. Although I have to agree that the option about the StringIndexOutOfBoundsException should be considered true, because if you pass charAt() a value higher than or equal to the length of string or less than 0, it will indeed throw StringIndexOutOfBoundsException. Period. I know one of the tips that I got from the Jeanne Boyarsky book is that don't try to read or over analyze the question. In the exam, we don't have much time to overanalyze questions although it's definitely a great discussion item.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest