Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 5: Writing Classes
Start Practice TestPractice Test
About Exam
medium Solved by 4 students

Static Class Members and Methods

Mark
< Prev
Next >

Consider the following class definition.

public class DatabaseConnection {
    private static int maxConnections = 5;
    private static int activeConnections = 0;
    private String connectionID;
    private boolean isConnected;
    
    public DatabaseConnection(String id) {
        connectionID = id;
        isConnected = false;
    }
    
    public boolean connect() {
        if (activeConnections < maxConnections) {
            activeConnections++;
            isConnected = true;
            return true;
        }
        return false;
    }
    
    public void disconnect() {
        if (isConnected) {
            activeConnections--;
            isConnected = false;
        }
    }
    
    public static int getActiveConnections() {
        return activeConnections;
    }
    
    public static boolean canConnect() {
        return activeConnections < maxConnections;
    }
}

What is printed when the following code segment is executed?

DatabaseConnection db1 = new DatabaseConnection("DB1");
DatabaseConnection db2 = new DatabaseConnection("DB2");
DatabaseConnection db3 = new DatabaseConnection("DB3");

db1.connect();
db2.connect();
db1.disconnect();
db3.connect();

System.out.println(DatabaseConnection.getActiveConnections() + " " + DatabaseConnection.canConnect());
A

2 true

B

2 false

C

1 true

D

3 false

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1chunxiangxu.cxu11 0m 05s 95
#2bommasam00034 4m 02s 48
#3y.seong202711 1m 57s -17
#4suhanakochhar00611 4m 43s -183
Items per page:
10
1 – 4 of 4

AI Tutor

How can I help?

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