OutputModerate2 marks
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);
Topic: Concatenation and parseInt()
Previous-year question practice database
Answer
Exam answer
Result 1 = 26100200 Result 2 = 126
Explanation
This question sets the two meanings of + side by side, which is exactly its purpose. D is built from Strings, so + CONCATENATES: "26" + "100" + "200" joins into 26100200. Nothing is added. x and y are converted to real numbers by parseInt(), so x = 26 and y = 100, and x + y is genuine arithmetic: 126. The rule: if either operand of + is a String, the operation is concatenation; only when both are numbers is it addition. That single rule answers a great many questions in this paper.
More from String Manipulation
To extract the word NOW from the word "ACKNOWLEDGEMENT", the Java statement "ACKNOWLEDGEMENT".substring(3, ____) is used. Choose the correct number to fill in the blank.2026String a[] = {"Atasi", "Aditi", "Anant", "Amit", "Ahana"};
System.out.println(a[1].charAt(1) + "*" + a[2].charAt(2));
The output of the above statement is:2026Which of the following String methods returns a negative value?2026Assertion (A): The substring() method modifies the original String.
Reason (R): The substring() method can extract part of a String starting from a specific index.2026Give the output of the following program segment:
String S = "GRACIOUS".substring(4);
System.out.println(S);
System.out.println("GLAMOROUS".endsWith(S));2026Name the following:
(a) The return data type of the method equals().
(b) The String method which has no parameter and returns a String.2026