Bird
0
0

Consider this Java OOP code:

medium📝 Predict Output Q5 of 15
Java - Object-Oriented Programming Concepts
Consider this Java OOP code:
class Car {
  int speed = 0;
  void accelerate() { speed += 10; }
}
Car myCar = new Car();
myCar.accelerate();
System.out.println(myCar.speed);

What is the output?
A0
B10
CCompilation error
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Understand initial speed and method effect

    speed starts at 0, accelerate adds 10.
  2. Step 2: After calling accelerate, speed is 10

    Print statement outputs 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Method changes attribute value = A [OK]
Quick Trick: Method changes object state; print updated value [OK]
Common Mistakes:
  • Assuming speed remains 0
  • Expecting null or error without object initialization
  • Confusing method call effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes