Page 1 of 1

Programming question for 1Z0-803

Posted: Sun Oct 25, 2015 1:59 pm
by Kev1np
Hello,

I've studied with the OCA/OCP JAVA SE 7 (Kathy Sierra, Bert Bates) book and i've done all the standard test (except the last-day one) from enthuware. I'm now taking the two exams from the CD-rom that comed with the book, and some question was a bit tricky. Maybe it will be good to add these in the enthuware bank ?

1) Coud you please tell me why this code will compile as is ? Even if the line B is unreachable.
However, if we remove the comment A, the code will not compile because line A is unreachable. I don't understand why the compiler behaves differently here.

Code: Select all

	public static void main(String args[]){
		int k = 2;
		outer:
			while(true){
				++k;
				inner:
					for(int j = 5; j > 2; j--){
						if(j==3)
							continue inner;
						break outer;
						//System.out.println(k); // A
					}
				System.out.println(k); // B
				continue outer;
			}
		System.out.println(k);
	}
2) octal in decimal number.
i don't find anything about octal number ".". It seems that if we have a "." in an octal number, the 0 is ignored and the number is treated as a decimal. Where can I find doc about this behavior ?

Code: Select all

	   double a = 09.4; // compile
	   double b = 094; // Error. 9 is not an octal digit 
Thanks in advance for your help.

Re: Programming question for 1Z0-803

Posted: Mon Oct 26, 2015 10:37 am
by admin
1. To understand the rules regarding unreachable statements, you need to go through this: https://docs.oracle.com/javase/specs/jl ... #jls-14.21

2. Rules for octal are given here: https://docs.oracle.com/javase/specs/jl ... jls-3.html
Remember that floating point numbers can only be written in decimal or hex. Therefore, 09.4 is not considered octal. It is decimal. But 094 is octal because it starts with 0 and that is why it does not compile because 9 is not a valid digit in octal.

Re: Programming question for 1Z0-803

Posted: Wed Oct 28, 2015 11:54 am
by Kev1np
1. Ok, i got it. It's because the boolean expression of the inner for is not taken into account by the compiler in the flow analysis. If we do not enter the inner for (even if in that special case, we will), we will not reach the break outer statement, therefore line B is consider by the compiler as reachable.
For example, a Java compiler will accept the code:

{
int n = 5;
while (n > 7) k = 2;
}

even though the value of n is known at compile time and in principle it can be known at compile time that the assignment to k can never be executed.
2.
Remember that floating point numbers can only be written in decimal or hex.
I guess I missed this rule.

Thank you for your swift answer.