Static Class Members and Methods
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 false
B
3 false
C
2 true
D
1 true
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | chunxiangxu.cxu | 1 | 1 | 0m 05s | 95 |
| #2 | bommasam000 | 3 | 4 | 4m 02s | 48 |
| #3 | y.seong2027 | 1 | 1 | 1m 57s | -17 |
| #4 | suhanakochhar006 | 1 | 1 | 4m 43s | -183 |
Items per page:
10
1 – 4 of 4
APFIVE