Page 1 of 1

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

Posted: Sun Aug 25, 2013 6:49 am
by kumarkhiani
Isnt the answer B same as answer E. Program ends without Printing anything. It does not throws / catches any exceptions either.

B : It will end without exceptions and will print nothing.
E : None of the above

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

Posted: Sun Aug 25, 2013 7:22 am
by admin
No, None of the above could also mean that it will print or do something that is not mentioned in any of the other options.

HTH,
Paul.

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

Posted: Mon Feb 03, 2014 4:18 am
by njaal123
I don't understand why answer B isn't the correct answer. I even tried running the code in IntelliJ with "closed" as a parameter. I got "Process finished with exit code 0" as output

Since "closed" does not equal "open", and the second if is inside the first, none of the remaining code will run?

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

Posted: Fri May 30, 2014 2:24 pm
by wokingtown11
When i copy and paste this code into eclipse and run it i get the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at TestClass4.main(TestClass4.java:10)

which is answer A, which is supposedly incorrect.
Any help?

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

Posted: Fri May 30, 2014 7:59 pm
by admin
Did you specify command line parameter as mentioned in the problem statement?
HTH,
Paul.

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

Posted: Mon Jun 02, 2014 3:26 pm
by wokingtown11
Oh dear oh dear, I didnt see that. cringe. paul thanks for the reply

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

Posted: Tue Jul 22, 2014 5:12 am
by Marthinus
Hi all,

The below statement in the explanation doesn't seem to be true. When I run the code with the arguments it works fine. Am I missing something?

"The else actually is associated with the second if. So had the command line been : java Test open, it would have executed the second if and thrown ArrayIndexOutOfBoundsException."

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

Posted: Tue Jul 22, 2014 6:22 am
by admin
Explanation is correct. Please post the exact code and the arguments that you are trying to run.

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

Posted: Tue Jul 22, 2014 7:02 am
by Marthinus
My apologies. Is seen now what I did wrong. I passed Test in as an argument *facepalm*. The world makes sense again.

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

Posted: Fri Feb 20, 2015 2:16 am
by Harini
Hi,

In the above question, the else block is given with proper indentation. So how do we come to a conclusion that it belongs to the second if block only? So does that mean that indentation doesn't hold good for such if-else blocks ? Please advise.

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

Posted: Fri Feb 20, 2015 2:27 am
by admin
In Java (unlike Python), indentation is meant for humans only. Compiler doesn't care about indentation.

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

Posted: Mon Oct 31, 2016 10:12 pm
by zhengye1
Which means the compiler will always find the nearest "if" to match the "else" no matter what the indentation looks like?

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

Posted: Tue Nov 01, 2016 9:54 pm
by admin
That is correct.

Consider the following statement.

Code: Select all

if ( num > 0 )   
  if ( num < 10 ) 
    System.out.println( "aaa" ) ;
  else
    System.out.println( "bbb" ) ;
In the example above, the else is indented with the inner if statement. It prints "bbb" only if num > 0 and num >= 10, which means it prints only if num is 10 or greater.
In the example below, the else is indented with the outer if statement.

Code: Select all

if ( num > 0 )   
   if ( num < 10 ) 
      System.out.println( "aaa" ) ;
else
   System.out.println( "bbb" ) ;
This prints if num <= 0. Depending on which if the else is paired with, you can get different results.

Java doesn't pay attention to indentation. So both examples above are the same to Java, which means that it must pick one version over the other.

Which one does it pick? It picks the first one. This phenomenon of having to pick between one of two possible if is called the dangling else.

Rule

An else matches with the nearest, previous, unmatched if that's not in a block.
By not in a block, I mean that the else is outside the block that the if is inside.

Thus,

Code: Select all

if ( num > 0 )   
  if ( num < 10 ) // Previous unmatched if
    System.out.println( "aaa" ) ;
  else  // Matches with previous unmatched if
    System.out.println( "bbb" ) ;
What if we wanted the else to match with the first if?
Then, we need braces.

Code: Select all

if ( num > 0 )  // Previous unmatched if
{
   if ( num < 10 ) // Unmatched, but in block
     System.out.println( "aaa" ) ;
}
else  // Matches with previous unmatched if not in block
   System.out.println( "bbb" ) ;
The else matches with the previous, unmatched if not in a block. This happens to be the outer if. There is an unmatched if that's the inner if, but that's in a block, so you can't match to it. Notice the else sits outside of the block. An else can not match to a previous if if the else is outside a block, while the previous if is inside.
Another Example

Let's add a second else.

Code: Select all

if ( num > 0 )    // Outer if
  if ( num < 10 ) // Inner if
    System.out.println( "aaa" ) ;
  else  // Matches inner if
    System.out.println( "bbb" ) ;
else  // Matches outer if
   System.out.println( "ccc" ) ;
Using our rule, the first else matches the inner if. Since the inner if is already matched, you can't do any match it with the next else.
The second else must match with the outer if because the inner if is already matched.

Each else must match with a unique if

Every else must match with a unique if. You can't have two or more else matching to the same if. If there is no match, then your program won't compile.
Each else be preceded immediately by a valid if

Furthermore, every else has to have a valid if statement just before it. Fortunately, your program fails to compile if you don't do this.

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

Posted: Fri Mar 10, 2017 5:36 am
by Javier
Hi Paul!
I copied and pasted the program and i am running it and the program is showing
ArrayIndexOutOfBoundsException.
(I am passing in the command line as arguments: closed)
What am I missing Paul?
Thank you so much, I am stuck :(

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

Posted: Fri Mar 10, 2017 6:37 am
by admin
Most probably your code is not exactly same as the one given in the problem statement.

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

Posted: Fri Mar 10, 2017 7:06 am
by Javier
Paul, of course you were right!
I made a mistake because I put a ";" at the end of the first if statement...
Sorry for my question.
And thank you so much for your quick answer!!

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

Posted: Sat Mar 11, 2017 3:45 am
by Mihai1977
Hi, I can not understand something, pls help:
The rule for this case is: "An else matches with the nearest, previous, unmatched if that's not in a block."
The first if() condition fails (args[0] not being "open").
The second if() condition fails too (args[1] doesn't exists).
So, applying the rule, the nearest, previous unmatched(not in a block) if is the second if and the else should match with it. Why it does not print:"Go away" + nothing?

thx.

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

Posted: Sat Mar 11, 2017 6:24 am
by admin
Since the else goes with the previous unmatched if, that means the second if and the else together form one statement. This whole statement makes the if true part of the first if i.e. it will be executed only if the first if condition is true.

There is no else part in the first if. Thus, since the first if condition is false, there is nothing to execute.

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

Posted: Sat Mar 11, 2017 2:12 pm
by Mihai1977
Thx, it's clear now.

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

Posted: Tue Oct 20, 2020 3:56 pm
by Dreamweaver
I think, the rule is that "the else belongs to the closest if"