Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Int x=5,y=6,z=7;int s=0;s = --x+++y---z*x--, what is the value of s?

user-image
Question added by himanshu mittal
Date Posted: 2013/07/04

in java the result is -13 :)

Preetham D
by Preetham D , Software Engineer / Senior Software Engineer , Mindtree

answer is -13,..
thing is maintain correct identation, so that it'll help u to analyse easily

it is the same in C :) Explanation: generally priority is given to the prefix operations.So1st all the prefix operations will be computed and then the postfix.In this case we have2 prefix operations i.e --x,++y.here both increment and decrement operators have the same priority as both come under prefix.NBow when you solve;you get :4+7-6*4.Now follow BODMAS...you will end up with -13.

Syed Faraz Hashmi
by Syed Faraz Hashmi , Sr. Programmer / DBA , Saudi German Hospital

Well, this all depends on the sttatement s = --x+++y---z*x--; There could be3 variations of this statement, because, statement is not properly structured usingbrackets: s = --x++ + y-- - z*x--; [Compile time Error] s = --x + ++y-- - z*x--; [Compile time Error] s = --x + ++y-- - z*x--; [Answer = -13] The above compilation errors are the cases in C#, which i think, should also be the case of C and Java as well, but not sure offcourse.
The reason of this compile time error is that, increment or decrement operators (--/++), are only application to variables, properties or indexers, but these cannot be used with literal values.
Like if first --x is executed, now the result no more remains a variable, it has become a value "4", and next ++ cannot be applied to "4".

Int x=5,y=6,z=7,s=0;

s = (--x)+(++y)-(--z*x--)

s=4+7-(6*4)

s=11-(24)

s=13

mukhtar ahmed Ibrahim
by mukhtar ahmed Ibrahim , Software engineer , Norwegian tax administration

Do we need to answer this question? oooh... are we solving a right problem or are we learning a basic mathematical problems? better to answer e(n−1) +e(n−2) +e(n−3) +···+e(1) +e(0). 

 

Jahanzeb Zaman
by Jahanzeb Zaman , Lead Solution Architect | Head of Development , Saudi Telecom Company

-13 is the answer

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.