Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
Java - Classes and Objects
Identify the error in the following code snippet:
class Person {
    String name;
    Person(String n) {
        name = n;
    }
}

public class Test {
    public static void main(String[] args) {
        Person p = Person("Alice");
        System.out.println(p.name);
    }
}
AMissing semicolon after System.out.println
BConstructor name does not match class name
CVariable p is not declared
DMissing new keyword when creating object
Step-by-Step Solution
Solution:
  1. Step 1: Check object creation syntax

    The code tries to create an object with Person("Alice") but misses the new keyword.
  2. Step 2: Confirm other parts are correct

    Constructor name matches class name, variable is declared, and semicolon is present.
  3. Final Answer:

    Missing new keyword when creating object -> Option D
  4. Quick Check:

    Use new keyword to create objects [OK]
Quick Trick: Always use new before class name to create objects [OK]
Common Mistakes:
  • Omitting new keyword
  • Confusing method call with object creation
  • Incorrect constructor naming

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes