About Question enthuware.ocajp.i.v7.2.1173 :
Moderator: admin
-
- Posts: 9
- Joined: Sun Aug 25, 2013 2:36 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1173 :
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
B : It will end without exceptions and will print nothing.
E : None of the above
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Mon Feb 03, 2014 3:57 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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?
Since "closed" does not equal "open", and the second if is inside the first, none of the remaining code will run?
-
- Posts: 4
- Joined: Tue May 06, 2014 3:37 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at TestClass4.main(TestClass4.java:10)
which is answer A, which is supposedly incorrect.
Any help?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
Did you specify command line parameter as mentioned in the problem statement?
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Tue May 06, 2014 3:37 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
Oh dear oh dear, I didnt see that. cringe. paul thanks for the reply
-
- Posts: 7
- Joined: Tue Jul 22, 2014 5:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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."
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."
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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.
-
- Posts: 7
- Joined: Tue Jul 22, 2014 5:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
My apologies. Is seen now what I did wrong. I passed Test in as an argument *facepalm*. The world makes sense again.
-
- Posts: 2
- Joined: Fri Feb 20, 2015 2:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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.
-
- Posts: 17
- Joined: Wed Jan 07, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
Which means the compiler will always find the nearest "if" to match the "else" no matter what the indentation looks like?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
That is correct.
Consider the following statement.
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.
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,
What if we wanted the else to match with the first if?
Then, we need braces.
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.
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.
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 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" ) ;
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" ) ;
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" ) ;
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" ) ;
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.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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!!
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!!
-
- Posts: 9
- Joined: Tue Feb 28, 2017 2:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
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.
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.
-
- Posts: 9
- Joined: Tue Feb 28, 2017 2:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
Thx, it's clear now.
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1173 :
I think, the rule is that "the else belongs to the closest if"
Who is online
Users browsing this forum: No registered users and 130 guests