Inheritance and Instance Variable Modification
Consider the following code segment. When this code is executed, how many times will the while loop in the countWhile method iterate?
class BaseCounter2 {
int limit = 8;
public void countWhile() {
int i = 0;
while(i < limit) {
System.out.print(i + " ");
i++;
}
}
}
class ChildCounter2 extends BaseCounter2 {
public ChildCounter2() {
limit = 5;
}
}
public class Main {
public static void main(String[] args) {
ChildCounter2 cc = new ChildCounter2();
cc.countWhile();
}
}
A
6
B
4
C
8
D
5
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 2 | 2 | 0m 00s | 200 |
| #2 | y.seong2027 | 0 | 1 | 0m 28s | -38 |
| #3 | suhanakochhar006 | 1 | 3 | 39m 36s | -2,296 |
| #4 | psak12 | 1 | 2 | 1h 41m | -5,997 |
Items per page:
10
1 – 4 of 4
APFIVE