## Interview Question

Country: United States

Comment hidden because of low score. Click to expand.
1
of 1 vote

It contains side-effects. Hence the answer depends on the order of evaluation. Hence Compiler Dependent

Comment hidden because of low score. Click to expand.
1
of 1 vote

The output is undefined. There is no sequence point defined between the two expressions ++i & i++. Even if the expression is written like i=i++, the output is said to be undefined. A sequence point is also needed betwwen the two expressions i & i++, as the evaluation order of the expression is uncertain & varies from compiler to compiler based on parsing algorithm it follows to parse the instructions to be executed.
In simple words, if the value of the same variable is changed more than once in a statement, then it is said to be UNDEFINED BEHAVIOUR(UB).
A semi-colon is a sequence point where as +(plus) is not.

Comment hidden because of low score. Click to expand.
0
of 0 vote

answer output would be 1 as i=++i/i++ the increment operator both post and pre happens only after the statement is executed, but in this particular statement both are equal to 5 and hence 1

Comment hidden because of low score. Click to expand.
0

check it for i=0 . Ans is 1 . Why ?
There's an increment because for i=-1 . Its showing an error . That means division by zero

Comment hidden because of low score. Click to expand.
0

hmmm great observation, let me check

Comment hidden because of low score. Click to expand.
0

now I understood, let us first take i=5;
now i=++i/i++;
calculation takes place from left to right OK
++i is executed (this is statement) ok
i=6
now i++ is executed
i=6 after statement is comeplete i becomes 7 but when calculaation is taking place it is 6/6 = 1
now is assigned 1

case i=-1

i=++i/i++
++i makes i=0
now i++ is executed bu when division is taking place i is still 0 and will become one when division is complete ( but before that division by zero)

case i=0
++i makes i=1
++i/i++ make 1/1 and then i should be incremented
so result is one

Comment hidden because of low score. Click to expand.
0
of 0 vote

If you run the given code in Linux g++ compiler then you get result = 2

Comment hidden because of low score. Click to expand.
0

This type of question is only important for interview, if you write it in a company then during code review it will be rejected ;)

Comment hidden because of low score. Click to expand.
0
of 0 vote

what Luv and shondik mentioned is correct. It will have side effects as there is no sequence points define.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.