• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Question 6 (KB Book Chapter 4)

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here's the question:

Given:
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?
A.beta
B.alpha
C.alpha beta
D.Compilation fails.
E.No output is produced.
F.An exception is thrown at runtime.


I am not completely satisfied with the explanation on the answer. Could you please carefully tell me what exactly is happening at this line?
if((bl = false) | (b1 & b3) | (bl | b2))

Thanks!
 
Ranch Hand
Posts: 135
  • 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 "};
}
}
--------------------------------------------------------

In the statement

if((bl = false) | (b1 & b3) | (bl | b2))

at first we are assigning false to boolean variable b1 . thats why now b1 is not 'true' rather it is 'false'..after that (b1 & b3) is just (false & true ) which is finally 'false' similarly (bl | b2) is just (false | false ) i.e false.

final experssion is just like (false|false|false)--> false

hence thats if() block is not executed...

Hope it will help you..
reply
    Bookmark Topic Watch Topic
  • New Topic