Bird
0
0

Given the class Movie, which constructor correctly initializes the instance variables title and director for each object?

hard📝 Application Q8 of 15
Java - Classes and Objects
Given the class Movie, which constructor correctly initializes the instance variables title and director for each object?
Apublic void Movie(String title, String director) { this.title = title; this.director = director; }
Bpublic Movie(String t, String d) { title = t; director = d; }
Cpublic Movie() { title = ""; director = ""; }
Dpublic Movie(String title, String director) { this.title = title; this.director = director; }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct constructor syntax

    A constructor must have no return type and the same name as the class.
  2. Step 2: Analyze options

    public Movie(String title, String director) { this.title = title; this.director = director; } correctly uses this to assign parameters to instance variables.
    public Movie(String t, String d) { title = t; director = d; } assigns without this, which works only if parameter names differ.
    public Movie() { title = ""; director = ""; } is a default constructor, not initializing variables from parameters.
    public void Movie(String title, String director) { this.title = title; this.director = director; } has a return type void, so it's a method, not a constructor.
  3. Final Answer:

    public Movie(String title, String director) { this.title = title; this.director = director; } correctly initializes instance variables.
  4. Quick Check:

    Constructor name matches class and no return type [OK]
Quick Trick: Constructor must match class name and have no return type [OK]
Common Mistakes:
  • Using void return type in constructor
  • Not using this keyword when parameter names match
  • Confusing methods with constructors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes