Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java programming test for interview

Tags:

java

oop

Here is a programming test used in a job interview. I find it has a very strange non-OO perspective and wonder why anyone would approach a constructor from this perspective. As a very experienced Java programmer, I immediately question the ability of the individual who wrote this code and the strange perspective of the question.

I find these strange out of context questions on interviews disturbing. I would love feedback from other experienced OO Java programmers.

Complete the Solver constructor so that a call to solveAll return a list with 2 values including the square root and the inverse of the integer passed as parameter.

public interface MathFunction {     double calculate(double x); }  public class Solver {      private List<MathFunction> functionList;      public Solver() {           //Complete here      }      public List<Double> solveAll(double x) {         List<Double> result = new ArrayList<Double>();         for (MathFunction function : this.functionList) {             result.add(new Double(function.calculate(x)));         }          return result;     } }  
like image 915
user916115 Avatar asked Aug 21 '12 14:08

user916115


1 Answers

This is testing your design patterns, by using the simplest possible method. I think this could be the Strategy (or some other behavioural pattern). See these:

http://en.wikipedia.org/wiki/Strategy_pattern

http://en.wikipedia.org/wiki/Behavioral_pattern

If you are going for a Java interview, you should be able to identify the design pattern they are hinting at and that should prevent you from being too unsettled!

To answer the question, create two classes that implement MathFunction as required, then create two instances and store them in functionList.

The point here is not 'can you do calculations in this strange way', it is 'can you identify design patterns'.

like image 72
Joe Avatar answered Sep 20 '22 17:09

Joe