Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par himanshu mittal
Date de publication: 2013/07/04
Preetham D
par 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
par 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".

Utilisateur supprimé
par Utilisateur supprimé

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
par 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). 

 

Utilisateur supprimé
par Utilisateur supprimé

-13

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

-13 is the answer

More Questions Like This