Arizona State University (ASU) CSE110 Principles of Programming Exam 1 Practice

Session length

1 / 500

Which statement is correct to convert a string str to a double?

double n = str.parseDouble();

double n = Integer.parseDouble(str);

double n = Double.parseDouble(str);

In Java, to convert a string representation of a number into a double primitive type, the method `Double.parseDouble(String s)` is used. This method belongs to the `Double` class and specifically handles the conversion from a `String` to a `double`. The correct answer demonstrates this proper usage by directly invoking the method on the `Double` class, passing the string as the argument.

The other options either reference incorrect types or methods that do not exist in the Java language. The option that suggests using `Integer.parseDouble(str)` is incorrect because `Integer` class deals with integers, not doubles, and there's no `double.parseDouble(str)` since `double` is a primitive type and does not have methods. The first option incorrectly tries to call a method that does not exist on a string, leading to confusion. Thus, the correct method usage emphasizes understanding of class methods in Java and their correct applications for type conversion.

Get further explanation with Examzify DeepDiveBeta

double n = double.parseDouble(str);

Next Question
Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy