Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

operator and assignment  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question ID :954698538220
Which statments about the output of the following programs are true?
public class TestClass
{
public static void main(String args[ ] )
{
int i = 0 ;
boolean bool1 = true;
boolean bool2 = false;
boolean bool = false;
bool = (bool2 & method1("1")); //1
bool = (bool2 && method1("2")); //2
bool = (bool1 | method1("3")); //3
bool = (bool1 | | method1("4")); //4
}
public static boolean method1(String str)
{
System.out.println(str);
return true;
}
}
A. 1 will be part of the output
B. 2 will be part of the output
C. 3 will be part of the output
D. 4 will be part of the output
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think correct answers are A, B and D
When we use '&' or '|' operator both expressions on either side of the operator will be executed.
but in case '&&' or '| |' the second expression will not be executed
Siva
 
shiwei liu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the response, actually the answer is 1 and 3 will appear on the output
I donot know why

Originally posted by Siva Prasad:
I think correct answers are A, B and D
When we use '&' or '|' operator both expressions on either side of the operator will be executed.
but in case '&&' or '| |' the second expression will not be executed
Siva


 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually 1 and 3 will output because in the fourth one
bool = (bool1 | | method1("4")); //4
It doesn't evaluate the second half because bool1 is true. Therefore, no call to method1("4") is made.
 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shame me!
Yes A and C are right answers
Siva
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, I understand.
donot blame yourself, man, that is my fault
 
I want my playground back. Here, I'll give you this tiny ad for it:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!