Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

The result is undefined. Side effects in an expression do not have a set order of evaluation in C. The outcome is up to the compiler's implementation.

A compiler may decide to evaluate the side effects of an expression at a different time. The side effect of j++ is permanently incrementing the value of j after using the current value.

This is not a simple expression. This is an arithmetic expression containing two side effects. One of the side effects can potentially impact the rest of the processing.

Since the subtraction has a set operator order of precedence (left-to-right), the code will first compute (j++). The value of which is the current value of j, 10. This is the left-hand side of the subtraction, the minuend. The side effect making j permanently represent a value one higher (j+=1) may not occur until later (the end of the line's execution), even though this evaluates.
Next, the right-hand side of the subtraction, the subtrahend, will evaluate. Now comes the reason we have an undefined outcome. Did the side effect of evaluating the minuend take effect immediately, or will it not take effect until the line is completed? The compiler decides this question, then evaluates (j++) again, yielding a value of 10 or 11.

With ++j - ++j, you may find the same issue. Do we get 11-12 (side effect happens immediately) or 11-11 (side effect happens at the end of the line's execution)?

Do not depend on side effects happening at a particular time in code.

- C++ Lion October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The real background of the story is about sequence points.

Increment operators are defined that the side-effect can happen anytime between the two sequence points where it appears. So j++ can happen anytime of the two ";"s that are "around" the assignment in question (; i = j++ + j++;) - the ;s define the sequence points - for more info check the C standard)

And it is sure that the two increments are applied after the ";" - the closing sequence point of the assignment statement.

- Selmeczy, Péter December 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void main() {
int i,j;
j = 10;
i = j++ - j++;
printf("++j - j++%d %d \n\n", i,j);

j = 10;
i = ++j - ++j;
printf("++j - j++%d %d \n\n", i,j);

j = 10;
i = j++ - ++j;
printf("++j - j++%d %d \n\n", i,j);

j = 10;
i = ++j - j++;
printf("++j - j++%d %d \n\n", i,j);
getch();
}

You get the same answer for all the above code because the expression is first evaluated and the (pre/post)-incremental takes place...

- shantanu.msp September 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Chrome this outputs " -1 1":
javascript: var x = 1, y = x++ - x++; var w = 1; var z = w++ - 0; alert(' ' + y + ' ' + z)

So seems like it is different answer than yours.

- Ninja Coder October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main() {
int i,j;
j = 10;
i = 3;
//i = j++ - j++;
//i =   i++ + ++i;
//i = j++ - j++ + i++ + ++i;
printf("%d\n", i);
}
~

If I un-comment the first comment then output = 0
if second then output = 9
if third then output = 8

Please explain why this is so

- learner October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just experiment with code :-)
Here is my example how to see what is going on.

template <typename T>
class op_test
{
private:
    T val_;
    op_test();
public:
    explicit op_test(const T& v)
        : val_(v)
    {
    }
    virtual ~op_test(){}

    op_test& operator-(const op_test& rhs){
        std::cout << "called operator-" << std::endl;
        val_ = val_ - rhs.val_;
        return *this;
    }

    op_test& operator++(int i)
    {
        std::cout << "called operator++" << std::endl;
        val_ += 1;
        return *this;
    }
};


int main()
{
    op_test<int> a(12), res(0);
    res = a++ - a++;
}

It produce following result for G++

called operator++
called operator++
called operator-

- Anonymous October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think if you overload the operator, it is more like a function call rather than post operation. This is why it is showing in the order you printed

- Abhishek February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think if you overload the operator, it is more like a function call rather than post operation. This is why it is showing in the order you printed

- Abhishek February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think if you overload the operator, it is more like a function call rather than post operation. This is why it is showing in the order you printed

- Abhishek February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Perl gives
-1 and 12
which i agree with .
ur interviewer is a doormat

- howdoesitmatter October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i agree with the last comment- howdoes it matter
perl gives -1 and 12 .

$j = 10;
$i = $j++ - $j++;
print "i=",$i," j=",$j;


-1 and 12

surely, his interviewer was a class A i_diot.

- mentalcase October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let not talk about perl and stick to this C code given.

- Anonymous October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its i = 10 - 10

- Angamuthu G October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its i = 10 - 10
becoz '++' is lower precedence than '-'.
you can try this program by
main() {
int i,j,a,b;
j = 10;
i = (a=j++) - (b=j++);
printf("%d %d %d %d", i,j,a,b);
}
you wil get 0 12 10 10.

- Angamuthu G October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this type of problems r compiler dependent...

- lalit October 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

main() {
int i,j;
j = 10;
i = j++ - j++; //{i=10-10=0}
printf("%d %d", i,j);//{j=j++=11,j++=12}
}

- robin October 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

j++ j value is incremented after the execution of statement
++j j value is incremented while the statement is executed
i=j++ - j++;
so above indicates i=10-10
however if
i=++j - ++j;
i value can be compiler dependent
so i value is 0 and j value is 12 as it is incremented twice after the execution of statement
i=j++ - j++;

- Satish November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ya its 0 ,12 because j++,j++ will remain on the same value it's 10 ,so i becomes 0,then first j++ (before -)will be inc then second j++ (second j++)will be inc.......

- Anonymous September 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

That's trivial ....
we have i = 10 -10 = 0 //post increment of j, means we use 10 rather than 11 ,then we increment that
then we see that j increment 2times which gives it the value of 12

- An Nguyen October 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is not correct.
According en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B substract has less priority then increment.

So it seems to me 0 because of 12 - 12

- Sergey October 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The result is undefined

- Anonymous October 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please explain why? It is not function call, just simple expression.

- Sergey October 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The behavior is undefined and may depend on which compiler you're using. There may be only one statement that changes the value of a variable between two sequence points.

- eugene.yarovoi October 09, 2011 | 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