Maventic interview question

A java program was given to solve.