Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware practice test question regarding the if statement

 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm studying for another attempt at the OCA exam and there was a practice test question in some uCertify materials that our school provides. They use the Enthuware question banks for these. The question is as follows:

Given the following code:


What is the output?

At first glance, I concluded that it would print True False. After compiling it, I understand the when run it's going to kick out True True. The explanation given only confuses me more. It states:

Notice how the last "else" is associated with the last "if." Now, the first if condition returns true so the next 'if' will be executed. In the second 'if' the condition returns false so the else part will be evaluated which prints 'True True.'

Specifically, the part that says "In the second 'if' the condition returns false...

If the condition returns false, wouldn't the code block for the 2nd 'if' statement run? I think I'm getting it twisted in my head due to a lack of reference variables.

I'm sure this is pretty basic stuff and it's how I'm reading it in my head that needs to be corrected. Would someone be kind enough to walk me through the logic in my brain so I can associate things properly?

1. What's true to begin with?
2. Are if(true) and if(false) assignment statements or something else?

I've refactored this code multiple times and the drunk monkey on my shoulder keeps telling me that in the 2nd 'if' statement whatever is being referenced has to true otherwise the code block for the 2nd 'if' statement would run. It reads allowed in my head as "if something is false, do this. if not, do what is in the else statement. The following code makes perfect sense to me:


When I read this, I can easily see that which code block is going to run in the nested 'if' statement is dependent on the value of x (although, I'm still not sure what's true to begin with). Wow! It feels more like philosophy than Java at this point. What's really true man? Two rainbows! What does it all mean?

Your input is much appreciated.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,

First of all, a warm welcome to CodeRanch!

The structure of a (simple) if statement looks like:

That's pretty straightforward.

So if we look at this if-statement:
The condition evaluates to false, so the else branch will be executed.

And if you prefer using variables, this is equivalent:

Oh yeah, 1 final note: don't let the text in the print statements fool you. You can use whatever text you want in a print statement. This code is equivalent to the code snippet in your original post (with an extra print statement):Output:
True True
true false


Hope it helps!
Kind regards,
Roel

PS. Instead of using color-tags around your code snippets, you better use code-tags instead. They improve the readibility of your post (and so you are more likely to get an answer). I added them for you in your post.
 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel,

Thanks for the response and for fixing my code(I'll make sure to format it correctly in the future). Actually, your explanation makes perfect sense to me:

I've got to fight the tendency to overcomplicate how I interpret the code. I read more into the questions than I should.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,
I checked this question and I see that the explanation also shows the simplified code i.e.

This code can be rewritten as follows:



Since you didn't mention it in your post, I was wondering whether you are able to see it at all in Ucertify's application. If not, then their viewer might have a problem displaying some of the content and we will take it up with them to fix it.

thank you,
Paul.
 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

Thanks for checking in. No, the explanation given in Ucertify's exam is exactly what I provided:

"Notice how the last "else" is associated with the last "if." Now, the first if condition returns true so the next 'if' will be executed. In the second 'if' the condition returns false so the else part will be evaluated which prints 'True True.'"

There seem to be a lot of errors with the Ucertify material. Actually, it's why I'm going to switch back to using the Enthuware software instead. The forums provided for each question are much more valuable.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Brumpton wrote:Paul,

Thanks for checking in. No, the explanation given in Ucertify's exam is exactly what I provided:

"Notice how the last "else" is associated with the last "if." Now, the first if condition returns true so the next 'if' will be executed. In the second 'if' the condition returns false so the else part will be evaluated which prints 'True True.'"

There seem to be a lot of errors with the Ucertify material. Actually, it's why I'm going to switch back to using the Enthuware software instead. The forums provided for each question are much more valuable.


Thanks, Brian! I will let them know about this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic