Student Interview Question
Applications DevelopersTeam: afai
Country: India
Interview Type: Written Test
z = 14. Here's why:
When it comes to increment operators in a statement, ++x implies using the value of x after incrementing it by 1. On the order hand, x++ implies using the value of x as is and then incrementing it.
So lets breakdown the statement into two steps such that the expression is written as such:
z = (++y) + (++x)
z = z + (++x)
Equation 1:
z = (++y) + (++x)
. Here, y is initially 8 and x is initially 1. Remember our rule with the operators, so we increment both values before using it. So this equation becomes (after the increment):
z = 9 + 2 = 11
Equation 2:
z = z + (++x)
.. Remember x is now 2 given we incremented it in equation 1 above and z = 11. Before we can use x in this equation, we have to increment if (from rules with the operators). So this equation becomes (after incrementing value of x):
z = 11 + 3 = 14
Hope this makes sense :)
++ is an increment operator. increment operator before the variable has the higher priority than any other operator involved with the variable. It is called pre-increment operator. If increment operator is after the variable than it is called the post increment operator It has the lower priority.
In the given code you have pre-increment operator's so when you apply plus operations first the value of the variable is increased then it is added to the another variable. like
z = (++y) + ++x + ++x; //here first the value of y increase than it is added to the value of x like the normal plus operation. Similarly for the other x varialbes.
But if it was like this:
z = ++y + x++ + ++x;
Here in case of first x it will add the value of x first to y and that value is added to the second x variable.But by the time control goes to the second x,the value of x is already increased by 1 in the post increment operator. So, the addition would be like this
z = 9 + 1 + 3
It is a simple program with a little twist.
Do not get confuse with the operator +, you need to split the equation
z = ++y + ++x + ++x;
as
z= (++y) + (++x) + (++x);
for your understanding.
Now as per your program the declared values are
x=1 and y=8
Now applying the value of y =8 since y is preincremented (++y) in the first part of the equation, the value of y becomes 9
the value of x is 1 in the second part of the equation the value of x will be preincremented (x++) so value of x will become 2
when it comes to the third part of the equation the incremented value of x ie 2 will be again preincremented to 3.
So the total will be
z=(++y)+(++x)+(++x);
z=9+2+3
z=14.
Hope you are cleared
z = 14. Here's why:
When it comes to increment operators in a statement, ++x implies using the value of x after incrementing it by 1. On the order hand, x++ implies using the value of x as is and then incrementing it.
So lets breakdown the statement into two steps such that the expression is written as such:
Equation 1:
. Here, y is initially 8 and x is initially 1. Remember our rule with the operators, so we increment both values before using it. So this equation becomes (after the increment):
Equation 2:
.. Remember x is now 2 given we incremented it in equation 1 above and z = 11. Before we can use x in this equation, we have to increment if (from rules with the operators). So this equation becomes (after incrementing value of x):
Hope this makes sense :)
- MissB May 04, 2016