Page 1 of 1

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

Posted: Tue Mar 20, 2012 8:17 am
by ETS User
Hey guys, this question says that this statement: System.out.println("12345".charAt(6)); does not throws the StringIndexOutOfBoundException, but when you try to run it, actually it throws. So what's the correct answer for this one?

Thanks

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

Posted: Tue Mar 20, 2012 10:58 am
by admin
Hi,
Did you read the detailed explanation provided with the answer. It explains why that option is not correct.

Further, the explanation does not say that it does not throw StringIndexOutOfBoundsException.

HTH,
Paul.

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

Posted: Fri Jul 20, 2012 10:04 pm
by Guest
Under the answer:
It will throw StringOutOfBoundsException.
There is no such exception. The correct name is StringIndexOutOfBoundsException. But that is also not the correct answer

StringIndexOutOfBoundsException should be the correct answer when compiled. Why does it say that it is not?

Thank you.

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

Posted: Sun Jul 22, 2012 9:31 am
by admin
As per the API documentation of this method ( http://docs.oracle.com/javase/6/docs/ap ... harAt(int) ), this method throws IndexOutOfBoundsException. Although, in practice the method throws StringIndexOutOfBoundsException, which is a subclass of IndexOutOfBoundsException, the implementation is free to throw some other subclass of IndexOutOfBoundsException. Thus, you should rely only on the published API documentation instead of what it actually throws.

HTH,
Paul.

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

Posted: Wed Oct 17, 2012 4:00 pm
by xvitcoder
The answer of "It will throw a StringOutOfBoundsException" option is wrong for sure

It says:
"There is no such exception. The correct name is StringIndexOutOfBoundsException. But that is also not the correct answer."

It should be:
"There is no such exception. The correct name is StringIndexOutOfBoundsException. If the option would be StringIndexOutOfBoundsException then then this option would be the correct answer"

It does not matter what API documentation says. People are reading what the question asks and the question is:
"What will the following code print?" and not "What kind of exception will be thorwn?"

So for sure the code in this question will not print a "IndexOutOfBoundsException".

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

Posted: Wed Oct 17, 2012 5:07 pm
by admin
xvitcoder wrote:The answer of "It will throw a StringOutOfBoundsException" option is wrong for sure

It says:
"There is no such exception. The correct name is StringIndexOutOfBoundsException. But that is also not the correct answer."

It should be:
"There is no such exception. The correct name is StringIndexOutOfBoundsException. If the option would be StringIndexOutOfBoundsException then then this option would be the correct answer"

It does not matter what API documentation says. People are reading what the question asks and the question is:
"What will the following code print?" and not "What kind of exception will be thorwn?"

So for sure the code in this question will not print a "IndexOutOfBoundsException".

1. None of the options says it will print StringIndexOutOfBoundsException.
2. "StringIndexOutOfBoundsException" is not printed because of System.out.println("12345".charAt(6));.
3. In fact, the statement System.out.println("12345".charAt(6)); prints nothing. This statement just throws a StringIndexOutOfBoundsException. But as explained in the explanation you cannot rely on the fact that it will throw StringIndexOutOfBoundsException because the API says it throws IndexOutOfBoundsException.

In other words, the given statement does NOT print StringIndexOutOfBoundsException. It throws IndexOutOfBoundsException, which is the correct answer.

HTH,
Paul.

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

Posted: Sun Nov 04, 2012 11:54 am
by gliesian
I think the folks at Oracle either need to fix their documentation or change their code.

-- Robert

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

Posted: Tue Feb 04, 2014 3:24 pm
by convertor
in practice, I've met that 6th's versions of Java on Macs and Wins are different.
despite any documentation.
so the more important is reality than what is written in API documentation, I think.

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

Posted: Tue Nov 04, 2014 9:41 am
by Kevin_C
I agree with xvitcoder that the question is contradictive with the correct answer. The question states: "What will the following code print?"

I know in the API documentation it's String#charAt(int index) throws IndexOutOfBoundsException, so in theory it could also throw an ArrayIndexOutOfBoundsException as it's also a subclass of IndexOufOfBoundsException. The point here however, is that it's not what the question asks, with the given piece of code. Is there even a remote case possible where the piece of code provided:

Code: Select all

System.out.println("12345".charAt(6));
gives any other exception than StringIndexOutOfBoundsException? Personally I think this was a trick question, even if you know that String's charAt throws IndexOufOfBoundsException in the API documentation.

Maybe the answers should be changed from: "It will throw a StringIndexOutOfBoundsException." to "It can only throw an StringIndexOutOfBoundsException." or something like that. Then its more clear that the question is about String's charAt in general, instead of this specific case with the code provided.

EDIT: Ok, I missed the part where it said StringOutOfBoundsException instead of StringIndexOutOfBoundsException in the answer.. my bad.. :roll:

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

Posted: Tue Nov 04, 2014 1:19 pm
by admin
Good discussion :)

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

Posted: Wed Jan 21, 2015 6:38 pm
by Alina_Lapina
On Monday, January, 19 2015, I was at the real 1Z0-803 exam and got an almost similar question. There wasn't the "IndexOutOfBoundsException" opthion. There was only "StringIndexOutOfBoundsException", "ArrayIndexOutOfBoundsException" and several checked exceptions. It look like they have fixed it. Relax! And good luck on your exam!

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

Posted: Wed Jan 21, 2015 8:09 pm
by admin
Well, that's great news!
thank you for sharing your experience and information.

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

Posted: Mon Nov 21, 2016 1:32 am
by mj.anjuthan
This is really a tricky ambiguous question.

Considering the following code, what should we answer?

Code: Select all

static void testMethod() throws IOException{
	throw new FileNotFoundException();
}
	
public static void main(String[] args) throws IOException{
	testMethod();		
}
What should be the answer to the following two possible questions?

Question 1) What will the following code print?
Question 2) What will the exception thrown when the code is compiled and run?

Say the options of both questions have "It throws IOException" and "It throws FileNotFoundException" and other options are obviously wrong.

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

Posted: Mon Nov 21, 2016 8:22 am
by admin
FileNotFoundException. Always go with most specific answer, if possible.
-Paul.

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

Posted: Fri Jun 29, 2018 9:35 am
by ananias
Bellow is the code I extracted from The String class:

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

I still don't uderstand how IndexOutOfBoundsException will be thrown instead of StringIndexOutOfBoundsException

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

Posted: Fri Jun 29, 2018 10:10 am
by admin
Please go through the complete discussion above.

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

Posted: Sat Jul 28, 2018 2:03 am
by flex567
Is there a list of ALL things we need to know for the examp by heart?
All APIs and which methods, All exception classes, All Date constants ...

I know that there are posts about that in the forum but is there one post that contain all the information ?

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

Posted: Tue Aug 21, 2018 1:20 pm
by Javier
Hi Enthuware!
Now I have a big doubt. So ArrayIndexOutOfBoundsException and StringIndexBoundsException shouldn´t be answered like correct in the test? Should we answer IndexOutOfBoundsException any time?
Thank you in advance!

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

Posted: Tue Aug 21, 2018 11:07 pm
by admin
Yes, in our opinion, you should go with what the JavaDoc says. In this case, it is IndexOutOfBoundsException.

I will also suggest you not to worry too much about this issue for the exam.

HTH,
Paul.

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

Posted: Wed Aug 22, 2018 11:36 am
by Javier
Hi again Paul!

In this example we have to say IndexOutOfBoundsException instead of StringIndex...(I read in the API that is throwing IndexOutOfBoundsException)

"test".substring(2,1); // IndexOutOfBoundsException

Am I right Paul?

Thank you very much!

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

Posted: Wed Aug 22, 2018 9:29 pm
by admin
Yes, that is correct.