Computer Applications
A student is trying to convert the string present in x to a numerical value, so that he can find the square root of the converted value. However the code has an error. Name the error (syntax / logical / runtime). Correct the code so that it compiles and runs correctly.
String x= "25";
int y=Double.parseDouble(x);
double r=Math.sqrt(y);
System.out.println(r);
Java Library Classes
ICSE Sp 2025
2 Likes
Answer
Syntax error in the line:int y=Double.parseDouble(x);
Corrected code:
String x= "25";
double y=Double.parseDouble(x); //int changed to double
double r=Math.sqrt(y);
System.out.println(r);
Explanation:
The error is a syntax error because the method Double.parseDouble(x)
returns a double
, but the code is trying to assign it to an int
variable without proper casting, which is not allowed in Java.
Answered By
1 Like
Related Questions
Write the output of the following String methods:
String x= "Galaxy", y= "Games";
(a) System.out.println(x.charAt(0)==y.charAt(0));
(b) System.out.println(x.compareTo(y));
Predict the output of the following code snippet:
char ch='B', char chr=Character.toLowerCase(ch); int n=(int)chr-10; System.out.println((char)n+"\t"+chr);
Consider the following program segment and answer the questions below:
class calculate { int a; double b; calculate() { a=0; b=0.0; } calculate(int x, double y) { a=x; b=y; } void sum() { System.out.println(a*b); }}
Name the type of constructors used in the above program segment?
Consider the following program segment and answer the questions given below:
int x[][] = {{2,4,5,6}, {5,7,8,1}, {34, 1, 10, 9}};
(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?