OutputModerate2 marks
What is value of x? String s1 = "45.50"; String s2 = "54.50"; double d1 = Double.parseDouble(s1); double d2 = Double.parseDouble(s2); int x = (int)(d1 + d2);
Topic: parseDouble() and type casting
Previous-year question practice database
Answer
Exam answer
x = 100
Explanation
Three steps. Double.parseDouble() converts the text "45.50" into the number 45.5, and likewise "54.50" into 54.5. Without this conversion the strings could not be added arithmetically at all — "45.50" + "54.50" would simply join them as text. Then d1 + d2 = 45.5 + 54.5 = 100.0. Finally (int) casts the double to an int by TRUNCATING the fractional part — not rounding. Here the sum is exactly 100.0, so x = 100. Had the total been 99.9, the cast would have given 99, not 100; that truncation is the point worth remembering.
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