Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

answer the question from scjp5.0

 
nikunj thakur
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • 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 ]
 
Saroja Eshwar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.
 
Gosia Wittemann
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic