| 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 8 students
Java Static Class Members
< Prev
Next >

Consider the following code.

public class DataProcessor {
    private static int count = 0;
    
    public static void incrementCount() {
        count++;
    }
    
    public static int getCount() {
        return count;
    }
    
    public static void resetCount() {
        count = 0;
    }
}

public class Main {
    public static void main(String[] args) {
        DataProcessor.incrementCount();
        DataProcessor.incrementCount();
        int first = DataProcessor.getCount();
        DataProcessor.resetCount();
        DataProcessor.incrementCount();
        int second = DataProcessor.getCount();
        System.out.println(first + " " + second);
    }
}

Which statement best explains why the static methods in the DataProcessor class can access and modify the static count variable?

A

The code works because each method call creates a new instance of DataProcessor automatically

B

The methods work because they are called from another static method (main), creating a static context

C

Static methods can access static variables because both belong to the class rather than to any specific instance

D

Static methods can only read static variables but cannot modify them without creating an object

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
#1kaisuki11 0m 00s 100
#2chunxiangxu.cxu11 0m 12s 88
#3winstonhou110711 0m 55s 45
#4zgj0731041711 1m 03s 37
#5lsj0803092211 1m 25s 15
#6richa.tuli01 0m 44s -54
#7bommasam00014 14m 29s -799
Items per page:
10
1 – 7 of 7
No comments yet. Be the first to comment!

AI Tutor

How can I help?

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