Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Java - Object-Oriented Programming Concepts
What will be the output of this code?
class Cat {
  String name = "Kitty";
  void printName() {
    System.out.println(name);
  }
}
public class Main {
  public static void main(String[] args) {
    Cat c = new Cat();
    c.printName();
  }
}
Anull
BCompilation error
CKitty
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze class and method

    The class Cat has a field name initialized to "Kitty" and a method that prints it.
  2. Step 2: Check main method execution

    The main method creates a Cat object and calls printName(), which prints "Kitty".
  3. Final Answer:

    Kitty -> Option C
  4. Quick Check:

    Object method prints field value = Kitty [OK]
Quick Trick: Object fields print their assigned values [OK]
Common Mistakes:
  • Expecting null if not initialized
  • Confusing method call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes