OutputModerate2 marks
Give the output of the following code: String p = "20", q = "19"; int a = Integer.parseInt(p); int b = Integer.parseInt(q); System.out.println(a + "" + b);
Topic: parseInt() and concatenation
Previous-year question practice database
Answer
Exam answer
2019
Explanation
Two steps, and the second is the trap. parseInt() converts the text into numbers, so a = 20 and b = 19. Then the printing. Because "" is a String, the + operator CONCATENATES rather than adds, and it works left to right: 20 + "" gives "20", then "20" + 19 gives "2019". Had the empty string not been there, System.out.println(a + b) would have printed 39. Whenever a String appears anywhere in a + chain, everything from that point on is joined as text.
More from Library Classes
What is the output of the statement Math.pow(36, 6/5);?2026The output of the statement:
System.out.println(Character.toUpperCase('b') + 2); is:2026What is the type of parameter to be given for the method parseInt()?2026Write the Java expression to find the sum of cube root of x and the absolute value of y.2026Character class methods are found in the package called:2025The output of Math.max(-7, Math.min(-5, -4)) is2025