About Question com.enthuware.ets.scjp.v6.2.363 :

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 com.enthuware.ets.scjp.v6.2.363 :

Post by ETS User »

Although this construct is correct :

Code: Select all

	boolean flag = true;
[b]	if(flag = false){
		System.out.println("1");[/b]
	}else if(flag){
		System.out.println("2");
	}else if(!flag){
		System.out.println("3");
	}else 	System.out.println("4");
it obviously has no sense as the System.out.println("1"); as well as all other code in this block will be never executed. Just a curious, is there any situation when it will be?

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

Re: About Question com.enthuware.ets.scjp.v6.2.363 :

Post by admin »

No, you are right, it will not be executed. The question is trying to test you on the construct flag = false.

BTW, you will find code in real exam that doesn't make sense in real life but you are expected to decipher it and understand what it is trying to do.

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

devlam
Posts: 50
Joined: Sun Nov 10, 2013 4:39 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.363 :

Post by devlam »

I should say that the final else is unreachable code because there is an elsif flag and an elsif !flag before that last else.
Why does the compiler not signal this unreachable code?

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

Re: About Question com.enthuware.ets.scjp.v6.2.363 :

Post by admin »

There is no elseif in java. So you should not consider else if as one statement. You need to group it like this:

Code: Select all

        if (flag = false) {
            System.out.println("1");
        } else {
            if (flag) {
                System.out.println("2");
            } else {
                if (!flag) {
                    System.out.println("3");
                } else {
                    System.out.println("4");
                }
            }
        }

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

devlam
Posts: 50
Joined: Sun Nov 10, 2013 4:39 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.363 :

Post by devlam »

Yes ok. But my point was that the statement
System.out.println("4")
is unreachable code, because on if (!flag) the result will always be true, which is not detected by the compiler.

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

Re: About Question com.enthuware.ets.scjp.v6.2.363 :

Post by admin »

devlam wrote:Yes ok. But my point was that the statement
System.out.println("4")
is unreachable code, because on if (!flag) the result will always be true, which is not detected by the compiler.
1. flag is a variable. Compiler cannot assume its value even if it is evident by looking at the code. Compiler can only use the values of compile time constants to determine if the code is unreachable or not.
2. if statement is granted an exception by the language designers. Please read this: http://docs.oracle.com/javase/specs/jls ... #jls-14.21

HTH,
Paul.
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 3 guests