Question 6
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
class MathUtils {
private MathUtils() { }
static int square(int x) { return x * x; }
}
public class Main {
public static void main(String[] args) {
MathUtils m = new MathUtils();
System.out.println(MathUtils.square(4));
}
}
- A square must not be static
- B The constructor is private, so Main cannot create a MathUtils; call the static method on the class instead Correct
- C Utility classes need public constructors
- D square needs an object
Correct Answer: Option B
A private constructor deliberately prevents instantiation of a utility class.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!