Java Numeric Literals and Operators for 1Z0-829

Answer Correct answer: C, D — Option C adds the integer value of s2 to 250, and Option D (assuming intended += operator) performs compound addition to result in 1250.

Given the code fragment: Which two statements at Line n1 independently enable you to print 1250? (Choose two.) - image

  1. Integer res = 250 + Integer.parseInt(s);
  2. Integer res = 250 + Integer.valueOf(s);
  3. Integer res = 250 + s2; Correct Answer
  4. Integer res = 250; Correct Answer
  5. Integer res = 250 + s;

Community Votes

CD
100%

100% of anonymous learners picked answer CD. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

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.

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.

Community Discussion (4 comments)

SrinivasJasti 👍 1 Selected: CD
thousand separator in numeric literals is accepted from java7 10_00 means 1000
xplorerpj 👍 1
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;
james2033 👍 2 Selected: CD
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); } }
supersquax 👍 3
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.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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?

String parsing methods do not accept underscore separators; they are only supported in source code numeric literals.

Is =+ a valid operator in Java?

No, =+ is not a standard compound operator. It is likely a typo for += in the exam question text.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide