About Question com.enthuware.jfcja.v8.2.206 :

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
a.bond
Posts: 3
Joined: Sun Jan 16, 2022 7:40 am
Contact:

About Question com.enthuware.jfcja.v8.2.206 :

Post by a.bond »

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.

admin
Site Admin
Posts: 9584
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.jfcja.v8.2.206 :

Post by admin »

What happened when you tried to compile and run it by putting it in a simple test program?
If you like our products and services, please help us by posting your review here.

a.bond
Posts: 3
Joined: Sun Jan 16, 2022 7:40 am
Contact:

Re: About Question com.enthuware.jfcja.v8.2.206 :

Post by a.bond »

admin wrote:
Sun Jan 16, 2022 12:52 pm
What happened when you tried to compile and run it by putting it in a simple test program?
It works fine in command line - result = 3.
I tried in IDE and it works fine also (with some additional highlighting in that area).
if_cond.jpg
if_cond.jpg (102.1 KiB) Viewed 805 times
Is it some kind of exception? I tried to google it, but did not succeed to find any topics.

admin
Site Admin
Posts: 9584
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.jfcja.v8.2.206 :

Post by admin »

No, it is not an exception. If you go through the explanation carefully, it actually explains it. See this part:
... There is a single =, which assigns false to flag. Thus, flag becomes false and the condition becomes false, ...
.

You need to understand that "flag=false" is an expression. In Java, all expressions have a value. In case of expressions that involve the = sign (i.e. the assignment operator), the value of the expression is the same as the value that is being assigned to the variable on the left side of =. This is an important concept and is explained thoroughly in section 6.1.2 of JFCJA Fundamental book.

So, the statement if(flag = false){ is actually doing two things - 1. it assigns false to the flag variable and 2. it essentially transforms the if statement to if(false). You can now figure out the rest. Let me know if you still have trouble understanding.
If you like our products and services, please help us by posting your review here.

a.bond
Posts: 3
Joined: Sun Jan 16, 2022 7:40 am
Contact:

Re: About Question com.enthuware.jfcja.v8.2.206 :

Post by a.bond »

admin wrote:
Mon Jan 17, 2022 8:42 am
No, it is not an exception. If you go through the explanation carefully, it actually explains it. See this part:
... There is a single =, which assigns false to flag. Thus, flag becomes false and the condition becomes false, ...
.

You need to understand that "flag=false" is an expression. In Java, all expressions have a value. In case of expressions that involve the = sign (i.e. the assignment operator), the value of the expression is the same as the value that is being assigned to the variable on the left side of =. This is an important concept and is explained thoroughly in section 6.1.2 of JFCJA Fundamental book.

So, the statement if(flag = false){ is actually doing two things - 1. it assigns false to the flag variable and 2. it essentially transforms the if statement to if(false). You can now figure out the rest. Let me know if you still have trouble understanding.
Thank you a lot for your support!
p.s. I'm on a chapter 5.4. right now.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest