Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 9: Inheritance
Start Practice TestPractice Test
About Exam
medium

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

0

B

7

C

3

D

4

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question
Mark for review

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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