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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ETS User

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

Post by ETS User »

How is a floating point hexadecimal value written? I thought 0x1 could be represented as a floating point number? Isn't 0x1 equal to 1 in base 10 decimal? If it is 1 in base 10 would its floating point representation be 1.0f? 0_o

Javanaut

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

Post by Javanaut »

I found this from the java language specification (JLS) -
For hexadecimal floating-point literals, at least one digit is required (in either the whole number or the fraction part), and the exponent is mandatory, and the float type suffix is optional. The exponent is indicated by the ASCII letter p or P followed by an optionally signed integer.


When I put a P and some arbitrary number and end the hex String with the f for floating point it runs and does not encounter a number format exception (NFE). :o

Code: Select all

// modified code to run and experiment with these concepts
// :D
public class Zfloat { 
public static void main(String[] args) { 
	System.out.println(new Zfloat().parseFloat("0xfP2f"));
	}
public float parseFloat(String s) { 

	float f = 0.0f;
	try { 
		f = Float.valueOf(s).floatValue();
		return f;
	}
	catch (NumberFormatException nfe) { 
		System.out.println("Invalid input " + s); 
		f = Float.NaN;
		return f;
	}
	finally { System.out.println("finally");return f; }
	
	
}
}

Michailangelo

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

Post by Michailangelo »

How come the command "return f ;" is executed before the println command? I thought when an exception occurs the control goes to a catch block without executing the rest of the code in the try block.

Also, I want to ask the commands that occur after the finally block are unreachable?

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

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

Post by admin »

Michailangelo wrote:How come the command "return f ;" is executed before the println command? I thought when an exception occurs the control goes to a catch block without executing the rest of the code in the try block.
There is a finally block in the code. The println is coming from there.
Also, I want to ask the commands that occur after the finally block are unreachable?
No, not necessarily. It depends on the code in try and catch blocks. For example, in this question, the try and catch both have return statements. Therefore, there is no way anything after the finally block can be executed.
But in the following example, statement after finally is not unreacheable:

Code: Select all

public void m1(){
   try{
     System.out.println("in try");
   }finally{
     System.out.println("in finally");
   }
     System.out.println("after finally...reachable.");
}


Deepa

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

Post by Deepa »

Hi,

I don't know how to write hexadecimal floating literals... is it important to learn for the exam..?
Please advise...

deepa.patre
Posts: 15
Joined: Thu Dec 13, 2012 9:44 am
Contact:

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

Post by deepa.patre »

Hi all,

I just wanted to clear my concept, so there should not be any code or block of statements after finally block. Incase if it exists the code will not compile. Is it generalised?

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

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

Post by admin »

deepa.patre wrote:Hi all,

I just wanted to clear my concept, so there should not be any code or block of statements after finally block. Incase if it exists the code will not compile. Is it generalised?
No, what you said is not correct. This is exactly I explained in my previous post.

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

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

Post by admin »

Deepa wrote:Hi,

I don't know how to write hexadecimal floating literals... is it important to learn for the exam..?
Please advise...
No, it is not important for the exam.

Saloni R Dangwal
Posts: 1
Joined: Fri Oct 18, 2013 7:26 am
Contact:

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

Post by Saloni R Dangwal »

So does this mean that when return statement is given in the finally block the code after the finally block will become unreachable and the compiler will complain whereas if return statement is there only in try and catch blocks then the code will compile smoothly, finally will be executed and the value from try or catch will be returned ??

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

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

Post by admin »

yes, that sounds right. But I suggest you try out multiple test programs to verify this.
-Paul.

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

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

Post by nikitos »

I have to say, that for me code does not compile, it complaints on unreachable return statement at the end (jdk 1.8).

I choose answer - code will not compile. And I think it's right answer.

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

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

Post by admin »

That is indeed marked as the correct answer.
-Paul.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

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

Post by heleneshaikh »

I had the answer correct, but for a different reason. The answer states that the code would compile if the "return" after the "finally" block wasn't there. However, the parseFloat method only takes in a String as a parameter, and in the possible answers only ints and doubles are passed to the method. Wouldn't that cause a compilation error as well?

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

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

Post by admin »

The options are not code fragments but just English statements mentioning the data that is being passed to the method. But I understand why it might cause confusion and I have updated the options to include quote marks around the input number.

thank you for your feedback!
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests