Microsoft Interview Question for Developer Program Engineers






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

Undefined behavior ("Between the previous and next sequence point an object shall have its stored value modified at most once ...").
But even if that was not the case, the order in which the two sub-expressions, b++ and ++b, are evaluated is unspecified. Finally, when the post increment happens is unspecified as long as it is after the value of b++ has been determined and before the next sequence point.

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

It is undefined behavior indeed.

- Shar3oob May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

16

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

I think it is NOT undefined. It must print 16.

- Alex July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@alex.. it is undefined... when you have two increment operators in a single statement... something called side- effect occurs and each and every compiler handles this side effect in their own way.....

- anon July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes it is undefined behaviour

- geeks July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its 16.value will pass through from right and will be only one increment.

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

thanks

- anonymous July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It might be dependent on the compiler. I tried on ideone, it generates a warning, but still prints the output (16), and b has been updated to 5.

- airfang613 July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

16

- nitin July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@rajeev...why do u try for microsoft ? its paying just 8.5...try in fb..45 lpa

- thaapa July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i dont work for money ...:P

- Rajeev July 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So, is it some question asked in MS (India)? I never knew they could be such crappy interviewer!!!

- hmm July 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's undefined. If you feel otherwise, put on your asbestos suit and file a bug report against either GCC (16) or Clang (15).

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

i tried in C#
Console.Write(b++ * ++b);

it gave me 15,

it is assiging value first and then incrementing when i say b++,
where as it is incrememting value first and assigning value when i say ++b
so it becomes 3*5 = 15

- wow.sitaram July 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

C# specifies that the order of evaluation is left-to-right with all side effects taking effect before next subexpression is evaluated. Hence the program is well-defined and indeed gives the answer 15.
C and C++ make no such gurantee

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

In Java, this also gives 15.
the reason is I think i++ means it uses the value of i (3 at that time) and increments the value (to 4) and then ++i increments the value before using. i (=5) used here.
so i++*++i = 3*5 = 15

- Anon November 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In Java, this also gives 15.
the reason is I think i++ means it uses the value of i (3 at that time) and increments the value (to 4) and then ++i increments the value before using. i (=5) used here.
so i++*++i = 3*5 = 15

- Anon November 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A C++ project in Visual Studio 2010 gives the answer as 16.. :) and so it should be...
In C# and Java it has to be 15...

- chamy50 July 16, 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