Question) Given this code what is the output?
boolean b = false;
if(b = true)
{
System.out.println("Yes");
}
Options)
Explanation) If you are puzzled looking at the output observe the code again. Inside the if condition you have an assignment operator(=) and not a equality operator(==) . Assignment will always be true regardless of anything and hence the code inside the for loop will always execute. Hence the output will be "Yes". Also note that b will be set to "True".
boolean b = false;
if(b = true)
{
System.out.println("Yes");
}
Options)
- Yes
- Nothing is printed
Explanation) If you are puzzled looking at the output observe the code again. Inside the if condition you have an assignment operator(=) and not a equality operator(==) . Assignment will always be true regardless of anything and hence the code inside the for loop will always execute. Hence the output will be "Yes". Also note that b will be set to "True".
No comments:
Post a Comment