Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each of the following code fragments, indicate what the value of the variabl

ID: 3653180 • Letter: F

Question

For each of the following code fragments, indicate what the value of the variables x, y and z would be at the end of execution, without executing the code. Also say if you believe the code is portable, i.e., would give the same results for any correct implementation of a C or Java compiler. a. int x = 1, y = 10, z = 100; y += (++x != 0 && x % 2 == 0) ? --z * 0 + --x : ++z ; b. int x = 1, y = 10, z = 100; if ((x *= (y++ + ++z) != 0 && y > z / y || --y < z-- ? 1 : 0) != 0) z -= y -= 5; else y += z += 5;

Explanation / Answer

a )

for this expr

y += (++x != 0 && x % 2 == 0) ? --z * 0 + --x : ++z;

( ++x ! = 0 && x %2 == 0 )
in this ++x increments the x to 2 ,so x becomes 2 after this execution ,then we check x%2 == 0 ,since x is 2 and 2%2 = 0.
so ( ++x ! = 0 && x %2 == 0 ) is true.

since ( ++x ! = 0 && x %2 == 0 ) is true --z * 0 + --x part will execute.

it will decrement z by 1 causing z to become 99 ,then multiplaying by 0 to z gives 0 .
then decrement x by 1 causes x to go to 1 and adding 0 gives 1.

so the expr becomes y+= 1
y is 10 initially ,so the result becomes y = 11.

so , x= 1 , y = 11, z = 99.


B)
consider the expression

((x *= (y++ + ++z) != 0 && y > z / y || --y < z-- ? 1 : 0) != 0)


initially x = 1, y =10 and z = 100

Part 1 )

(y++ + ++z) is similar to 2 statements ( y + (++z)) ;y+=1, so this gives the value of expression to 111 and z to become 101 and y to become 11,
so (y++ + ++z ) != 0 is true. so (y++ + ++z) != 0 gives 1 as answer .

Part 2 )

y > z/y <=> 11 > 101/11 which is true ,so gives 1 as answer.
after this part value of y is 11 and value of z is 101
since part 1 and part 2 are true then the and(&& ) of both part is true so the compiler will not execute the part 3 because it is Or operation and since the first operand of Or is true , the truthhood/falsehood of second operand does not affect the result ( because if one operand of or operator is true the result is true )
SO PART 3 will not execute , but i have written it here to let u understand about the preincrement and postincrement operator

Part 3 )

--y < z-- is equal to 10 < 101, so this gives 1 as answer and causes y to become 10 and z to become 100.

using result of part 1 ,2 we get,
((x *= (y++ + ++z) != 0 && y > z / y || --y < z-- ? 1 : 0) != 0)
= ( ( x*= ( 1 && 1 || # ) ? 1 : 0 ) != 0 ) ( # denote we don't care about this value because teh first operand of or operator is true)
= ((x *= 1 ) != 0 )
since x has 1 as initial value this expression is true and x becomes 1.
if condition is true so, then part will execute which is
z -= y -= 5

this is similar to following set of instructions

y = y - 5                this gives y = 11-5 = 6

z = z - y     this gives z = 101 - 6 = 95

So the answer is x = 1,y = 6, z= 95

since we are not using any operator which depends on the underlying processor so this should be portable.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote