Sunday, 7 April 2013

MCQ #12

Question) Given this code what is the output?

boolean b = false;
if(b = true)
{
   System.out.println("Yes");
}

Options)

  • Yes
  • Nothing is printed
Answer) Yes

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

t> UA-39527780-1 back to top