my thread of basic Java questions

Discussion in 'Java' started by denim_genes@hotmail.com, Apr 13, 2008.

  1. Guest

    Why is the following true?:

    (ok || (n1 < 0))

    And what the heck is "ok"?

    My guess: any String or character or whatever is "true" on its own,
    since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    answer to the question would still be true.
     
    , Apr 13, 2008
    #1
    1. Advertising

  2. Mark Space Guest

    wrote:
    > Why is the following true?:
    >
    > (ok || (n1 < 0))
    >
    > And what the heck is "ok"?
    >
    > My guess: any String or character or whatever is "true" on its own,
    > since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    > answer to the question would still be true.


    It isn't true, and strings in Java have double quotes around them.

    ok is obviously a variable that's been declared elsewhere, just like n1.
     
    Mark Space, Apr 13, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 13, 12:18 am, Mark Space <> wrote:
    > wrote:
    > > Why is the following true?:

    >
    > > (ok || (n1 < 0))

    >
    > > And what the heck is "ok"?

    >
    > > My guess: any String or character or whatever is "true" on its own,
    > > since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    > > answer to the question would still be true.

    >
    > It isn't true, and strings in Java have double quotes around them.
    >
    > ok is obviously a variable that's been declared elsewhere, just like n1.


    It's true... I'm looking at the answer key right now...

    int dinosaur = 0;

    dinosaur == true ?
     
    , Apr 13, 2008
    #3
  4. Mark Space Guest

    wrote:
    > On Apr 13, 12:18 am, Mark Space <> wrote:
    >> wrote:
    >>> Why is the following true?:
    >>> (ok || (n1 < 0))
    >>> And what the heck is "ok"?
    >>> My guess: any String or character or whatever is "true" on its own,
    >>> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    >>> answer to the question would still be true.

    >> It isn't true, and strings in Java have double quotes around them.
    >>
    >> ok is obviously a variable that's been declared elsewhere, just like n1.

    >
    > It's true... I'm looking at the answer key right now...
    >
    > int dinosaur = 0;
    >
    > dinosaur == true ?


    It's false. I don't care about your answer key. Get a compiler and try
    it, or better yet learn the language.

    Your latest one here is wrong too, ints aren't even comparable to booleans.:

    init:
    deps-jar:
    Compiling 1 source file to
    C:\Users\Brenden\Dev\misc\FinalizeTest\build\classes
    C:\Users\Brenden\Dev\misc\FinalizeTest\src\finalizetest\Main.java:31:
    incomparable types: int and boolean
    if( dinosaur == true )
    1 error
    BUILD FAILED (total time: 2 seconds)
     
    Mark Space, Apr 13, 2008
    #4
  5. Guest

    On Apr 13, 2:18 am, Mark Space <> wrote:
    > wrote:
    > > On Apr 13, 12:18 am, Mark Space <> wrote:
    > >> wrote:
    > >>> Why is the following true?:
    > >>> (ok || (n1 < 0))
    > >>> And what the heck is "ok"?
    > >>> My guess: any String or character or whatever is "true" on its own,
    > >>> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    > >>> answer to the question would still be true.
    > >> It isn't true, and strings in Java have double quotes around them.

    >
    > >> ok is obviously a variable that's been declared elsewhere, just like n1.

    >
    > > It's true... I'm looking at the answer key right now...

    >
    > > int dinosaur = 0;

    >
    > > dinosaur == true ?

    >
    > It's false. I don't care about your answer key. Get a compiler and try
    > it, or better yet learn the language.
    >
    > Your latest one here is wrong too, ints aren't even comparable to booleans.:
    >
    > init:
    > deps-jar:
    > Compiling 1 source file to
    > C:\Users\Brenden\Dev\misc\FinalizeTest\build\classes
    > C:\Users\Brenden\Dev\misc\FinalizeTest\src\finalizetest\Main.java:31:
    > incomparable types: int and boolean
    > if( dinosaur == true )
    > 1 error
    > BUILD FAILED (total time: 2 seconds)


    I know the language, hence why I was confused as to this "reliable"
    answer key.
     
    , Apr 13, 2008
    #5
  6. "Mark Space" <> wrote in message
    news:NAhMj.5686$...
    > wrote:
    >> On Apr 13, 12:18 am, Mark Space <> wrote:
    >>> wrote:
    >>>> Why is the following true?:
    >>>> (ok || (n1 < 0))
    >>>> And what the heck is "ok"?
    >>>> My guess: any String or character or whatever is "true" on its own,
    >>>> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    >>>> answer to the question would still be true.
    >>> It isn't true, and strings in Java have double quotes around them.
    >>>
    >>> ok is obviously a variable that's been declared elsewhere, just like n1.

    >>
    >> It's true... I'm looking at the answer key right now...
    >>
    >> int dinosaur = 0;
    >>
    >> dinosaur == true ?

    >
    > It's false. I don't care about your answer key. Get a compiler and try
    > it, or better yet learn the language.
    >
    > Your latest one here is wrong too, ints aren't even comparable to
    > booleans.:

    [ SNIP ]

    This question brings up the always popular programming trivia contest...what
    are the truth values in various programming languages? Not always easy to
    remember...

    For example, in awk "0" is true because it's a non-null string value.
    However, in Perl "0" is false (but "0.0" is true). Apparently in REBOL the
    integer 0 is true.

    Does the language in question (1) use two boolean values, or (2) does it
    define truth values for all values of datatypes, or (3) does it define two
    boolean values *and* also truth values for values of other datatypes?
    Example of #3 being Javascript.

    Then there's the convention in UNIX that a program is considered to have
    succeeded if it sets an exit code of 0, which can catch the novice shell
    script programmer by surprise...

    I think there was an attempt to design a bool class in C++ quite some back
    but they couldn't quite design one that was completely correct, hence the
    bool datatype. Of course you can still initialize a bool with 0...

    AHS
     
    Arved Sandstrom, Apr 13, 2008
    #6
  7. don't you need a boolean or something that can be converted to a
    boolean by the compiler for that to work?
     
    Chase Preuninger, Apr 13, 2008
    #7
  8. Arne Vajhøj Guest

    wrote:
    > Why is the following true?:
    >
    > (ok || (n1 < 0))
    >
    > And what the heck is "ok"?
    >
    > My guess: any String or character or whatever is "true" on its own,
    > since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    > answer to the question would still be true.


    What is ok declared as ?

    Arne
     
    Arne Vajhøj, Apr 13, 2008
    #8
  9. Arne Vajhøj Guest

    Arne Vajhøj wrote:
    > wrote:
    >> Why is the following true?:
    >>
    >> (ok || (n1 < 0))
    >>
    >> And what the heck is "ok"?
    >>
    >> My guess: any String or character or whatever is "true" on its own,
    >> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    >> answer to the question would still be true.

    >
    > What is ok declared as ?


    And what is its value ?

    Arne
     
    Arne Vajhøj, Apr 13, 2008
    #9
  10. Guest

    On Apr 13, 11:51 am, Arne Vajhøj <> wrote:
    > Arne Vajhøj wrote:
    > > wrote:
    > >> Why is the following true?:

    >
    > >> (ok || (n1 < 0))

    >
    > >> And what the heck is "ok"?

    >
    > >> My guess: any String or character or whatever is "true" on its own,
    > >> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    > >> answer to the question would still be true.

    >
    > > What is ok declared as ?

    >
    > And what is its value ?
    >
    > Arne


    boolean ok = true;
    int n1 = 100;
    int n2 = 0;

    They forgot to put that in the question. I guess it makes sense now.
     
    , Apr 13, 2008
    #10
  11. Arne Vajhøj Guest

    wrote:
    > On Apr 13, 11:51 am, Arne Vajhøj <> wrote:
    >> Arne Vajhøj wrote:
    >>> wrote:
    >>>> Why is the following true?:
    >>>> (ok || (n1 < 0))
    >>>> And what the heck is "ok"?
    >>>> My guess: any String or character or whatever is "true" on its own,
    >>>> since it exists. Thus, "ok" could be replaced by "dinosaur", and the
    >>>> answer to the question would still be true.
    >>> What is ok declared as ?

    >> And what is its value ?

    >
    > boolean ok = true;
    > int n1 = 100;
    > int n2 = 0;
    >
    > They forgot to put that in the question. I guess it makes sense now.


    Yes.

    (ok || (n1 < 0)) = (true || (100 > 0)) = (true || false) = true

    Arne
     
    Arne Vajhøj, Apr 13, 2008
    #11
  12. Lew wrote:
    > wrote:
    >>> Why is the following true?:
    >>> (ok || (n1 < 0))

    >
    >>> boolean ok = true;
    >>> int n1 = 100;
    >>> int n2 = 0;
    >>>
    >>> They forgot to put that in the question. I guess it makes sense now.

    >
    > Arne Vajhøj wrote:
    >> Yes.
    >>
    >> (ok || (n1 < 0)) = (true || (100 < 0)) = (true || false) = true

    >
    > Nitpick: Since || is an early-out operator, the (100 < 0) clause is
    > never evaluated once 'ok' is found to be true.
    >
    > (ok || (n1 < 0)) == (true || <unevaluated>) == true


    Good point.

    If it is one of those Java language test, then that could be
    considered important.

    Arne
     
    Arne Vajhøj, Apr 13, 2008
    #12
  13. Mark Space Guest

    Arne Vajhøj wrote:
    > Lew wrote:
    >> wrote:
    >>>> Why is the following true?:
    >>>> (ok || (n1 < 0))

    >>
    >>>> boolean ok = true;
    >>>> int n1 = 100;
    >>>> int n2 = 0;
    >>>>
    >>>> They forgot to put that in the question. I guess it makes sense now.

    >>
    >> Arne Vajhøj wrote:
    >>> Yes.
    >>>
    >>> (ok || (n1 < 0)) = (true || (100 < 0)) = (true || false) = true

    >>
    >> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
    >> never evaluated once 'ok' is found to be true.
    >>
    >> (ok || (n1 < 0)) == (true || <unevaluated>) == true

    >
    > Good point.
    >
    > If it is one of those Java language test, then that could be
    > considered important.


    Yup, if( ok || (n1++ < 0) ){}, what is the value of n1 after this
    statement?

    Still 100 because the ++ never got evaluated.

    I'd like to see a link to this "answer key" the OP is referring too. If
    it's really Java and not some other language, I don't think it's a very
    good one. But I'd like to check it out myself.
     
    Mark Space, Apr 13, 2008
    #13
  14. Mark Space wrote:
    > Arne Vajhøj wrote:
    >> Lew wrote:
    >>> wrote:
    >>>>> Why is the following true?:
    >>>>> (ok || (n1 < 0))
    >>>
    >>>>> boolean ok = true;
    >>>>> int n1 = 100;
    >>>>> int n2 = 0;
    >>>>>
    >>>>> They forgot to put that in the question. I guess it makes sense now.
    >>>
    >>> Arne Vajhøj wrote:
    >>>> Yes.
    >>>>
    >>>> (ok || (n1 < 0)) = (true || (100 < 0)) = (true || false) = true
    >>>
    >>> Nitpick: Since || is an early-out operator, the (100 < 0) clause is
    >>> never evaluated once 'ok' is found to be true.
    >>>
    >>> (ok || (n1 < 0)) == (true || <unevaluated>) == true

    >>
    >> Good point.
    >>
    >> If it is one of those Java language test, then that could be
    >> considered important.

    >
    > Yup, if( ok || (n1++ < 0) ){}, what is the value of n1 after this
    > statement?
    >
    > Still 100 because the ++ never got evaluated.
    >
    > I'd like to see a link to this "answer key" the OP is referring too. If
    > it's really Java and not some other language, I don't think it's a very
    > good one. But I'd like to check it out myself.


    That type of code is not good code, but is exactly what some tests
    like to check.

    Arne
     
    Arne Vajhøj, Apr 13, 2008
    #14
  15. Mark Space Guest

    Arne Vajhøj wrote:

    > That type of code is not good code, but is exactly what some tests
    > like to check.


    Yup, exactly what I was thinking. Not a good pattern, but something
    that you would expect to see on a test.
     
    Mark Space, Apr 13, 2008
    #15
  16. Guest

    On Apr 13, 4:19 pm, Mark Space <> wrote:
    > Arne Vajhøj wrote:
    > > That type of code is not good code, but is exactly what some tests
    > > like to check.

    >
    > Yup, exactly what I was thinking. Not a good pattern, but something
    > that you would expect to see on a test.


    This is an introductory programming course, hence the poor quality of
    questions (if that's any excuse).

    I found myself handing in assignments with increased efficiency (not
    having variables I didn't need) and error-checking when they weren't
    even on the grading criteria. Really a basic course.

    Which is why I'm pissed... I totally didn't study anything related to
    making classes (no time), so I may have lost marks on that part of the
    exam I just wrote. I assumed I would handle their basic questions, but
    they did ask some rather weird ones.

    Anyway, thnx.
     
    , Apr 14, 2008
    #16
  17. On Sun, 13 Apr 2008 13:00:38 -0400, Lew wrote:
    > Nitpick: Since || is an early-out operator, the (100 < 0) clause is
    > never evaluated once 'ok' is found to be true.
    >
    > (ok || (n1 < 0)) == (true || <unevaluated>) == true



    Partly related I often use:

    if (object != null && object.hasSomeCondition) {
    executeFooBaa();
    }

    I rely on the fact that the second part of the condition never gets
    executed if object == null. This seems a little sloppy on my behalf but
    it looks nicer than:

    if (object != null) {
    if (object.hasSomeCondition) {
    executeFooBaa();
    }
    }

    What does everyone else do?

    Lionel.
     
    Lionel van den Berg, Apr 14, 2008
    #17
  18. Mark Space Guest

    Lionel van den Berg wrote:

    > if (object != null && object.hasSomeCondition) {
    > executeFooBaa();
    > }


    When Arne called my code "not good," I think he was referring to the use
    of side effects and the post increment ++ operator in a comparison. Or
    at least I assumed he was.

    Short circuit operators like above are fine. Side effects can be
    questionable. ++ and -- are both especially subject to abuse.
     
    Mark Space, Apr 14, 2008
    #18
  19. Mark Space wrote:
    > Lionel van den Berg wrote:
    >> if (object != null && object.hasSomeCondition) {
    >> executeFooBaa();
    >> }

    >
    > When Arne called my code "not good," I think he was referring to the use
    > of side effects and the post increment ++ operator in a comparison. Or
    > at least I assumed he was.
    >
    > Short circuit operators like above are fine. Side effects can be
    > questionable. ++ and -- are both especially subject to abuse.


    I use that construct myself.

    Extremely strictly speaking then crashing versus not crashing could
    be considered a side effect.

    But I don't think this is a real problem.

    The ++ and -- could be a problem, but even those are
    not the worst.

    It is:

    if(something || sometmethodthatchangestate())

    that really make it hard to follow the logic.

    Arne
     
    Arne Vajhøj, Apr 15, 2008
    #19
  20. Roedy Green Guest

    On Sat, 12 Apr 2008 20:59:34 -0700 (PDT),
    wrote, quoted or indirectly quoted someone who said :

    >Why is the following true?:
    >
    >(ok || (n1 < 0))


    it isn't. It is true if and only if the boolean variable ok is true or
    if n1 < 0.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Apr 24, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AnonymousFC4
    Replies:
    3
    Views:
    497
    Knute Johnson
    Jul 29, 2005
  2. Engineer
    Replies:
    6
    Views:
    678
    Jeremy Bowers
    May 1, 2005
  3. Replies:
    0
    Views:
    478
  4. Bonnie Johnson
    Replies:
    2
    Views:
    525
    Andrew Thompson
    Jan 10, 2007
  5. Replies:
    4
    Views:
    3,473
Loading...

Share This Page