174k views
4 votes
Given the below Java method, are there any (syntactical, semantical, logical) errors? What can be improved from the security-compliance perspective? If there are security-related issues, how can we fix them?

public class FinalExam {
private int noStudents;
private static final int MAX_SEATS = 26;
public void counter() {
noStudents = 0;
while (condition()) {
/* ... */
if (noStudents++ > MAX_SEATS) {
System.out.println("Room 215 does not have so many seats");
return;
}
private boolean condition() {/* ... */}
// No other method references noStudents
// but several other methods reference MAX_SEATS
}

1 Answer

5 votes

Final answer:

The given Java method has a syntactical error on line 11, and a logical error on line 9. Additionally, the access modifiers of the method can be improved for security compliance.

Step-by-step explanation:

The given Java method has a syntactical error on line 11, where the closing parentheses for the counter() method is missing. To fix this error, the closing parentheses should be added before the opening curly brace.

From a security-compliance perspective, there is a logical error on line 9. The condition noStudents++ > MAX_SEATS should be changed to ++noStudents > MAX_SEATS in order to accurately compare the updated value of noStudents with MAX_SEATS and avoid potential security issues.

In addition, the counter() method is missing proper access modifiers. To comply with security-compliance best practices, it is recommended to make the counter() method private to restrict access from outside the class.

User Jamband
by
8.4k points