9
klajdi
14d

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

Comments
  • 7
    Undefined behavior.
  • 1
    I guess it's B: 2

    But I might be completely wrong, I'm not used to some much shit in a single formula
  • 1
    2 + 0 + 1 = 3
  • 4
    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?
  • 1
    @phreakyphoenix the prefix and postfix gets processed before any other operation? I didn't know that, cool :)
  • 9
    @phreakyphoenix because if you'll use that in production I'll make you undefined 😈🔪

    But real reason is it depends on compiler optimizations.
  • 0
    @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!
  • 1
    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++.
  • 0
    @stormwise Why do you say so? Do you think that clang or ICC will give different output?
  • 0
  • 1
    @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.
  • 1
    @7Raiden no-no-no. I meant that different compiler can be optimized differently and compute those operations in different order. Also it's horribly unreadable and unmaintainable.
  • 1
    @irene Ah, that I agree! And also very true that -O3 *might* change the instruction order, so it might as well be!!
  • 2
    @7Raiden yes. In C++ those expressions are documented undefined behavior. Every compiler can and will compute them differently. Standard does not define the order in which that thing must be computed.
  • 1
    @irene 100% agree with you :) glad to have found a fellow C++ Dev here!
  • 1
    If I have to use my brain I would be on SO
  • 0
    ok 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!
  • 0
    Edit
    after running in editor to check.

    Its 1 + 2 / 8 * 2 + 10 % 3
    But as stated in last post the answer does not change.
  • 1
    I'm sure in output, there is no "a)" or "b)" or "c)" or "d)"
    °∆°
  • 1
  • 2
    Pull request rejected, code marked to be dropped during code revew, employee evaluation down by ten percent points.
Your Job Suck?
Get a Better Job
Add Comment