• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't understand answer for Chapter 2 Review Question 7 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Joe Herbert
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm a first time poster here and a complete greenhorn to programming. I'm having difficulty understanding the answer given to question 7 in chapter 2.

Question 7 goes like this:

7. What is the output of the following code?



A. 5
B. 4
C. 10
D. 8
E. 7
F. The code will not compile because of line 4.

The answer given is:

D. As you learned in the section “Ternary Operator,” although parentheses are not required, they do greatly increase code readability, such as the following equivalent statement:
System.out.println((x > 2) ? ((x < 4) ? 10 : 8) : 7)
We apply the outside ternary operator first, as it is possible the inner ternary expression may never be evaluated. Since (x>2) is true, this reduces the problem to:
System.out.println((x < 4) ? 10 : 8)
Since x is greater than 2, the answer is 8, or option D in this case.

I can't understand why the brackets have been put where they have been put. Why have the brackets been put around x<4 and 10:8 and not 7? I'm sure whatever has been expressed in the book is logical, I'm just completely lost on what the logic is! The book suggests to use brackets to help understand thee question but I can't see how I'm meant to have known to use the brackets in this way?

I hope this makes sense! Thanks in advance for all the help
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not that we expect you to be able to insert parentheses, so much as the exam does. Parentheses are not required for ternary operations and this example highlights that. The exam can mix multiple ternary operators in a question.

To solve it, you need to understand operator precedence discussed throughout the chapter and read from left to right. For example < and > has a higher precedence than the ternary operations. The first ? you encounter begins the first ternary operation. The next ? begins the second ternary operation. When the first : is encountered, you're still in the second ternary expression, so you can break it up as:

The shorthand above is all you really need to solve it. We wrote out extra parentheses in the solution guide just to help you see how things are evaluated.
 
Ahmed Bin S
Ranch Hand
Posts: 378
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
operators have precedence.

if i have a * b + c, because a * b will be evaluated first and then c added to the result I can rewrite it as (a * b) + c to make the code easier to understand.
i could also rewrite it as (a * b) + (c) if i wanted to but putting () around the c doesn't make anything easier to understand, on the contrary, it clutters the code.
 
Joe Herbert
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Scott and Ahmed,

Thank you both for the quick replies. I found the answer 5 mins after I typed it out! the bottom of the page showed me similar posts and someone else had posted the exact same question! But thank you for the answers, they cleared up the doubt I had.

Scott, I've gone through chapter 1 and 2 of this study guide and I'm finding it hard to understand the material. It's beginning to dawn on me that this book isn't for complete beginners. I've never done any programming of any sort and got this book simply because I'm trying to go for the Java SE 8 Programmer certification. What's your thoughts on this approach, would you say it's ill advised trying to jump into this material without any prior knowledge of programming? Do you have any suggestions for a book I could either refer to first, or use side by side with this guide?
Do you have any advice on this?

Thanks again.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, as we wrote in the Introduction, if you've never programmed before, you might want to pick up a book that emphasizes programmer theory. While the book does present the Java topics from start to finish, it is primarily focused on being a study guide for the exam. I believe the OCA FAQ has some recommended starting books.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Herbert wrote: Do you have any suggestions for a book I could either refer to first, or use side by side with this guide? Do you have any advice on this?


Scott Selikoff wrote: While the book does present the Java topics from start to finish, it is primarily focused on being a study guide for the exam. I believe the OCA FAQ has some recommended starting books.

As does Chapter 1 of the book currently in your hands . In the book we recommend:
  • Head First Java - old but a great intro
  • Thinking in Java (older editions are free)
  • Java for Dummies


  • I'd also add Murach's Beginning Java to the list. I read that after we published the OCA book so it didn't make the intro.

    Any of thoe four books would be a great pre-req to get you up to the level you need to be at for our book.
     
    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 Joe Herbert,

    First of all, a warm welcome to CodeRanch!

    Joe Herbert wrote:I can't understand why the brackets have been put where they have been put. Why have the brackets been put around x<4 and 10:8 and not 7? I'm sure whatever has been expressed in the book is logical, I'm just completely lost on what the logic is! The book suggests to use brackets to help understand thee question but I can't see how I'm meant to have known to use the brackets in this way?

    You can add parantheses almost anywhere you want as long as it results in valid Java syntax. So for every opening paranthesis, you need a matching closing one. If I encounter the given statement in real codeI would probably only add parantheses to the inner ternary operator, because (in my opinion) it improves readibilityBut for the exam you need to know what's valid Java syntax and what's not. As the explanation in the study guide illustrates you can add parantheses around both conditions as wellAnd you are wondering why you can't add parantheses around 7. If you really want to, you can easily add parantheses around 7. In fact, you can add even three (or more) pairs of parantheses around 7 (but I doubt if you would do this in real code, because it doesn't improve the readibility at all)And if you really want to go crazy (I mean really crazy), you can add double parantheses around every variable, every literal, every condition, and every ternary operator. Just for fun and because it is possible Illustrated in this code snippetIt compiles successfully, but it's (as you can see for yourself) very hard to read. So don't do this in real code (and you won't see this kind of parantheses abuse on the actual exam).

    Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic