| preferred AP College board partner for AP classes
AP Computer Science A/Unit 9: Inheritance
Start Practice TestPractice Test
Share
About Exam
medium Solved by 1 students
Polymorphism and Method Overriding
< Prev
Next >

Consider the following code. How many times will the body of the for loop execute when the main method is run?

class Parent {
    public void runLoop() {
        for (int i = 1; i <= 3; i++) {
            System.out.print(i + " ");
        }
    }
}

class Child extends Parent {
    @Override
    public void runLoop() {
        for (int i = 0; i < 4; i++) {
            System.out.print(i + " ");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        p.runLoop();
    }
}
A

4

B

3

C

7

D

0

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy