Oracle
1Z0-819 · Question #190
Which method throws an exception for not-a-number and infinite input values?``java // A static float validate1(String s, float min, float max) throws IllegalArgumentException { return…
The correct answer is A. // A static float validate1(String s, float min, float max) throws IllegalArgumentException { return Float.parseFloat(s); }. See the full explanation below for the reasoning.
Question
Which method throws an exception for not-a-number and infinite input values?```java
// A
static float validate1(String s, float min, float max) throws IllegalArgumentException {
return Float.parseFloat(s);
}
// B
static float validate2(String s, float min, float max) throws IllegalArgumentException {
float f = Float.parseFloat(s);
if (Float.isNaN(f) || f < min || f > max) {
throw new IllegalArgumentException();
}
return f;
}
// C
static float validate3(String s, float min, float max) throws IllegalArgumentException {
float f = Float.parseFloat(s);
if (Float.isNaN(f) || f == Float.POSITIVE_INFINITY || f == Float.NEGATIVE_INFINITY || f < min || f > max) {
throw new IllegalArgumentException();
}
return f;
}
// D
static float validate4(String s, float min, float max) throws IllegalArgumentException {
float f = Float.parseFloat(s);
if (Float.isInfinite(f) || f < min || f > max) {
throw new IllegalArgumentException();
}
return f;
}```
Options
- A// A static float validate1(String s, float min, float max) throws IllegalArgumentException { return Float.parseFloat(s); }
- B// B static float validate2(String s, float min, float max) throws IllegalArgumentException { float f = Float.parseFloat(s); if (Float.isNaN(f) || f < min || f > max) { throw new IllegalArgumentException(); } return f; }
- C// C static float validate3(String s, float min, float max) throws IllegalArgumentException { float f = Float.parseFloat(s); if (Float.isNaN(f) || f == Float.POSITIVE_INFINITY || f == Float.NEGATIVE_INFINITY || f < min || f > max) { throw new IllegalArgumentException(); } return f; }
- D// D static float validate4(String s, float min, float max) throws IllegalArgumentException { float f = Float.parseFloat(s); if (Float.isInfinite(f) || f < min || f > max) { throw new IllegalArgumentException(); } return f; }
How the community answered
(22 responses)- A73% (16)
- B14% (3)
- C5% (1)
- D9% (2)
Community Discussion
No community discussion yet for this question.