| preferred AP College board partner for AP classes
medium Solved by 2 students
Java Static Variables and Methods
< Prev
Next >

Consider the following class definition.

public class GameSession {
    private static int sessionCount = 0;
    private static int totalPlayers = 0;
    private int sessionID;
    private int players;
    
    public GameSession(int numPlayers) {
        sessionCount++;
        sessionID = sessionCount;
        players = numPlayers;
        totalPlayers += numPlayers;
    }
    
    public static void resetStats() {
        sessionCount = 0;
        totalPlayers = 0;
    }
    
    public int getSessionID() {
        return sessionID;
    }
    
    public static int getTotalPlayers() {
        return totalPlayers;
    }
}

What is printed as a result of executing the following code segment?

GameSession game1 = new GameSession(4);
GameSession game2 = new GameSession(2);
System.out.println(game1.getSessionID() + " " + GameSession.getTotalPlayers());
GameSession.resetStats();
GameSession game3 = new GameSession(3);
System.out.println(game2.getSessionID() + " " + GameSession.getTotalPlayers());
A

4 6
2 3

B

1 6
0 3

C

1 4
2 3

D

1 6
2 3

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

Question Leaderboard

Not enough data yet to show leaderboard.

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