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

- Luv July 03, 2012 | Flag Reply
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.

- Aashish July 03, 2012 | Flag Reply
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

- Anonymous July 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Shobhit July 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hmmm great observation, let me check

- Anonymous July 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous July 03, 2012 | Flag
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

- Anonymous July 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous July 03, 2012 | Flag
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.

- Anonymous July 03, 2012 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More