| preferred AP College board partner for AP classes
AP Computer Science A/Unit 2: Using Objects
Start Practice TestPractice Test
About Exam
easy Solved by 4 students
Variable Shadowing with an Instance Variable
< Prev
Next >

Consider the following ShadowError class. When the main method is executed, the program prints 0. Which of the following statements best explains this behavior?

public class ShadowError {
    int counter = 0;
    
    public void increase() {
        for (int i = 0; i < 5; i++) {
            int counter = 0;
            counter += i;
        }
        System.out.println(counter);
    }
    
    public static void main(String[] args) {
        ShadowError s = new ShadowError();
        s.increase();
    }
}
A

The inner loop variable ‘i’ is not used in any meaningful computation.

B

The counter should be incremented outside of the loop to accumulate the total.

C

The loop termination condition is off, leading to fewer iterations than expected.

D

The variable ‘counter’ is redeclared inside the loop, which shadows the instance variable and prevents its intended update.

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1samyrabahfc22 0m 00s 200
#2zgj0731041701 0m 23s -33
#3lsj0803092211 4m 25s -165
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy