Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Sybex:Java OCA/OCP 8 Practice Tests Chapter 3 question 25 mock answer incorrect fact

 
Ranch Foreman
Posts: 98
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sybex:Java OCA/OCP 8 Practice Tests Chapter 3 question 25 mock answer incorrect fact

25. Ternary operations require both right-hand expressions to be of compatible data types.



It should say only when combined with the assignment operator.
There is no requirement that the second and third expressions in ternary operations have the same data type.

by alinvlad05
 
Vlad Alin
Ranch Foreman
Posts: 98
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if this is really a mistake because the ternary was inside a method with String return type.
It should say because of String return type?
 
Vlad Alin
Ranch Foreman
Posts: 98
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know what i mean. It should say because of assignment. By itself it is allowed to be different types.
 
Vlad Alin
Ranch Foreman
Posts: 98
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package mode;
public class Transportation {
  public static String travel(int distance) {
     return distance<1000 ? "train" : 10;
  }
  public static void main(String[] answer) {
     System.out.print(travel(500));
  }
}

25. C. Ternary operations require both right-hand expressions to be of compatible data types.
In this example, the first right-hand expression of the outer ternary operation is of type
String, while the second right-hand expression is of type int.

"outer" should be removed because there is no inner ternary
 
Vlad Alin
Ranch Foreman
Posts: 98
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it refers to the fact that inner operation is distance<1000
 
Vlad Alin
Ranch Foreman
Posts: 98
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it refers to the fact that inner operation is distance<1000
 
Saloon Keeper
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I spent about 15 minutes on this, first I tried to come up with a use like you were saying and failed.

Then I went and read section 15.25 of the JLS to see if you were right.

(Note: the book said "compatible" not "identical")

There is way, way, way more to a conditional expression than any of us really wants to know, but I would say from skimming thru that section, it is not possible to use incompatible types in a way you are suggesting.

I think it is great you are looking at these things so carefully, despite this being Java 8 material, it is always good to be cautious, clear and specific.

I think this one was fine as they wrote it.
 
author & internet detective
Posts: 40912
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. Knowing the ternary operator can be used without an assignment operator isn't in scope for the exam.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic