Interview Question


Country: United States




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

this is undefined behaviour...

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

undefined behavior

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

ready my comment (SHAN) on comment id:14259670

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

It's Simple,

Keep one thing in mind, calculation is done from right to left, you will get anwer for first question.

For second question, int division results to 1, incrementing that we will get 2.

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

Dont join that compant who ask these questions.. :)
Google Undefined Behaviour!!!

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

u r right

- dileep March 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

please someone explain

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

Undefined Behavior.....
It is explained very well here....

stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
and
stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviour-that-a-c-programmer-should-know-ab

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

These are the expression involved with sideeffect.
Preincrement is a unary operator and has highest precedence, post increment is done after assignment.So output is 2 in both cases.How ever o/p is compiler dependent.Modern gcc compiler will output 2 in both cases.

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

the first exp can be written as (x++)/(++x) //precedence of operator
now from right the value of x becomes 2 but the value of x on the left does not change as its a post fix so we have x/x resulting in 1 now the postfix takes effect so value becomes 2

the 2nd exp same thing follows (++x)/(x++)
from right being a post fix value remains one, but in the left value chages to 2
so x/x results in 2/2 i.e 1 and with post fix x again becomes 2

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

Undefined Behaviour - Explained very well in "C FAQ's" by Steve Summit.

Please refer this question (question No. 3.1)

" Q: Why doesn't this code:

a[i] = i++;

work?

Ans - The subexpression i++ causes a side effect--it modifies i's value--which leads to undefined behavior since i is also referenced elsewhere in the same expression. There is no way of knowing whether the reference will happen before or after the side effect--in fact, neither obvious interpretation might hold "

- Sandeep Singh July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Acc to sequence point rule in 2 cosecutive sequence points if the value of a variable is modified more than once then it is voilation in c. The value to above ques wil b compiler dependent. ;,&&?: these r the eg of sequence points.

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

for x=x++/++x;
its evaluated in this order:
1.++x makes x =2.
2.now since x++ is post increment, the exp becomes x/x++ ie 1/2 = 0(since x is int).
3.now assignment operator(=) has low precedence than post increment(++), x=1 is incremented to 2 and then its assigned to x.


for x=++x/x++;
its evaluated in this order:
1.++x increments x =2
2.now exp becomes x = 2/(1)++ which is evaluated to 2/1 = 2
3.x is still 1 and its incremented to 2(due to post increment) and assigned to x making it 2.

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

its very simple ........in evaluation it will take first only expression then increment operator means
for both the expressions x=x/x now it will put the value of x =2 because of pre increment operator ..... so we will get the result 1 and then again incremented by 1 because of post increment......

- pushpendra singh chouhan August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here first evalution continue from right,so the ++x becomes 2 and now x=2 so
x=x/2,2/2,1 now the value of x is 1 but remember postincrement is waiting for increase the value(x++) so the value of x is become 2(ans).
here compiler tereatesd all x same it can not diffrenciate them....

- alien August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In my environment x is 1. @ Ajeet, ++x has the highest priority, so in the second one x will be 2 first and then x = 2/2 which 1.

- Bin December 12, 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