Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1225

Posted: Sun Dec 04, 2016 1:48 am
by petzchen
Hi,

the question item enthuware.ocajp.i.v8.2.1225 seems not correct.

The Option: "It throws StringIndexOutOfBoundsException if passed a value higher than or equal to the length of the string (or less than 0)." is a valid option.
The Java Docs (https://docs.oracle.com/javase/8/docs/api/) are not precise here. A StringIndexOutOfBoundsException will be thrown beside the fact that its base class is
of IndexOutOfBoundsException.

Anyway, the more specific solution (Option with StringIndexOutOfBoundsException) is treated as an error here, despite the fact that it is correct.

I am using Java 1.8.0_60 and checked also the jdk sources of the String class. Question Bank version is 1/44.

Maybe I missed something or the issue has been reported already. In that case I say "Sorry" for bothering you.

Happy day
STephan

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

Posted: Sun Dec 04, 2016 10:41 pm
by admin

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

Posted: Mon Dec 05, 2016 12:25 am
by petzchen
Appreciate your feedback. Thanks.

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

Posted: Mon Jul 06, 2020 1:21 pm
by VijayTripathi
The Question is asking about if we can pass a char to charAt(). Now as per the information given by you it can, but till the compilation level. If it is the case can you please tell me what happens in the background if the statement is written as "Make it to 65 characters".charAt('A');

Is this tries to convert it into its ASCII value and search for the index position of the ASCII value? or something else!
Thanks in advance!

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

Posted: Mon Jul 06, 2020 5:00 pm
by admin
Well, a char is an integral data type. So, you can pass 'A' to charAt. Integral value of 'A' i.e. 65 will be passed. It will be like calling charAt(65).
You can try printing the value of i after this statement and see what happens:
int i = 'A';

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

Posted: Mon Jul 06, 2020 11:54 pm
by VijayTripathi
Yes, I have tried this but even after having string value length as 104. I am getting this error.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 104(Still why?)
The tested code is:
char checkChar = "This is a string of length 104".charAt('A');

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

Posted: Tue Jul 07, 2020 12:59 am
by admin
As mentioned above, charAt('A') is same as charAt(65). Do you have more than 65 characters in your string "This is a string of length 104" ? What do you think the character at index 65 is in your string?

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

Posted: Tue Jul 07, 2020 2:03 am
by VijayTripathi
admin wrote:
Tue Jul 07, 2020 12:59 am
As mentioned above, charAt('A') is same as charAt(65). Do you have more than 65 characters in your string "This is a string of length 104" ? What do you think the character at index 65 is in your string?
you are right. Thanks!