Bird
0
0

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:
  1. Step 1: Identify constructor syntax

    A constructor has no return type and its name matches the class name.
  2. 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.
  3. Final Answer:

    public class Sample { Sample() { } } correctly defines a default constructor.
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes