Give the output of the following code:

String A ="26", B="100";
String D=A+B+"200";
int x= Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x+y;
System.out.println("Result 1 = " + D);
System.out.println("Result 2 = " + d);

Views 13

Answer:

Output of the above code is:

Result 1 = 26100200
Result 2 = 126
Explanation
  1. As A and B are strings so String D=A+B+"200"; joins A, B and "200" storing the string "26100200" in D.
  2. Integer.parseInt() converts strings A and B into their respective numeric values. Thus, x becomes 26 and y becomes 100.
  3. As Java is case-sensitive so D and d are treated as two different variables.
  4. Sum of x and y is assigned to d. Thus, d gets a value of 126.

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.