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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

xvitcoder

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

Post 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".

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

gliesian
Posts: 9
Joined: Sun Oct 28, 2012 8:17 pm
Contact:

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

Post by gliesian »

I think the folks at Oracle either need to fix their documentation or change their code.

-- Robert

convertor
Posts: 7
Joined: Mon Jan 27, 2014 6:25 am
Contact:

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

Post 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.

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

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

Post 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:

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

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

Post by admin »

Good discussion :)
If you like our products and services, please help us by posting your review here.

Alina_Lapina
Posts: 15
Joined: Tue Jan 13, 2015 12:10 pm
Contact:

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

Post 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!

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

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

Post by admin »

Well, that's great news!
thank you for sharing your experience and information.
If you like our products and services, please help us by posting your review here.

mj.anjuthan
Posts: 10
Joined: Thu Nov 10, 2016 3:07 am
Contact:

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

Post 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.

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

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

Post by admin »

FileNotFoundException. Always go with most specific answer, if possible.
-Paul.
If you like our products and services, please help us by posting your review here.

ananias
Posts: 3
Joined: Fri Jun 29, 2018 1:33 am
Contact:

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

Post 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

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

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

Post by admin »

Please go through the complete discussion above.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 201
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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 ?

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post 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!

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post 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!

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

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

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest