111k views
3 votes
Use the following code fragment to answer the question - If x is currently ), a = ) and b = -5, what will x become after the above statement is executed?

if (a > 0)
if (b < 0)
x +=5;
else
if (a > 5)
x +=4;
else
x =+3;
else
x +=2;
A) 0
B) 2
C) 3
D) 4
E) 5

1 Answer

0 votes

Final answer:

After running the given code with x = 0, a = 0, and b = -5, x will become 2.

Step-by-step explanation:

If x is currently 0, a = 0, and b = -5, the code fragment in question will evaluate the conditions and alter the value of x accordingly. The first if statement checks if a > 0, which is false since a equals 0. Therefore, the nested conditions within this first if statement are not executed. Instead, the code moves to the else part of the statement, which simply adds 2 to x, making it 2.

User Paul Rademacher
by
7.6k points