CD but there's an error with the display of Answer option D
D. Integer res = 250;
res =+ s2;
This will only return 1000 in result because there's no operator like =+
The correct choice of answer would be as below (for D),
D. Integer res = 250;
res += s2;
package q25;
public class Q25 {
public static void main(String[] args) {
String s = "10_00";
Integer s2 = 10_00;
// Line n1
// A.
// Integer res = 250 + Integer.parseInt(s); // parseInt exception.
// B.
// Integer res = 250 + Integer.valueOf(s); // parseInt exception.
// C.
// Integer res = 250 + s2; // at the end, result = 1250 .
// D.
Integer res = 250;
res += s2; // At then end, result = 1250 .
// E.
// Integer res = 250 + s; // Syntax error.
// F.
// Integer res = 250 + Integer(s2); // Syntax error.
System.out.println(res);
}
}
Should be C and D, but D should have "+=" instead of "=+". I've tried all the alternatives in an online compiler and these are the only ones that work.
A and B would work if "s" were "1000", but parseInt() and valueOf() arent able to handle the underscore in "10_00".
E is just plain wrong cuz s is a string and cant be automatically cast to an int.
F is wrong cuz "Integer()" casting doesn't exist. One would instead use valueOf() to cast.
upvoted 3 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
SrinivasJasti
2 weeks, 2 days agoxplorerpj
6 months, 4 weeks agojames2033
10 months, 4 weeks agosupersquax
11 months, 2 weeks ago