ICSE Class 10 Computer Applications Question 21 of 30

Solved Sample Paper 1 — Question 21

Back to all questions
21
Question

Question 2(i)

What will be the output of the following statements?

int a = 3;
System.out.println(" " + (1 + a)); 
System.out.println(" " + 1 + a);
Answer
Output
 4
 13
Explanation
  1. System.out.println(" " + (1 + a)); — In this statement, we have an expression inside the parentheses (1 + a), which is evaluated first. This expression adds 1 and the value of a, which is 3. So, (1 + a) equals 4. After that, the + operator outside the parentheses is used for string concatenation. The string " " (a space character) is concatenated with the result of (1 + a), which is 4. Therefore, the output for this line will be " 4" (a space followed by the number 4).
  2. System.out.println(" " + 1 + a); — In this statement, we have a different expression. The + operator is evaluated from left to right. So, " " + 1 is evaluated first. It concatenates the string " " with the number 1, resulting in the string " 1". Then, it concatenates this string with the value of a, which is 3. Therefore, the output for this line will be " 13" (a space followed by the numbers 1 and 3).