4. What will be the output of the following code fragment ?
:
if (a = 5)
System.out.println ("Five");
else
System.out.println (“Not Five");
if the input given is (i) 7 (ii) 5 ?
Answer is needed as fast as possible. pls help me guys.
Answers
Answered by
4
Output:
1) Not Five
2) Five
1) Not Five
2) Five
Answered by
0
The output for both inputs (i) 7 and (ii) 5 will be "Five".
- The if statement in the code fragment has the condition a = 5. This is an assignment operator that assigns the value 5 to the variable "a" and returns the value 5.
- Since the value 5 is non-zero, it is considered true in a Boolean context, and the code block inside the if statement will be executed.
- So, when the input is (i) 7, the value 5 is assigned to "a" and the if statement condition is considered true, so the output will be "Five".
- Similarly, when the input is (ii) 5, the value 5 is assigned to "a" and the if statement condition is again considered true, so the output will be "Five".
- In order to check for equality, we need to use the comparison operator "==".
- Therefore, if we change the if statement condition to a == 5, it will check if the value of "a" is equal to 5 and the output will change depending on the value of the input.
#SPJ3
Similar questions