Question 8
Back to Exercise
What is the output of the following Java program?
class Login {
private int passwordHash;
private int failures;
public void setPassword(String pw) { passwordHash = pw.hashCode(); failures = 0; }
public boolean check(String pw) {
if (failures >= 2) return false;
boolean ok = pw.hashCode() == passwordHash;
failures = ok ? 0 : failures + 1;
return ok;
}
}
public class Main {
public static void main(String[] args) {
Login l = new Login();
l.setPassword("Java@21");
System.out.println(l.check("java@21") + " " + l.check("Java@21") + " " + l.check("x") + " " + l.check("y") + " " + l.check("Java@21"));
}
}
- A false true false false false Correct
- B false true false false true
- C true true false false true
- D false false false false false
Correct Answer: Option A
A success resets failures; after two failures in a row the account locks, so even the correct password is refused. (String.hashCode is not a real password hash – it is only for illustration.)
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!