Java Numeric Literals and Operators for 1Z0-829
Short answer
CD Option C adds the integer value of s2 to 250, and Option D (assuming intended += operator) performs compound addition to result in 1250.
Question

Key Takeaways
The question tests Java 7+ underscore support in integer literals and the syntax of += versus =+. A common trap is misinterpreting the operator or failing to recognize that string parsing fails on underscores.
This question tests the use of underscore separators in numeric literals and compound assignment operators. The correct options enable printing 1250 by combining 250 with a variable representing 1000.
Common mistake: Candidates often reject Option D due to confusion over the operator syntax (+= vs =+) or fail to realize that Options A and B throw NumberFormatException because Integer.parseInt/valueOf do not accept underscores in strings.
Expert Analysis
Why the Answer Is Correct
Option C works because s2 is declared as an Integer literal 10_00, which Java 7+ interprets as 1000. Adding 250 + 1000 results in 1250. Option D, when interpreted as intended in the context of valid exam answers despite potential typo issues in display, likely implies a compound addition res += s2 or similar logic to reach 1250. However, strictly looking at the provided text "Integer res = 250; res =+ s2;" this is syntactically incorrect or ineffective. Let's re-read carefully. The prompt says "Which two statements... independently enable you to print 1250?". If D is literally Integer res = 250; res =+ s2;, =+ is not a standard operator in Java (it parses as = (+s2) which assigns positive 1000 to res, resulting in 1000). But community consensus and votes strongly point to CD. Let's look at C: Integer res = 250 + s2;. This is valid and equals 1250. What about D? If the option was res += s2; it would be correct. Given the strong vote and comment notes suggesting D should have been +=, and that A/B/E are definitely wrong, C is one answer. We need another. Is there a typo in my reading? Ah, the image might show res += s2;. Without the image, we rely on comments. Comments say "D should have '+=' instead of '=+'". And "CD but there's an error with the display... D. Integer res = 250; res =+ s2;... This will only return 1000". Wait, if D returns 1000, it's wrong. But votes are 100% CD. Let's look at A and B again. Integer.parseInt("10_00") throws exception. E is string concat. F doesn't exist. So C is definitely right. Is there another? Maybe the code fragment defines s2 differently? No, s2 is 10_00. If D is invalid, then the question is flawed. However, in certification contexts, if the 'intended' answer is CD, we assume D represents the logical addition. Let's assume the option D in the actual exam was res += s2;. Then C and D are correct.
Why the Other Options Are Wrong
Options A and B fail because Integer.parseInt and Integer.valueOf cannot parse strings containing underscores (like "10_00"). Option E attempts to add an int and a String, which results in string concatenation ("2501000"), not arithmetic sum.
Community Comment Notes
Users note that underscores are valid in numeric literals from Java 7 onwards. Several commenters highlight that Option D likely contains a typo in the text representation (=+ vs +=) but is considered correct in the context of the exam's intended logic for adding the values.
Exam Strategy
Always check for Java version features like underscore separators in numbers. Be wary of operator typos in practice questions; focus on the logical intent (addition vs assignment) when multiple options are ambiguous but others are clearly invalid.
Frequently Asked Questions
Why do parseInt and valueOf fail with underscores?
Is =+ a valid operator in Java?
Community Votes
100% of anonymous learners picked answer CD. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 4 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 4
-
supersquax 👍 3Should 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.
-
james2033 👍 2package 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); } }
-
xplorerpj 👍 1CD 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;
-
SrinivasJasti 👍 1thousand separator in numeric literals is accepted from java7 10_00 means 1000
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.