Ask Question

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.

+2
Answers (2)
  1. 10 November, 00:53
    0
    Option B 120

    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.
  2. 10 November, 01:01
    0
    B) 120

    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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; ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers