Interview Question


Country: United States




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

Please take time to read this:

stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points

- Anonymous July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 5 vote

2

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

why ans is 2 for

int main() {
int x = 1;
x=x++/++x;
printf("%d\n",x);
return 0;
}

- niraj.nijju July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well if we convert above statement into instruction set then it ll execute as follow:
eg: Add a,b,c (in instruction set)
i.e add value at address of b with value at address of c and store at address of a. Remember address plays imp role here.
Now
x=x++/++x
It will be converted as follows:
(x++): take value at address of x and then increment later. (x is still 1)
(++x): first increment value at address of x then take its value. (x becomes 2)
finally x++/++x i.e (2/2. This is because in second instruction value has become 2).
so it is 1. now final execution of 1st instruction (i.e increment value of x) is still left. so x becomes 1+1 i.e 2.

For simplicity Look this instruction set conversion:

Div &x,&(x++),&(++x)
++x will cause value to change 2.
so x becomes 2.
x++ will first use this 2 and then increment x by 1.
so 2/2=1 and increment it. =2
Hope tht helps :)

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

i think this is right ...

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

-1 for wrong answer. Its undefined behavior.

See here: stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points

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

if we declare x as a volatile int x. then it will give result as 3 why?

why ans is 3 for

int main() {
volatile int x = 1;
x=x++/++x;
printf("%d\n",x);
return 0;
}

- ashok September 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This comment has been deleted.

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

It's undefined behavior. See the answer that mentions sequence points.

- eugene.yarovoi July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you Deven Bhooshan for your kind explanation. But i couldn't follow you. Just go through the link given above.

- kishore July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

undefined behavoir...

- anubhavco July 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer:1,its never 2!!!!!

- Niraj Acharya(Nepal) July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Read about what undefined behavior is.

- eugene.yarovoi July 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Explanation:
x="++x/x++"=1
++x means the value of x at this instant is 2,again x++ means the value of x will be 3 only wen x comes again in the code.But see clearly,there is x=++x/x++;means,the value of x =2/2...Here x++ is not 3 as x/x is stored in x itself which comes to be 1............Great Question!!! :D

- Niraj Acharya(Nepal) July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i compiled the same in visual studios c compiler i got the answer as 2

- rakhil July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the answer is definitely 2.
Because in x = ++x/x++;
at a time the initial value stored in x will be fetched up by the expression.
so when ++x will put 2 for calculation, x++ will put 1 and result of div = 2.
By the time, ++x as well as x++ both might have written/overwritten the value of x = 2.
So consider any way, the result has to be 2 only.

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

Have you heard of "undefined behavior'? Undefined behavior means the answer doesn't have to be anything.

- eugene.yarovoi July 22, 2012 | Flag


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