Bird
0
0

What will be the output of this Java code?

medium📝 Predict Output Q13 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 + " barks");
  }
}
public class Test {
  public static void main(String[] args) {
    Dog d = new Dog();
    d.name = "Max";
    d.bark();
  }
}
AMaxbarks
Bbarks Max
CMax barks
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the Dog class and method

    Dog has a name field and bark() prints name + " barks" with a space.
  2. Step 2: Trace main method execution

    Creates Dog object d, sets d.name = "Max", calls d.bark() which prints "Max barks".
  3. Final Answer:

    Max barks -> Option C
  4. Quick Check:

    Prints name + " barks" = A [OK]
Quick Trick: Prints field + string exactly as coded [OK]
Common Mistakes:
  • Forgetting space between name and 'barks'
  • Mixing order of printed words
  • Assuming compilation error without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes