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

Moderator: admin

Post Reply
kumarkhiani
Posts: 9
Joined: Sun Aug 25, 2013 2:36 am
Contact:

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

Post by kumarkhiani » Sun Aug 25, 2013 6:49 am

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

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

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

Post by admin » Sun Aug 25, 2013 7:22 am

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.
If you like our products and services, please help us by posting your review here.

njaal123
Posts: 2
Joined: Mon Feb 03, 2014 3:57 am
Contact:

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

Post by njaal123 » Mon Feb 03, 2014 4:18 am

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?

wokingtown11
Posts: 4
Joined: Tue May 06, 2014 3:37 pm
Contact:

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

Post by wokingtown11 » Fri May 30, 2014 2:24 pm

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?

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

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

Post by admin » Fri May 30, 2014 7:59 pm

Did you specify command line parameter as mentioned in the problem statement?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

wokingtown11
Posts: 4
Joined: Tue May 06, 2014 3:37 pm
Contact:

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

Post by wokingtown11 » Mon Jun 02, 2014 3:26 pm

Oh dear oh dear, I didnt see that. cringe. paul thanks for the reply

Marthinus
Posts: 7
Joined: Tue Jul 22, 2014 5:07 am
Contact:

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

Post by Marthinus » Tue Jul 22, 2014 5:12 am

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."

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

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

Post by admin » Tue Jul 22, 2014 6:22 am

Explanation is correct. Please post the exact code and the arguments that you are trying to run.
If you like our products and services, please help us by posting your review here.

Marthinus
Posts: 7
Joined: Tue Jul 22, 2014 5:07 am
Contact:

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

Post by Marthinus » Tue Jul 22, 2014 7:02 am

My apologies. Is seen now what I did wrong. I passed Test in as an argument *facepalm*. The world makes sense again.

Harini
Posts: 2
Joined: Fri Feb 20, 2015 2:12 am
Contact:

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

Post by Harini » Fri Feb 20, 2015 2:16 am

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.

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

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

Post by admin » Fri Feb 20, 2015 2:27 am

In Java (unlike Python), indentation is meant for humans only. Compiler doesn't care about indentation.
If you like our products and services, please help us by posting your review here.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

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

Post by zhengye1 » Mon Oct 31, 2016 10:12 pm

Which means the compiler will always find the nearest "if" to match the "else" no matter what the indentation looks like?

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

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

Post by admin » Tue Nov 01, 2016 9:54 pm

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.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 47
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier » Fri Mar 10, 2017 5:36 am

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 :(

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

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

Post by admin » Fri Mar 10, 2017 6:37 am

Most probably your code is not exactly same as the one given in the problem statement.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 47
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier » Fri Mar 10, 2017 7:06 am

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!!

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

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

Post by Mihai1977 » Sat Mar 11, 2017 3:45 am

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.

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

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

Post by admin » Sat Mar 11, 2017 6:24 am

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.
If you like our products and services, please help us by posting your review here.

Mihai1977
Posts: 9
Joined: Tue Feb 28, 2017 2:13 am
Contact:

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

Post by Mihai1977 » Sat Mar 11, 2017 2:12 pm

Thx, it's clear now.

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests