About Question com.enthuware.jfcja.v8.2.206 :
Posted: Sun Jan 16, 2022 12:08 pm
What will the following code print?
boolean flag = true;
if(flag = false){
System.out.println("1");
}else if(flag){
System.out.println("2");
}else if(!flag){
System.out.println("3");
}else System.out.println("4");
You had to select 1 option(s)
1 - Answered Incorrectly
2 - Answered Incorrectly
3 - Correct answer
4 - Answered Incorrectly
Compilation error. - Answered Incorrectly
Description from test:
At the beginning, flag is true. In the first if condition, we have flag = false. Notice that it is not flag == false. There is a single =, which assigns false to flag. Thus, flag becomes false and the condition becomes false, therefore, 1 is not printed.
In the first else if, again, since flag is false, 2 is not printed.
In second else if, !flag resolves to !false, which means true, therefore, 3 is printed.
Finally, since one of the else-if conditions has been satisfied, the last else is not executed.
----------------------------------------------------------------------------------------------------
This is question from test. Correct answer is 3.
But I doubt it. Because after "if" there should be "condition" statement. Is (flag = false) conditional statement? For me it seems "=" is assignment operator.
boolean flag = true;
if(flag = false){
System.out.println("1");
}else if(flag){
System.out.println("2");
}else if(!flag){
System.out.println("3");
}else System.out.println("4");
You had to select 1 option(s)
1 - Answered Incorrectly
2 - Answered Incorrectly
3 - Correct answer
4 - Answered Incorrectly
Compilation error. - Answered Incorrectly
Description from test:
At the beginning, flag is true. In the first if condition, we have flag = false. Notice that it is not flag == false. There is a single =, which assigns false to flag. Thus, flag becomes false and the condition becomes false, therefore, 1 is not printed.
In the first else if, again, since flag is false, 2 is not printed.
In second else if, !flag resolves to !false, which means true, therefore, 3 is printed.
Finally, since one of the else-if conditions has been satisfied, the last else is not executed.
----------------------------------------------------------------------------------------------------
This is question from test. Correct answer is 3.
But I doubt it. Because after "if" there should be "condition" statement. Is (flag = false) conditional statement? For me it seems "=" is assignment operator.