• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

answer the question from scjp5.0

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Titanic {
public static void main(String[] args) {
Boolean bl = true;
boolean b2 = false;
boolean b3 = true;
if((bl & b2) | (b2 & b3) & b3)
System.out.print("alpha ");
if((bl = false) | (b1 & b3) | (bl | b2))
System.out.print("beta "};
}
}

What is the result?

beta

alpha

alpha beta

Compilation fails.

No output is produced.

An exception is thrown at runtime.
answer is exception is thrown at runtime
i want to ask can we have more than 2 expresions for the above operator
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you run the program and see the exception..??? cause as far as i see the program should not throw any exception... it wont actually compile cause this you should close it with ) noot }... apart from that the program is fine
anyway please quote your sources so its also easier for us
[ November 26, 2007: Message edited by: abhishek pendkay ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nikunj,

The output you have given is wrong. It wont complile itself because of this line

System.out.print("beta "};

Need to close the paranthesis here...
System.out.print("beta ");

If this has been corrected then your code will compile but it wont give any output.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did actually try to compile and run this example (after correcting the paretheses issue and variable names (sometimes it's b1 - B one, and sometimes bl - BL)) and I get following exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from boolean to Boolean

Of course, the easy solution is to make all variables primitive. But my question is why doesn't the compiler unbox the Boolean reference variable to a primitive?? I run it on Eclipse with Java 1.5
 
nikunj thakur
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but i want to ask can put 3 expressions in for logical operators
 
Gosia Wittemann
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nikunj, yes you can. After converting all variables to a primitive the code compiles, runs, just doesn't generate any output.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Originally posted by Gosia Wittemann:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from boolean to Boolean



After fixing the brackets and one's to el's (or vice-versa) boxing does indeed work for me. No need to convert everything to a primitive. Are you sure eclipse is compiling your code under 1.5? Check your preferences->Java->compiler setting.

Cheers, Jared.

P.S. There is no output.
 
Gosia Wittemann
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jared, thanks so much, that was the trick, I used the 1.5 JRE, but with 1.4 compliance
Anyway, searching for this bug made me install Eclipse Europa in the long last
 
author
Posts: 9025
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nikunj,

We're very happy that you're using our book to help you in your studies, and you are more than welcome to post mock questions from the book here in this forum for discussion. Speaking for myself and for all authors of mock exam questions, from now on, we need you to tell everyone the source of the mock exam question you're discussing.

Thanks,

Bert
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic