+ 1

Can some one explain this code

public class Program { public static void main(String[] args) { boolean b=false; if(b=false) System.out.println("1"); else if(b) System.out.println("2"); else if(!b) System.out.println("3"); else System.out.println("4"); } }

21st Aug 2017, 5:05 AM
Srivatshan.P.K
Srivatshan.P.K - avatar
6 Answers
0
if the 1st if statement was if(b==false) then that has been executed and the ans has been 1 (e) cause b==false == means checking if the value is same or not = means assigning a value
21st Aug 2017, 8:45 AM
sayan chandra
sayan chandra - avatar
+ 4
are you sure you copied it as it is? b=false check this
21st Aug 2017, 5:06 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
+ 1
thanks nw I understand
21st Aug 2017, 8:43 AM
Srivatshan.P.K
Srivatshan.P.K - avatar
0
yes it's a objective question here are the options a) fail to compile b)2 c)no output d)4 e)1 f)3 g)runtime error
21st Aug 2017, 5:10 AM
Srivatshan.P.K
Srivatshan.P.K - avatar
0
ans is 3 i mean f
21st Aug 2017, 8:36 AM
sayan chandra
sayan chandra - avatar
0
b=false means b is false now.. so if(b)--> if(false) no execution else if(b)--> else if(false) no execution else if(!b)--> else if(not b)--> else if (not false)--> else if(true) executes else part dont execute.. if(True)//if(not False)//if(8)//if(7==7) this things always ececutes
21st Aug 2017, 8:41 AM
sayan chandra
sayan chandra - avatar