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

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.1175 :

Post by ETS User »

This behaviour is not defined in the api, so it should be chose by the implementator.. Shouldn't it?
Is this question part of a real exam?

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

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

Post by admin »

Which behavior is not defined by the API? As per, http://docs.oracle.com/javase/7/docs/ap ... r,%20char)
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned.
Yes, this method is covered in the exam.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest »

I chose E for my answer..

The answer D:

"String".replace('g','g')=="String"

seems to be an implementation detail, becouse the api states:

If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

So following the api specification becouse 'g' occurs in our String a new Object should be created, the implementator chose otherwise, but following the api the correct answer should be None of this


Kind regards

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

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

Post by admin »

I see now what you mean. Yes, you are right. It is possible that it is implementation dependent.
-Paul.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

The String object String is different from "String" that is on left hand side isnt it? and equals returns false if 2 objects with a same value. According to my knowledge every time you use the String a new String is created.

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

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

Post by admin »

gparLondon wrote:The String object String is different from "String" that is on left hand side isnt it? and equals returns false if 2 objects with a same value. According to my knowledge every time you use the String a new String is created.
No, equals returns true if 2 String objects contain the same value.
No, Strings are interned. So a new String object may not be created every time you use a String. You might want to go through this http://stackoverflow.com/questions/1057 ... -interning
Or check out other articles and tutorial on "interning" of String in java.
If you like our products and services, please help us by posting your review here.

itsriaz
Posts: 10
Joined: Tue Nov 18, 2014 10:01 am
Contact:

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

Post by itsriaz »

Hi I am confused about the right answer after reading the explanation by Guest.

Which one is the right answer D or E ?

"I chose E for my answer..

The answer D:

"String".replace('g','g')=="String"

seems to be an implementation detail, becouse the api states:

If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

So following the api specification becouse 'g' occurs in our String a new Object should be created, the implementator chose otherwise, but following the api the correct answer should be None of this


Kind regards
I chose E for my answer..

The answer D:

"String".replace('g','g')=="String"

seems to be an implementation detail, becouse the api states:

If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

So following the api specification becouse 'g' occurs in our String a new Object should be created, the implementator chose otherwise, but following the api the correct answer should be None of this

Kind regards"

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

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

Post by admin »

You are right. There is a problem. However, it seems like the problem is with the wording of the JavaDoc API. In version 7, it says, "Otherwise, a new String object is created ...", while in Java 8, it says, "Otherwise, a String object is returned that...". Notice the absence of the word "new" in Java 8.

I am guessing that they realized the confusion caused by the original statement and fixed it.

Of course, the exam is for version 7 and we should go by the JavaDoc for version 7 but I think it is just a mistake in JavaDoc so I will leave the answer as well as this discussion as it is so that the reader can make an informed decision.

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

itsriaz
Posts: 10
Joined: Tue Nov 18, 2014 10:01 am
Contact:

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

Post by itsriaz »

Thanks very much for the explanation. So it means E is the correct answer for Java7.

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

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

Post by coder007 »

This statement from the question returns false:

"String".replace('g','G') == "String".replace('g','G');

But I don't understand why so? It is known, that Java manage String constant pool where all already defined strings are stored in. If both sides of equality return the String with the same value (i.e. StrinG == StrinG), they should point to the same object contained in the constant pool. It isn't?

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

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

Post by admin »

The JavaDoc API clearly states, "...Otherwise, a new String object is created ...". A String object from the String pool not returned in this case. Therefore, == will not return true as both sides have different objects.
If you like our products and services, please help us by posting your review here.

Venceslas
Posts: 15
Joined: Thu Feb 05, 2015 3:50 pm
Contact:

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

Post by Venceslas »

The answer is dependant from the JVM implementation. With Oracle JVM that is answer:
"String".replace('g','g')=="String"

With IBM JVM that is answer "none of these":

IBM JVM does not test that both char are identical, below you will find the code for an IBM JVM:

/**
* Copies this String replacing occurrences of the specified character
* with another character.
*
* @param oldChar the character to replace
* @param newChar the replacement character
* @return a new String with occurrences of oldChar replaced by newChar
*/
public String replace(char oldChar, char newChar) {
int index = indexOf(oldChar, 0);
if (index == -1) return this;

char[] buffer = new char[count];
System.arraycopy(value, offset, buffer, 0, count);
do {
buffer[index++] = newChar;
} while ((index = indexOf(oldChar, index)) != -1);
return new String(0, count, buffer);
}

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

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

Post by admin »

For the purpose of the exam, you should only rely on Oracle's JVM.
If you like our products and services, please help us by posting your review here.

anathema
Posts: 3
Joined: Sat Jun 20, 2015 12:31 pm
Contact:

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

Post by anathema »

After reading the previous posts on this thread, I assume or I think that only String literals are being added on the constant pool and the Strings that are being returned by String methods are new String objects that are not part/or being placed on the pool unless the intern method is being called.

If I declare a String variable with a value "hello world" (String a = "hello world")then during compilation time does that String literal is being added already on the String constant pool? or it is being added during runtime?

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

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

Post by admin »

The string pool is created at run time. So all the values in the pool are added at run time when the program is run.
If you like our products and services, please help us by posting your review here.

skissh
Posts: 7
Joined: Tue Jan 31, 2017 4:16 pm
Contact:

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

Post by skissh »

Just a heads-up. I got to this discussion from the link in exam file japv8.ets for this question, yet the Wed Dec 03, 2014 8:21 am post mentions the Java 7 exam.
This is what my implementation does:

Code: Select all

    System.out.println("String".replace('g','g') == "String");  // true
    System.out.println("String".replace('x','g') == "String");  // true
    System.out.println("String".replace("g","g") == "String");  // false
    System.out.println("String".replace("x","g") == "String");  // true

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

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

Post by admin »

This question exists in both the versions of the question bank that is why it is linked from japv7.ets as well as japv8.ets.

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

lonewolf
Posts: 1
Joined: Sat Jun 10, 2017 8:33 am
Contact:

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

Post by lonewolf »

Just failed with the "String" == "String".replace("g", "g") question ...
This raises a more fundamental issue for me: The answer for this cannot be deducted from anything in any preparation book but is hidden somewhere in the API description .. and even there .. as we see .. it still can be discussed.

Why would anybody ask such details in a Java Associate "Exam"?

I have a master in CS plus morte than 20 years experience in the IT industry and I'm doing the OCA more or less for fun. But questions like this "really make me angry" :-). If I look at most of the "puzzles" in those exams, I would fire the people writing such obfuscated, non-explicit code in anythingb like a production system right away. Seems like the "exam community" takes fun from checking stuff that no programmer should ever rely on in practice. This one is only an extreme example.

But back: Is it necessary to be able to recite the API doc like one would recite a phone directory as an Asperger patient?
I'm not autistic - and hence my abilities (and also motivation) to be able to recite a phone directory from the first to the last page are limited.

My tactics for the exam will be to not try to copy learn the API decription and accept some 10-20% below 100% - but sometimes there are e.g. too many of those in a single test :-(

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

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

Post by admin »

You are right that it may be a little too much for the exam. But then it may not be. Official exam objectives explicitly mention, "Create and manipulate Strings". replace method is an important method of this class.

Also, an important thing that we have noticed about the questions in the exam is that it does contain questions that test you on edge cases, boundary conditions, and special cases. On the stuff we normally don't pay much attention to. Not too many but some.

So it is up to you how much time do you want to spend on learning for the exam. If your objective is to score 80-90%, then yes, you probably do not need to spend too much time on such finer details.

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

ehammond
Posts: 3
Joined: Fri Sep 01, 2017 4:52 pm
Contact:

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

Post by ehammond »

Can someone please explain why "String".replace('g','g')=="String" evaluates to true? I understand that a reference to a String object is returned (and followed the reference specific discussion above), but don't understand what 'evaluates to true' means here. Thanks.

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

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

Post by admin »

The whole statement "String".replace('g','g')=="String" is an expression. Evaluation means to find out the value of this expression. "evaluates to true" means that when the JVM evaluates this expression, it determines that the value is true.
If you like our products and services, please help us by posting your review here.

bomicbon
Posts: 5
Joined: Sun Jan 21, 2018 7:33 pm
Contact:

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

Post by bomicbon »

Oh my god this has got to be the most nit-pickiest quirk I've seen thus far. #SADBOYZ

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests