Consider the following code segment. What is printed as a result of executing the code segment?
String word = "methods";
int n = word.length();
double root = Math.sqrt(16);
System.out.println(n + " " + root);
8 4.0methods 167 47 4.0word.length() returns 7 and Math.sqrt(16) returns the double value 4.0, so both parts print together.
Explanation
An API entry states what kind of result comes back. int length() returns an int, and the word "methods" has 7 letters. The Math entry returns a double, so the square root keeps a decimal part.
- A is not correct: the word
"methods"has7letters rather than8, andlength()returns that count, so the first printed value is7. - B is not correct: the program prints the values stored in
nandroot, not the original text and the argument that was given tosqrt. - C is not correct:
Math.sqrt(16)returns adouble, so the value stored inrootprints as4.0rather than as a whole number.