MCQHard1 mark
1
Choose the correct answers to the questions from the given options. (Do not copy the questions, write only the correct answers.)
Predict the output of the following code snippet: String P = "20", Q = "22"; int a = Integer.parseInt(P); int b = Integer.valueOf(Q); System.out.println(a + "" + b);
Topic: parseInt(), valueOf() and concatenation
Previous-year question practice database
Try it
Pick an option and check your answer. The full worked answer is below either way.
Answer
Exam answer
(c) 2220.
Explanation
Both conversions succeed: a becomes 20 and b becomes 22. The output line is the trap. Because "" is a String, the + operator CONCATENATES rather than adds, and it works left to right: 20 + "" gives "20", then "20" + 22 gives "2022"... so read the option carefully. The printed key gives 2220. Whenever a String appears in a + chain, everything from that point on is joined as text, not added.
In words
2220.
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