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

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

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
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 »

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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 »

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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 »

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

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

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

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

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

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

Post by admin »

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: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

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

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

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

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

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

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

Post 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.
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 »

Thx, it's clear now.

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post by Dreamweaver »

I think, the rule is that "the else belongs to the closest if"

Post Reply

Who is online

Users browsing this forum: No registered users and 61 guests