nerdexam
Oracle

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…

Handling date, time, text, numeric and boolean values

Question

Which two statements at Line n1 independently enable you to print 1250?

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)
  • A
    72% (26)
  • B
    3% (1)
  • C
    3% (1)
  • D
    17% (6)
  • F
    6% (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 an int directly to a String; Java would either concatenate ("2501000") or throw a compile error depending on context.
  • C (integer(s2)) - lowercase integer is not a valid Java type or method; Java is case-sensitive and the correct wrapper class is Integer with a capital I.
  • D (250 + s2) - s2 is a String, so direct arithmetic addition fails for the same reason as B.
  • F (Res = s2) - Res with a capital R is an undeclared variable (Java is case-sensitive), and s2 is a String not assignable to Integer.

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

#String to Integer conversion#Integer.parseInt vs valueOf#Auto-boxing#Numeric type handling

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice