Answer is C.
To test:
public static void main(String[] args) {
int x = 10;
int y = ++x;
int z = 0;
if(y >= 10 | y <= ++x) {
z = x;
} else {
z = x++;
}
System.out.println(z);
}
The correct answer is the letter C, notice that in the instantiation line of the variable "y" and in the if condition the x receives 2 times a "++", that is, the variable x had an increment in its value 2 times having the value of 12. As if gave true, the value of z was equal to that of x giving the answer 12. The code compiles normally, the difference between using "|" or "||" in if is that the first option when compiling tests the 2 possibilities, but "II" will give true or false testing only the first condition, this was done to run the code faster on some occasions when it is not necessary to test the second condition if the first one already delivers whether it is true or false.
It really depends on the if statement whether or not there is typo. If it is "if(y >= 10 || y <= ++x)", then the answer is 11, as y = 11 so y >= 10, based on short-circuiting, y <= ++x is not executed and z is assigned to the value of 11.
If it is "if(y >= 10 | y <= ++x)", now the | is the bitwise operator so the two sides of the operators must be executed. As two sides are true, true | true is true and x now becomes 12 (due to the y <= ++x). Thus z is assigned to 12.
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
MPignaProTech
2 months, 2 weeks agosamarrrr
1 year, 8 months agocarloswork
2 years, 2 months agoiSnover
2 years, 3 months agoalex_au
2 years, 3 months ago