can anyone help me?? (8) Assuming true is replaced with an appropriate test, the
ID: 3544921 • Letter: C
Question
can anyone help me??
(8)
Assuming true is replaced with an appropriate test, the geometric mean of two positive numbers is computed by:
public static double geoMean(double a, double b) {
if (! true) {
throw new IllegalArgumentException( );
}
double result = Math.sqrt(a * b);
return result;
}
The geometric mean is not defined if the numbers are not positive. Ignoring roundoff error, what is the postcondition of this method?
1.
(result == a || result == b)
2.
(result * result == a * b)
3.
(result + result == a + b)
4.
(result > a && result < b)
(9)
Assuming true is replaced with an appropriate test, the following method returns a String consisting of the first and last characters of the String parameter.
public static String firstAndLast(String s) {
if (! true) {
throw new IllegalArgumentException( );
}
int len = s.length( );
String result = s.substring(0,1) + s.substring(len - 1, len);
return result;
}
To avoid an index out of bounds error, what test should replace true in the method?
1.
(s.length( ) < 0)
2.
(s.length( ) > 0)
3.
(s.length( ) < 1)
4.
(s.length( ) > 1)
(10)
Assuming true is replaced with an appropriate test, the following method returns a String consisting of the first and last characters of the String parameter.
public static String firstAndLast(String s) {
if (! true) {
throw new IllegalArgumentException( );
}
int len = s.length( );
String result = s.substring(0,1) + s.substring(len - 1, len);
return result;
}
What are the postconditions of this method? (Select all that apply.)
1.
(result.length( ) < s.length( ))
2.
(result.length( ) == 2)
3.
(s.startsWith(result.substring(0,1)))
4.
(s.endsWith(result.substring(1,2)))
5.
(s.equals(result))
1.
(result == a || result == b)
2.
(result * result == a * b)
3.
(result + result == a + b)
4.
(result > a && result < b)
Explanation / Answer
Hi ,
The solutions are :
8) 2 (Since Gm*Gm = a*b )
9) 4 ( length shouldbe > 1, or else it will throw an error )
10) 2 ( The result must be 2 characters , since it is the first and last character of the original string )
Hope it helps :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.