Bird
0
0

What will be the output of this Java code?

medium📝 Predict Output Q4 of 15
Java - Object-Oriented Programming Concepts
What will be the output of this Java code?
class Dog {
  String name;
  void bark() {
    System.out.println(name + " says Woof!");
  }
}
public class Test {
  public static void main(String[] args) {
    Dog d = new Dog();
    d.name = "Buddy";
    d.bark();
  }
}
AWoof! Buddy says
Bnull says Woof!
CBuddy says Woof!
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation and field assignment

    A Dog object is created and its name field is set to "Buddy".
  2. Step 2: Analyze the bark method output

    The bark method prints the name followed by " says Woof!" so output is "Buddy says Woof!".
  3. Final Answer:

    Buddy says Woof! -> Option C
  4. Quick Check:

    Object field used in method = Buddy says Woof! [OK]
Quick Trick: Set object fields before using them [OK]
Common Mistakes:
  • Forgetting to assign name
  • Expecting method to return value
  • Confusing print order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes