LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Predict the Output · Question 18

Converting between strings and numbers Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 18 Back to Exercise

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        String a = "25", b = "17";
        System.out.println(a + b);
        System.out.println(Integer.parseInt(a) + Integer.parseInt(b));
        System.out.println(Integer.parseInt("ff", 16) + " " + Integer.toBinaryString(10) + " " + Double.parseDouble("1e3"));
        try {
            Integer.parseInt(" 42");
        } catch (NumberFormatException e) {
            System.out.println("bad number");
        }
    }
}
  • A 2517 42 255 1010 1000.0 bad number Correct
  • B 42 42 255 1010 1000.0 bad number
  • C 2517 42 255 1010 1e3 bad number
  • D 2517 42 255 1010 1000.0 42
Correct Answer: Option A

+ on Strings concatenates. parseInt does not accept spaces – trim or strip input first.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links