1. Given the following code:

    String string = "0";
    int x = Integer.parseInt (string);

    Which of these statements is correct and why?

A) The code goes in error because the variable string is not an integer
B) The code does not in error because the string "0" converts to the number 0
C) The code goes in error because the method parseInt () is not static is then to invoke it must instantiate an object of class Integer

  1. Given the following code:

    String string = "Hello";
    int x = Integer.parseInt (string);

    Which of these statements is correct and why?

A) The code goes in error because the variable string is not an integer
B) The code goes in error because the JVM tries to convert the string "Hello" in a number but the conversion fails
C) The code goes in error because the method parseInt () is not static is then to invoke it must instantiate an object of class Integer

Recommended Answers

All 4 Replies

So ... what do you think it is?

You do realize it would only take you half the time it took to post it here, to actually test what the outcome would be, right?

As it regards the first case I think is the B (I tried the code and the result is 0), but as regards the second case I get confused between answers A and B (only because they seem very similar as answers).
Of course I also tried the second case and obviously the code goes wrong (NumberFormatException). But why? Because of answer A or B, for me how I already write above, 2 answers are very similar.

Since I'm still at the beginning, I'm probably doing a bit of confusion. No need to rage just because someone wants to learn and asks for help from someone more experienced.

Wanting to learn, and copy pasting your assignment here, aren't the same.

String string = "0";
int x = Integer.parseInt (string);

Which of these statements is correct and why?

A) The code goes in error because the variable string is not an integer
B) The code does not in error because the string "0" converts to the number 0
C) The code goes in error because the method parseInt () is not static is then to invoke it must instantiate an object of class Integer

Here your guess is B, which is correct. It doesn't cause any problems, because the String string, with value "0", can easily be parsed to a numeric value.

String string = "Hello";
int x = Integer.parseInt (string);

Which of these statements is correct and why?

A) The code goes in error because the variable string is not an integer
B) The code goes in error because the JVM tries to convert the string "Hello" in a number but the conversion fails
C) The code goes in error because the method parseInt () is not static is then to invoke it must instantiate an object of class Integer

Here, you are correct with your thoughts that C is not the right answer, but A and B are not similar at all. I'll even make it simpler: by the answer of the first question, you can already eliminate A as possible answer. In the first question "0" was not an integer either, yet it worked. So, that is not the problem.

Where as "0" is a String representation of a numerical value (within the limitations of int), "Hello" clearly is not. The exception (NumberFormatException) you get, shows it: the compiler tries to parse it, but doesn't know the format of number you pass.

Thanks a lot for your kindness in answering, clarifying my doubts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.