Student Interview Question for Applications Developers


Team: afai
Country: India
Interview Type: Written Test




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

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 :)

- MissB May 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 :)

- MissB May 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//9 + 2 + 3 = 14
z = ++y + ++x + ++x;

- SV May 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//9 + 2 + 3 = 14
{{z = ++y + ++x + ++x;}}

- SV May 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x=1, y=8, z
z= (++(y=8)) + (++(x=1)) + (++(x=1))

increase value of first x; it become x=2
z=(9) + (x=2)) + (++(x=2))

now increase value of next x; it become 3 so both places x represent value 3
z= (9) + (x=3)) + (x=3))
z=9+3+3
z=15

- govind May 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

++ 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

- Denis May 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(((++y) + (++x)) + (++x))
Putting the brackets or drawing the expression tree will help to understand it better.
The expression is evaluated from left to right.
The operator ++ has higher precedence that +

- RoBa May 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- shameerariff May 11, 2016 | 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