About Question enthuware.ocajp.i.v8.2.1350 :

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

Moderator: admin

Post Reply
nikitos
Posts: 21
Joined: Mon Oct 24, 2016 6:55 am
Contact:

About Question enthuware.ocajp.i.v8.2.1350 :

Post by nikitos »

How does it parse directly double value?
Float.valueOf("12.3");

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

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

Post by admin »

Not sure I understand your question. "12.3" is not a double. It is a String.
Paul.
If you like our products and services, please help us by posting your review here.

qmwuzapz
Posts: 3
Joined: Sun Jan 29, 2017 1:54 pm
Contact:

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

Post by qmwuzapz »

admin wrote:Not sure I understand your question. "12.3" is not a double. It is a String.
Paul.
i have the same question as nikitos.
because you cannot do like

Code: Select all

Float f = 12.5
EDIT:
found this:
Float (Java Platform SE 7 ) wrote:The numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is not equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string
"1.00000017881393421514957253748434595763683319091796875001d"
results in the float value 1.0000002f; if the string is converted directly to float, 1.0000001f results.

amit4sun
Posts: 1
Joined: Fri Sep 22, 2017 6:21 am
Contact:

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

Post by amit4sun »

Need some help with the same concept.

How does compiler catch this :- Integer intObj = 123.3;
But not this :-

Float f = null;
f = Float.valueOf("12.3");
String s = f.toString(); int i = Integer.parseInt(s); // One reason i can think of is, output of toString() is not decided till runtime ?

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

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

Post by admin »

Yes, compiler doesn't know the value of s because the compiler can't execute any code. That is why the compiler does not know that s points to a String that cannot be parsed into an int.
If you like our products and services, please help us by posting your review here.

Pierce
Posts: 2
Joined: Thu Dec 28, 2017 10:02 pm
Contact:

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

Post by Pierce »

Gentlemen,

I'll dare to ask this question again since there were no answer provided yet.

Yes, we understand that "12.3" is not a double. It is a String.
But the value it represents - 12.3 - is double, not float.
How come that during the convertion from string 12.3 it becomes a float?

Thank you.

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

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

Post by admin »

Because the method that parses the String argument and converts it to float is coded to accept a String that doesn't end with f. Here is what the JavaDoc API description of Float.valueOf (which is what Float.parseFloat invokes internally) says:
Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type.
If you like our products and services, please help us by posting your review here.

Pierce
Posts: 2
Joined: Thu Dec 28, 2017 10:02 pm
Contact:

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

Post by Pierce »

Okay, now it's more clear.
Thank you.

Rick84
Posts: 12
Joined: Tue Mar 06, 2018 9:54 am
Contact:

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

Post by Rick84 »

This is another question where the answers help find the solution;

The first two answers are:
12
13

But the code says:

Code: Select all

System.out.println("i = "+i);
So those answers are never correct. I think they should be:
i = 12
i = 13

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

Will the value of f not be unreachable from the catch block? (There were qs in other tests in which we try to print a variable declared in the try block in the catch block and it doesn't compile in these cases.) So I marked trouble: null!

Am I missing something?

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

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

Post by admin »

Why do you think it is unreachable?
An unreachable statement causes compilation error. So even if you think it is unreachable, why do you think it will print trouble: null?
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

An unreachable statement causes compilation error.
Yes Yes :o
1. I thought it would print trouble:null because f is initialized to null outside the try block (and so is an instance field).
2. There were questions similar to this in the previous tests.. I don remember the q number, though; where the answer was "compile time error" I guess when we try to access a value from the catch block which was in try. The scenario might have been slightly different.

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

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

Post by admin »

If there is a compilation error, how will the program run to produce any output?
You really need to go through a good book before attempting mock questions.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

I actually got your point. Thank you..!
And I have read Kathy Sierra's complete OCA syllabus! :)

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

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

Post by admin »

You might want to try the one by Jeanne Boyarsky or Mala Gupta.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

Yupp yup. Thank you!

creigelde
Posts: 1
Joined: Tue Nov 26, 2019 12:29 am
Contact:

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

Post by creigelde »

It is a compile-time error if a statement cannot be executed because it is unreachable. This means that the control flow of your program can't get to that statement, but you assume that they would be. The compiler analyzes the flow, and reports these statements to you as error messages. It is a reliable indicators of logical error in your program.

When the compiler reports an unreachable statement, it typically points you to the statement. When that happens, you can follow the flow of control from top to bottom to discover why the statement can never be reached. There are quite strict rules when statements are reachable in java. These rules are design to be easily evaluated and not to be 100% accurate. It should prevent basic programming errors.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 32 guests