Bird
0
0

Which of the following is the correct syntax for a parameterized constructor in Java?

easy📝 Syntax Q12 of 15
Java - Constructors

Which of the following is the correct syntax for a parameterized constructor in Java?

public class Car {
    String model;
    int year;

    // Constructor here
}
Avoid Car(String model, int year) { this.model = model; this.year = year; }
Bpublic Car(String model, int year) { this.model = model; this.year = year; }
Cpublic Car() { model = ""; year = 0; }
Dpublic void Car(String model, int year) { model = model; year = year; }
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor syntax

    A constructor has no return type and matches the class name exactly.
  2. Step 2: Verify parameter usage

    public Car(String model, int year) { this.model = model; this.year = year; } correctly uses parameters and assigns them to fields with this.
  3. Final Answer:

    public Car(String model, int year) { this.model = model; this.year = year; } -> Option B
  4. Quick Check:

    Constructor syntax = no return type + class name [OK]
Quick Trick: Constructor name = class name, no return type [OK]
Common Mistakes:
  • Adding return type to constructor
  • Not using 'this' to assign fields
  • Using void or other return types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes