For the empty class below, what is the exact prototype of its compiler-provided default constructor? public class Test { }

Difficulty: Easy

Correct Answer: public Test()

Explanation:


Introduction / Context:
When you do not declare any constructor, the Java compiler provides a no-argument default constructor. Understanding its signature and access level is important for instantiation and subclassing.



Given Data / Assumptions:

  • An empty public class named Test.
  • No user-declared constructor is present.


Concept / Approach:
The compiler synthesizes a no-arg constructor with the same access level as the class. In this case the class is public, so the generated constructor is public Test(). Java does not use void in constructor parameter lists, so signatures like Test(void) are invalid.



Step-by-Step Solution:

No explicit constructor detected → compiler inserts a default.Class access is public → constructor is also public.Constructor has no parameters and no return type → public Test().


Verification / Alternative check:
Decompile the compiled class or use javap -p Test to confirm the generated constructor signature.



Why Other Options Are Wrong:
Test() misses the required public modifier; Test(void) and public Test(void) are illegal; protected Test() has the wrong access.



Common Pitfalls:
Thinking constructors can declare a return type or void; forgetting that visibility matches the class when synthesized.



Final Answer:
public Test()

More Questions from Declarations and Access Control

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion