1Z0-829 · Question #6
Which two statements at Line n1 independently enable you to print 1250?
The correct answer is A. Integer res = 250 + integer.parseInt(s) E. Integer res = 250 + integer .valueOf(s). Options A and E both correctly convert the String variable s (which holds "1000") into a numeric type before performing addition. Integer.parseInt(s) returns an int primitive, and Integer.valueOf(s) returns an Integer object that is auto-unboxed - both yield 250 + 1000 = 1250…
Question
Options
- AInteger res = 250 + integer.parseInt(s)
- BInteger res = 250 + s;
- CInteger res = 250 + integer (s2);
- DInteger res = 250 + s2;
- EInteger res = 250 + integer .valueOf(s);
- FRes = s2;
How the community answered
(36 responses)- A72% (26)
- B3% (1)
- C3% (1)
- D17% (6)
- F6% (2)
Explanation
Options A and E both correctly convert the String variable s (which holds "1000") into a numeric type before performing addition. Integer.parseInt(s) returns an int primitive, and Integer.valueOf(s) returns an Integer object that is auto-unboxed - both yield 250 + 1000 = 1250.
Why the distractors fail:
- B (
250 + s) - you cannot add anintdirectly to aString; Java would either concatenate ("2501000") or throw a compile error depending on context. - C (
integer(s2)) - lowercaseintegeris not a valid Java type or method; Java is case-sensitive and the correct wrapper class isIntegerwith a capital I. - D (
250 + s2) -s2is aString, so direct arithmetic addition fails for the same reason as B. - F (
Res = s2) -Reswith a capital R is an undeclared variable (Java is case-sensitive), ands2is aStringnot assignable toInteger.
Memory tip: When you see a String needing arithmetic in Java, remember the two Integer doorways: parseInt → gives you a primitive int, valueOf → gives you a boxed Integer. Both sit on the Integer class with a capital I - spot any lowercase integer and you've found your trap answer.
Topics
Community Discussion
No community discussion yet for this question.