84.7k views
3 votes
Consider the following code segment: String str = "I am"; str += 10 + 3; String age = "years old"; System.out.println(str + age); What is printed as a result of executing the code segment?

User Euralis
by
8.0k points

1 Answer

2 votes

Final answer:

The code will print 'I am13 years old'.

Step-by-step explanation:

The code segment given is as follows:

String str = "I am";
str += 10 + 3;
String age = "years old";
System.out.println(str + age);

When this code is executed, it will print I am13 years old.

The reason for this is the concatenation and arithmetic operations happening in the code. The variable 'str' initially stores the string 'I am'. Then, the '+=' operator is used to concatenate the value of 'str' with the result of the expression '10 + 3', which is 13. Finally, the 'println' statement prints the concatenated string 'I am13 years old' to the console.

User Colin Claverie
by
7.3k points