Bird
0
0

Find the error in the following code snippet:

medium📝 Debug Q6 of 15
Java - Classes and Objects
Find the error in the following code snippet:
class Car {
  String model;
  Car(String m) {
    model = m;
  }
}
public class Test {
  public static void main(String[] args) {
    Car c = new Car;
  }
}
Amodel variable is not initialized
BConstructor name does not match class name
CNo error, code is correct
DMissing parentheses after Car in object creation
Step-by-Step Solution
Solution:
  1. Step 1: Check object creation syntax

    The line Car c = new Car; is missing parentheses after new Car, which is required.
  2. Step 2: Verify other parts

    The constructor name matches the class, and model is initialized in constructor, so no other errors.
  3. Final Answer:

    Missing parentheses after Car in object creation -> Option D
  4. Quick Check:

    Parentheses required after constructor call [OK]
Quick Trick: Always add () after new ClassName to call constructor [OK]
Common Mistakes:
  • Omitting parentheses after new keyword
  • Confusing constructor name mismatch
  • Ignoring initialization in constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes