I was asked these questions in my java interview and was asked to find issues with the code snippet.
First problem, I told them that the hashcode should not return static value as it will cause the values to be override in the hashmap.
public class Account {
int id;
float balance;
public int hashCode() {
return 42;
}
Second problem, I told them the catch block is missing print stack trace statement for debugging.
public class Account {
int id;
float balance;
public void processAccounts (){
try{
List<Account> accounts = loadAccounts();
accountsProcessing (accounts);
} catch (OutOfMemoryError e) {
System.gc();
}
}
}