2.1k views
0 votes
What will be the value of ans after the following code has been executed?

int ans = 10;
int x = 65;
int y = 55;
if (x >= y)
ans = x + y;
A) 10
B) 120
C) 100
D) No value, there is a syntax error.

User Si Zi
by
5.2k points

2 Answers

2 votes

Answer:

B) 120

Step-by-step explanation:

In the first three lines of the code, three variables ans, x and y have been declared and initialized;

=> ans = 10;

=> x = 65;

=> y = 55;

On the fourth line and fifth line of the code, there is an if block statement that will get executed if the value of x is greater than or equal to that of y;

i.e

if (x >= y)

ans = x + y;

And since x = 65 and y = 55, it implies that x is greater than or equal to y.

Therefore the fifth line of the code will be executed

=> ans = x + y

=> ans = 65 + 55

=> ans = 120

Note:

Though the value of variable ans was initially 10 before the execution of the if statement, its new value 120 will replace the former value.

Therefore the value of ans after the code has been executed is 120

User Azt
by
5.5k points
1 vote

Answer:

Option B 120

Step-by-step explanation:

There are three variables, ans, x and y given in the code. In beginning, the variable. ans, holds value of 10. And variable x and y hold value of 65 and 55, respectively. Since the value held by x is bigger than one held by y, the if condition is met and the statement ans = x + y will run. The execution of this statement will total x with y -> 65 + 55 and the initial value of ans is overwritten by the addition result which is 120.

User Justkikuchi
by
5.4k points