Computer Applications
What will be the output of the following statements?
int a = 3;
System.out.println(" " + (1 + a));
System.out.println(" " + 1 + a);
Values & Data Types Java
19 Likes
Answer
Output
4
13
Explanation
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 ofa
, 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).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 ofa
, which is 3. Therefore, the output for this line will be" 13"
(a space followed by the numbers 1 and 3).
Answered By
8 Likes
Related Questions
Assertion (A) JVM is a Java interpreter loaded in the computer memory as soon as Java is loaded.
Reason (R) JVM is different for different platforms.- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A).
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A).
- Assertion (A) is true and Reason (R) is false.
- Assertion (A) is false and Reason (R) is true.
String is …………… in Java.
- interface
- mutable
- method
- immutable
Find the output of the following code.
String str = "I Love My Family"; System.out.println(Integer.toString(str.length())); System.out.println(str.substring(12));
Write a Java statement for the following mathematical expression :
V = πr2h