Microsoft Interview Question for Interns


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
6
of 8 vote

The order in which a++ and a get evaluated is not defined, and so this will be undefined behavior as different execution orders will lead to different results.

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

Please quote the appropriate section from the standard.

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

{
int i=-3, j=2, k=0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);

}
//what about this question if a expression evalutes from right to left ,,,,i am confuse in whether a expression evalutes from left or right please help

- yogesh August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

o/p will be 4 2 0 1
evaluates from left to right
when i goes to ++ it will be 4..No further action is required in case of OR operator(Because in case of OR only one true statement is required ).

- Anonymous August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

The proirity of + is greater than ++ hence would get exectued first
and + operates from right to left
hence if Expession is a + ++a then a gets 11 on execution of right side of + operator i.e a++ ....
now 11+11=22
b=22
a=11

- shreyans August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The answer is correct: it's undefined. It's not about operator precedence, it's about sequence points (SP) which define whether side effects have been been guaranteed to have been performed. Since there's no SP between a++ and a, the standard says the value of the expression is undefined. Look at the Wikipedia page for sequence points. It points to the standards.

- smparkes@smparkes.net August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Similarly a++++ is not allowed as well
Check this : http ://ideone.com/ 4S3S4Z

- crystal.rishi2 October 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

here the expression is a=a+++a
so by operator precedence a++ i.e postfix is evaluated first and then add with a
but Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.

so answer will be b=20 a=11

- tushar October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

compiler will not report an error and their is no ambiguity...the expression b=a+++a will always evaluates to b=(a++)+a as postfix operator++ have higher precedence than prefix operator++

- Aditya Goel August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+++ lexes to ++ + because of the way lexing works, not operator precedence which is a parsing thing. And it's undefined because there is no sequence point. See the accepted answer.

- smparkes@smparkes.net August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 4 vote

#include <iostream>
using namespace std;

int main()
{
int a=10,b=2;
b=a+++a;
cout<<b<<" "<<a<<"\n";
return 0;
}

out_put
------------------------------
20 11
Press any key to continue . . .

on both TC or DEV ........

b=a+++a;
compilers evaluated as "b=(a++)+a not b=a+(++a)" in both compilers

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

i am not sure but i think compiler evaluate expression from right to left ,it first take last a then add it to second one a, after addition it increase a's value then it become 11.

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

according to u(right to left) then why b=20......So will be 21.

there are post increment! (a++) +a

a=11 bcoz value of 'a' read (access) once again after the increment .........So 11

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

Even the expression b = (a++) + a is undefined.

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

b=a+++a is not undefined :
a+++a is (a++) + a.
as compiler's follow greedy approach to get operator . hence a+++a is a++ + a and not a + ++a

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

don't we need to consider the sequence point concept here
i think the result will b undefined
correct me if m wrong plz

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

why result will b undefined ???

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

Because there's no sequence point: en.wikipedia.org/wiki/Sequence_point

Under section "Sequence points in C and C++", read Item 4

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

If u increment a before using in the expression as..

a++; 
             b=a+a;

then meaning of the both statements are same. But u'll get the answer as a=11 and b=22

- Srinivas Desai August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why b is not 21 , because [ post / pre ]increments are performed when they got [ && / || / ; ] symbols ,

like a++&&a , here a will increment .
&

a++ ;
a // here a will increments .

- tushar September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@justneel
we are incrementing a i.e 10+1 and not 20+1 becoz we did not assign 20 to a again

- dileep November 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What does "cout" and its corresponding conditions mean in this context? I got a = 11 and b = 22 for my output by simply executing the b = (a++)+a statement once...

- VivienneJK March 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

output will be undefined ...if we use a+++b ,it depends on the compiler u are using..so we cannot predict..as i read from some c book...

- sushmasush1991@gmail.com July 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

check for C
http: / / ideone . com / YK4ga (remove all spaces)

check for C++
http: / / ideone . com / gxmyX (remove all spaces)

both hv same out put........!!!!!

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

Side effect will take place....hence undefined behavior.
Precedence comes in picture only after fetching the data. Due to a+++a, we are unsure what the value for 2nd 'a' will be fetched... ...if it is fetched after a++ then it is 11 else 10 is fetched....so udefined behavior.

- Nishant October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess + operation is right associative. so a++ + a will result in 10+10 = 20

- confused_banda December 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here b=20 and a=11
as the expression is b=a+++a
here since the precedence of assignment operator is from right to left so first a will be 10 and expression will be executed and after that a will be incremented

- apoorva September 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a = 10;
int b = 30;
if (a * 2 < b) {
a = a * 3;
}
if (b < a) {
b++;
} else {
a--;
}
System.out.println(a + “ “ + b);

- Anonymous August 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

21 11

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

Do u Explain how??

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

There are 2 ways of drawing the expression tree for this expression: a+++a
They are as follows:
{
.......(+)
..../.........\
(a++)......(a)

}
or
{
.......(+)
..../........\
..(a)...(++a)

}

Hence, b=21 and since a is incremented once, a=11

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

Ok.... but i didn't get that in TC/Devcpp.........

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

why it is not
..............(++)
.........../.....\...\
.........(a)...+ ...(a)

- Anonymous November 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is 20 and 11 for the fact that lexer embraces the longest token that matches a valid C token while consuming characters from left to right. So, it will tokenize the string

+++

rendering tokens {{ ++ }} followed by a

+

.

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

what do u mean by lexer embraces ??

string evaluated in order from left to right ...?

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

embraces means the characters it includes under one token

- zer_or_one July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

on java I do the following code

int a = 10;
		int b = 2;
		b = a+++a;
		System.out.println(a+","+b);

it printed out 11,21. I think the way java do that is it find first a ++
and use 10 in for now, but increase it immediately afterward (before evaluating the second a), and when adding the second a, it is already 11, which gives b = 21.

When I tried that on GCC, it gives 11, 20. I think it is because it increase the value of a after the entire expression is evaluated, meaning a is increased after the second a is read.

The following code also prove that
in java it gives 13, 33

int a = 10;
		int b = 2;
		b = a++ + a++ + a++;
		System.out.println(a+","+b);

but in C it give 13, 30.

- miaomiao July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Microsoft does not ask such questions. Stop posting puzzles from websites.

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

as it should calculate from right to left so a+ (++a) so a-11 and b=21

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

........its ok

but in TC/devcpp it display 20 11

this b=a+++a is undefined ....its depend on platform

- 1010101... July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

ohhh my god why are u making it so complex....
final answer will be:
a=11(as the postfix expression execute in the next line)
b=20(10 +10)

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

Ohhhhhh......

ok.........i m right!!!!!

u too....

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

@whomsoever it may concern
source is:.http : / / bit . ly / PljdXy(remove spaces)

- akash1600 July 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

compiler reports an error for this becoz in the same expresion we are incrementing the value of a and using it....either this way a+(++a) or this way (a++)+a

- nehu July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

compiler reports an error for this becoz in the same expresion we are incrementing the value of a and using it....either this way a+(++a) or this way (a++)+a

- nehu July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i think complier fellow greedy approach....

- cse10.rahul August 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Precedence of operators plays the game here. And the output is
20 11

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

this code contains some coding error , like header file must be iostream.h and and in line cout<<b<<" "<<a"\n" ,it must be cout<<b<<a<<"\n" whithout any coding error it give answer 20 and 11, it first evaluate expression a+++a then increase a's value.

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

yea........its should be "cout<<b<<" "<<a<<"\n";

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

There is nothing like iostream.h in standard C++

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

dear just brush up ur........

- Anonymous July 27, 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