Bird
0
0

Find the error in this Java class:

medium📝 Debug Q7 of 15
Java - Classes and Objects
Find the error in this Java class:
class Student {
  String name;
  void Student(String n) {
    name = n;
  }
}
AConstructor has void return type
BField name is not initialized
CClass name should be lowercase
DMissing semicolon after field
Step-by-Step Solution
Solution:
  1. Step 1: Understand constructor syntax

    Constructors in Java do not have a return type, not even void.
  2. Step 2: Identify the error in method declaration

    The method void Student(String n) looks like a constructor but has void return type, so it's a regular method, not a constructor.
  3. Final Answer:

    Constructor has void return type -> Option A
  4. Quick Check:

    Constructors have no return type [OK]
Quick Trick: Constructors never have return types [OK]
Common Mistakes:
  • Adding void to constructor
  • Confusing methods with constructors
  • Ignoring missing semicolons

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes