Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
ZioCain26676yI guess it's B: 2
But I might be completely wrong, I'm not used to some much shit in a single formula -
On solving prefix and postfix first, it becomes, 1+2/8*2+10%3
So it looks like 2.
@irene why do you think it's undefined? -
7Raiden8756y@phreakyphoenix the prefix and postfix gets processed before any other operation? I didn't know that, cool :)
-
7Raiden8756y@irene Do you think that with -O3 this can change result? I would be surprised to be honest! Also, there are some compiler warning that tells you if/how any optimization is prevented!
-
Is this java och C++? I am not fluent in any of them, but I'm pretty sure that if any such code would be presented to a C++ compiler, the result would be undefined or compiler dependent. But I understand that java is much more rigidly specified than C/C++.
-
7Raiden8756y@stormwise Why do you say so? Do you think that clang or ICC will give different output?
-
@7Raiden Maybe. I know that C/C++ standards contain a lot of compiler dependent and undefined features, and there are even courses explaining how to avoid them and make C code more deterministic. I understand that java has tighten up some of these features so that the behavior is more defined.
-
7Raiden8756y@irene Ah, that I agree! And also very true that -O3 *might* change the instruction order, so it might as well be!!
-
pain04861716yok so it boils down to this
1 + 1 / 8 * 2 + 10 % 3
then apply the order of operations just before addition and subtraction you get:
1 + 1/16 + 1
(1 is the result of 10% 3 and is applied at the same level as multiplication and division)
And since this are integers 1/16 is not 0.0625 but rather 0 so its more like
1 + 0 + 1 or 1 + 1
Also is does not matter if you prefix or post fix the 2nd value as anything lower than 16 will give you 0 as the output
So 2 is the answer! -
pain04861716yEdit
after running in editor to check.
Its 1 + 2 / 8 * 2 + 10 % 3
But as stated in last post the answer does not change. -
mt3o19136yPull request rejected, code marked to be dropped during code revew, employee evaluation down by ten percent points.
Without using editor...
public class Test {
public static void main(String[ ] args) {
int value = 3, sum = 6 + --value;
int data = --value + ++value / sum++ * value++ + ++sum % value--;
System.out.println(data);
}
}
a) 1 b) 2
c) 0 c) 3
random