ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل himanshu mittal
تاريخ النشر: 2013/07/04
Preetham D
من قبل 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

Monica Peddi
من قبل Monica Peddi

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
من قبل 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
من قبل mukhtar ahmed Ibrahim , Software Engineer , the norwegian tax administration (skatteetaten)

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
من قبل Jahanzeb Zaman , Lead Solution Architect | Head of Development , Saudi Telecom Company

-13 is the answer

المزيد من الأسئلة المماثلة