Which of the following correctly defines a default constructor in Java?
easy📝 Syntax Q3 of 15
Java - Constructors
Which of the following correctly defines a default constructor in Java?
Apublic class Sample { Sample(int x) { } }
Bpublic class Sample { void Sample() { } }
Cpublic class Sample { public void Sample() { } }
Dpublic class Sample { Sample() { } }
Step-by-Step Solution
Solution:
Step 1: Identify constructor syntax
A constructor has no return type and its name matches the class name.
Step 2: Check each option
public class Sample { Sample() { } } defines a constructor with no parameters and no return type, which is a default constructor. public class Sample { void Sample() { } } and C have return types (void), so they are methods, not constructors. public class Sample { Sample(int x) { } } is a parameterized constructor, not default.
Final Answer:
public class Sample { Sample() { } } correctly defines a default constructor.
Quick Check:
Constructor name = class name, no return type [OK]
Quick Trick:Constructor has no return type and matches class name [OK]
Common Mistakes:
Confusing methods with constructors by adding return type
Using parameters in default constructor
Incorrect constructor name
Master "Constructors" in Java
9 interactive learning modes - each teaches the same concept differently