Interview Question






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

15

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

The expression evaluation is compiler dependent.
gcc compiler returns 13 whereas turbo c returns 15

- Kamran August 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

12

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

j will be 12. The compiler will scan from left to right. at first it will get ++i then increment i by one and store the value of i = 2+1 = 3 for addition. then it will move to the next ++i and also increment i to 4 and store it. And so...

so j = 3+4+5 = 12

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

It's undefined behavior - see Sequence_point at wikipedia

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

its is undefined in c no sequence point is there in mid so no fix evaluation order

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

Crappy coding practice. Not worth consideration.

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

+1.... a programmer who code like this should be kicked out instantly!

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

And so should the interviewer who asks this sort of question. I have been coding for 15 years, and I am quite good with algorithms and most aspects coding in general, and I have no idea what that code would yield, and I want to make a point of never ever finding out either.

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

Given how many people are getting the answer (Undefined Behavior) wrong, this seems to be an excellent question for eliminating candidates.

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

13 coming please explain!!!!

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

Its 12 as Raquibur said it above.

Since its ++i, its preincrement operator which makes i = 3 at first.
So when it goes to compute second ++i, i is already = 3 and preincrement will make it 4.
Next time the value is 5.

So (++i) + (++i) + (++i) = 3 + 4 + 5 = 12.

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

Despite what others have mentioned about the lack of sequence point, you choose to just overlook that.

This may seem like a lousy question. But clearly it eliminates a lot of amateurs.

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

13 is the output when executed on g++ compiler.

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

The explanation for 13 is as follows, the first (++i) evaluates it to 3. But there is no temporary variable created for the expression evaluation. Now the next after the next (++i) is evaluated then the value of i becomes 4. Now because there was no temp variable created, the expression evaluates to (++i)+(++i) = 4 + 4. But after this there is a temp variable with value 8 created and is not effected by further increments of i. Therefore, (++i)+(++i)+(++i) = 4 + 4 + 5 = 13

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

Can we stop this nonsense. The code has undefined behavior. A compliant compiler can return any value.

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

I dont think the behavior will be undefined here. Pre-increments are always calculated before calculating an expression so before 'j' is calculated, 'i' will be incremented 3 times which gives i = 5 and j = 5 + 5 + 5 = 15.

- Guest August 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression."
The code violates this rule, hence undefined behavior.

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

Behavior is undefined.... If an object is modified twice within an sequence point, the state of the object is undefined. A sequence point can be a ',' , '&&' , '||'...

- amruthkesav.s August 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The result turns out to be 13 (when run, might be implementation dependent).
The explanation is like this.

++i will increment the value to 3. But its reference will be used
++i will increment the value to 4. Again the reference will be used. Since there is no paranthesis, the compiler will first sum the first two, and evaluate the third one.

4+4+5 = 13

if you had paranthesis

like

j=(++i)+((++i)+(++i)) the result wil lbe 15 for similar resons.

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

Great. Another Anonymous reader ignoring the lack of sequence point.

- Anonymous August 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it is compiler dependent.gcc returns 13 whereas turbo c returns 15 as the answer

- kamran August 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Turbo see first pre-increments i and then use the incremented value in place of i and evaluates the expression i.e,-5+5+5=15.
gcc compiler first increments only 2 instances of pre-increment and uses it.the next pre incremented operand is incremented and added and so on.i,e,=>4+4+5=13
example=>(++i)+(++i)+(++i)+(++i)+(++i),i=2.the expression is evaluated as=>4+4+5+6+7=26.

- Kamran August 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yup that work for me.
turbo c returns the correct(or the expected result)
while gcc(i use dev c compiler) works on different principal,as explained above.parentheses has nothing do here

- priyanshu August 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yup..it will b 15.

- Kamlesh Meghwal October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

j=12 is the output.however the 'i' which is incremented first may be the compiler dependent.since it is all sum of numbers there is no issue,however if it is a subtraction the answer may be compiler dependent

- Satish November 07, 2011 | 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