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

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

Moderator: admin

Post Reply
amort42
Posts: 1
Joined: Sat May 11, 2013 8:55 am
Contact:

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

Post by amort42 »

I think the information provided under the first option is a little confusing:
You can apply a label to any code block or a block level statement (such as a for statement) but not to individual statement such as :- loopX : int i = 10;
It makes it seem as if this means a label cannot be applied to any individual statement, but from another question we see that the following will compile:

JILL: System.out.println(c);

And indeed, loopX : i = 10; will compile

So is it just to declarations that a label cannot be applied, or are there other cases?

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

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

Post by admin »

Actually, it should say that labels cannot be applied to declarations. As per JLS Section 14/7 ( http://docs.oracle.com/javase/specs/jls ... l#jls-14.7 ), statements can have labels.

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

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

Output:
Loop Lable line
In Finally

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

Post by ramon.carrascom »

I don't understand very well what is it happening here. I thought that

Code: Select all

break <label>
could only be used when <label> is pointing the same loop in which it is called:

Code: Select all

MY_LABEL: for (int i=0; i<10; i++) {
                        if (i==5) break MY_LABEL; }
          // After break goes here
But tracing source code of test question using Eclipse, I see that...

Code: Select all

loop :         // 1
	      {
	         System.out.println("Loop Lable line");
	         try{
	            for (  ;  true ;  i++ ){
	               if( i >5) break loop;       // 2  -------->> 2 -AFTER FINALLY, TRACE COMES BACK HERE AGAIN <<---------------
	            }
	         }
	         catch(Exception e){
	            System.out.println("Exception in loop.");
	         }
	         finally{
                    // -------->> 1 - FIRST, AFTER BREAK, TRACE GOES HERE <<------------
	            System.out.println("In Finally");      // 3
	         }
                 // -------------->> 3 - AND TRACE FINISHES HERE <<----------------------
I also tried to substitute break with continue, and it gives a compiler error (as I thought it would happen with break). What is it happening here? Thanks

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

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

Post by admin »

There are precise rules about where you can put break without a label and break with a label. These are given in 14.15 The break Statement. Although this would be a bit too much for the OCAJP exam but no harm in going through it and it will clear all your concepts about break.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

Very interesting. This seems like effectively a forward goto. I have found that you don't even need a loop, or if statement to break to an outer label. You can just...do it.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

admin wrote:
Sat Jul 07, 2018 9:00 am
__JJ__ wrote:
Sat Jul 07, 2018 7:19 am
Very interesting. This seems like effectively a forward goto. I have found that you don't even need a loop, or if statement to break to an outer label. You can just...do it.
Not really. You can't use break just about anywhere. You need an enclosing loop block to break out of. (or a switch block).
Yes that's what I thought would be the case. But I did check this out before I posted. This is what I had knocked up to get an understanding of how far it might be taken:

Code: Select all

package oca8.practice;
class T7{
    public static void main(String args[]){
        int i = 0;
    
        OUTER1: {
            OUTER2:{
                OUTER3:
                    OUTER4:
                    INNER:                 
                    {
                        System.out.println("Before try...");
                        try{
                            break OUTER1;
                        } catch(Exception e){
                            System.out.println("Exception in loop.");
                        } finally{
                            System.out.println("In Finally");           
                        }
                    }
                    System.out.println("AFTER INNER");                    
            } System.out.println("AFTER OUTER2");
        } System.out.println("AFTER OUTER1");        
    }
}
OUTPUT:

Code: Select all

Before try...
In Finally
AFTER OUTER1
There is no loop, no conditional (if/else), just a try block.
What's interesting is I just tried it without the try block

Code: Select all

package oca8.practice;
class T7A{
    public static void main(String args[]){
        int i = 0;
    
        OUTER1: {
            OUTER2:{
                OUTER3:
                    OUTER4:
                    INNER:                 
                    {
                        System.out.println("Before try...");
                        break OUTER1;
                    }
                    System.out.println("AFTER INNER");                   //error: unreachable statement  
            } System.out.println("AFTER OUTER2");
        } System.out.println("AFTER OUTER1");        
    }
}

It fails compilation, with unreachable statement, which suggests a try block is seen as a pseudo-conditional, and may (almost certainly does) explain why the answer to one of the other questions (a while(true) inside a try block) was not "compilation fails with unreachable code".

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

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

Post by admin »

As per JLS 8 section 14.15:
If no switch , while , do , or for statement in the immediately enclosing method,
constructor, or initializer contains the break statement, a compile-time error
occurs.
and
A break statement with label Identifier attempts to transfer control to the enclosing
labeled statement (§14.7) that has the same Identifier as its label; this statement,
which is called the break target, then immediately completes normally. In this case,
the break target need not be a switch , while , do , or for statement.
So you are right!
Paul.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

Is it correct to say that short of using System.exit(), the finally block is always executed in a try/catch/finally and try/finally blocks, no matter what?

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

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

Post by admin »

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

soncrash
Posts: 8
Joined: Sat Aug 27, 2016 2:51 pm
Contact:

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

Post by soncrash »

I thought that you cannot catch Exception in catch block, if the code does not have a chance to throw Exception:

I speak about that part:

Code: Select all

         try{
            for (  ;  true ;  i++ ){
               if( i >5) break loop;       // 2
            }
         }
         catch(Exception e){
            System.out.println("Exception in loop.");
         }
how for loop is able to throw exception in try block?

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

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

Post by admin »

The rule is about checked exceptions but Exception is a special case because RuntimeException extends Exception and any code can throw RTE. Compiler may not know that the code will throw a RTE and so it must allow a try/catch for java.lang.Exception class.

If you try it with other checked exception classes such as IOException, it will not work.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

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