🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: What Is OOP: all 10 questions

Quiz: What Is OOP · Question 7 of 10

What does this print? (code)

What does this print?

class Car {
    String brand;
    int speed;
    Car(String brand, int speed) { this.brand = brand; this.speed = speed; }
    void accelerate(int by) { speed += by; }
}

public class Main {
    public static void main(String[] args) {
        Car a = new Car("Swift", 10);
        Car b = new Car("Nexon", 10);
        a.accelerate(30);
        b.accelerate(5);
        a.accelerate(5);
        System.out.println(a.speed + " " + b.speed);
    }
}
  1. A.50 50
  2. B.45 15
  3. C.40 15
  4. D.45 45
Answer & explanation

Correct answer: 45 15

Each object keeps its own speed: a goes 10 + 30 + 5 = 45, b goes 10 + 5 = 15.

Can you get the rest right without peeking?

Take the timed Quiz: What Is OOP quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →