Please open the menu to show more

Sunday, August 13, 2017

very very simple but tricky question asked during Java interviews

Tell me the output of this code and explain

class Test 
{
public static void main(String[] args)
{
boolean b = false; // lets assume it is line number 1

if(b = true) // lets assume it is line number 2
{
System.out.println("if block executes");
}
else
{
System.out.println("else block executes");
}

}


output of code is =>
                    if block executes


explanation of code:
  ## at line number 1 we are assignning false in a boolean variable b
  ## at line number 2 we are again assigning true inside variable b using 
        assignment operator (that is =)
   ## so new value of variable b is true, so result inside if statement 
         becomes true
  ## and we know in case of true condition the block of if will be 
         executed 


              
     

11 comments:

  1. Very tricky.....but now you make this question very simple :)
    Thank you sir

    ReplyDelete
  2. good trick sir inside if condition....

    ReplyDelete
  3. good trick sir inside if condition....

    ReplyDelete
  4. Hmm .. very tricky and important thing to be remembered..
    Thank you sir

    ReplyDelete
  5. Hmm .. very tricky and important thing to be noticed.
    Thank you sir

    ReplyDelete
  6. Please enter more questions here like above.
    Thank u sir...

    ReplyDelete