Maventic Interview Question

A java program was given to solve.