Output Questions for Class 10 ICSE Computer Applications

Determine the output of the following program.

public class Test
{
    public static void main(String[] args) 
    {
        int a = 1, b = 2;
        System.out.println("Output1: " + a + b);
        System.out.println("Output2: " + (a + b));
    }
}

Java

Java Operators

33 Likes

Answer

Output

Output1: 12
Output2: 3

Explanation

In the first println statement, the expression is "Output1: " + a + b. First "Output1: " + a is evaluated. As one operand of addition operator is string and other is int so int is casted to string and concatenated giving the result as "Output1: 1". After that, "Output1: 1" + 2 is evaluated and similarly the result is Output1: 12.
In second println statement, the expression is "Output2: " + (a + b). Due to brackets, (a + b) is evaluated first. As both operands are integers so they are added giving the result as 3. After that, "Output2: " + 3 is evaluated, resulting in "Output2: 3".

Answered By

14 Likes